user6546278 user6546278 - 5 months ago 16
PHP Question

php log in not working online but works perfectly fine offline

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!

Answer

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.