0

I have created a log in page in php ('login.php) that works fine offline. But when i take it online, the log in fails. However the other pages connect seamlessly to the same database from where I pull in the log in details. Here's the code on the log in page:

    <!DOCTYPE HTML>
    <html>
    <head>
    <link rel="stylesheet" href="css/admin_style.css">
    <body>


    <div id="login_style">
    <div class="login_head">
    <img src="images/admin_logo.png" alt="Parlour Products">
    <img src="images/admin_panel.png" width="200" height="75" alt="Admin Panel"             class="adminPanel">
    </div>
    <br><br>
    <h3>
<?php echo @$_GET['false_admin']; ?>
</h3>
<h3>
<?php echo @$_GET['logged_out']; ?>
</h3>

<h1> Admin Section</h1><br>
        <form method="post">
        User Id: <input type="text" name="name" placeholder="Enter User ID" required="required" />
        Password: <input type="password" name="pass" placeholder="Password" required="required" /><br><br>
        <button type="submit" name="login">Log In</button>
    </form>

</div>

</body>
</html>

<?php

session_start();

include ("includes/db.php");

if(isset($_POST['login'])){

      $name = mysql_real_escape_string($_POST['name']);
      $pass = mysql_real_escape_string($_POST['pass']);

      $sel_user = "select * from admin where admin_name='$email' AND admin_pass='$pass'";
      $run_user = mysqli_query($con, $sel_user);

      $check_user = mysqli_num_rows($run_user);

      if($check_user==0 ){
          echo "<script>alert('Login Failed. Please Try Again!')</script>";         
          }
      else {
          $_SESSION['user_email']=$email;  
          echo "<script>window.open('index.php?logged_in=Sucessfully Logged In!','_self')</script>";

    }
}

?>

But every time I try to enter with the correct user name and password, I get the login error message. Initially I thought it was a database error, so I created this 'test.php' page with the following code:

<form action="test.php" method= "post" >

<table>
<tr>
<td colspan="4" align="left"><h1>Insert New User</h1></td>
</tr>
<tr>
<td><input type="text" name="user_name"/></td>
<td><input type="text" name="user_pass"/></td>
<td><input type="submit" name="add_user" value="Add User" /></td>
</tr> 
</table>

</form>



<?php

include("includes/db.php");

if(isset($_POST['add_user'])){

    $new_name = $_POST['user_name'];
    $new_pass = $_POST['user_pass'];

    $insert_user = "insert into admin (admin_name,admin_pass) values ('$new_name', '$new_pass')";

    $run_user = mysqli_query($con,$insert_user);

    if($run_user){
        echo "<script>alert('New User Added!')</script>";
        echo "<script>window.open('test.php','_self')</script>";
        }

    }

?>

This 'test.php' page connects fine and even inserts the data into the table. The table structure is same both online and offline. Other user login pages are all working fine too!

Kironjit
  • 3
  • 1
  • I created a separate table remotely and put in the user data through the test.php file. I've checked that the user data is present on the remote database too. – Kironjit Jul 04 '16 at 08:29
  • try removing mysql_real_escape_string, maybe it corrupts your data somehow. also, you can debug your application and see if your query data is right – Ori Price Jul 04 '16 at 08:36
  • you are passing the wrong variable in select query to admin_name, you should use a $name instead of $email – sayli bhagwat Jul 04 '16 at 08:42
  • Thanks, Sayli... rectified the variable name. Actually, the error crept into the code when I was tweaking it, before posting this question. The original file has it corrected. But the issue still remains... – Kironjit Jul 04 '16 at 09:14
  • Had the same problem. Check this out, worked for me: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – user5791323 Jan 24 '17 at 22:14

1 Answers1

0

First of all use the error reporting function when you're not sure of the error:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>

Probably the start session function is creating the problem here. try moving the

session_start();

function to the top of the page, before any other text is outputted. Hopefully, this should solve it.