Thursday, 10 August 2017

PHP - header('location:profile.php'); not working




when I hit login it keeps saying invalid login information. I want it to go to profile.php
I used header('location:profile.php') I wonder whats wrong? may you guys take a look please, and let me know if I'm missing something or had typos. thanks!




    //database information
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

//connect to database
mysql_connect($host, $user, $pass);

mysql_select_db($db);

//select table
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1";
$res = mysql_query($sql);

?>





Admin Login




 






















Username
Password

if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];

if (mysql_num_rows($res) == 1) {
echo "You have successfully logged in.";
exit();
} else {
echo "Invalid log in information.";
exit();
header ('location:profile.php');
}
}
?>

 







Answer



Here is your code:



echo "Invalid log in information.";
exit();
header ('location:profile.php');



First when you execute an echo the server sends headers to the browser, so you can’t have that before a header call. But past any of that you have an exit(); before the header ('location:profile.php'); call. So that will never execute it. Just do this instead:



// echo "Invalid log in information.";
// exit();
header ('location:profile.php');


Commenting out the echo and exit just in case there is a valid reason for you to have those there. Perhaps for debugging? But if this is in production, those are not needed.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...