I am trying to create my very first site login system. The following code seems to work just fine right up until it is supposed to redirect to the “login_success.php” file once it gets into the if function. I know it makes it into the if because of the 2 echo statements which are there just for debugging. All I get when I run this is an output of the $fusername (thomas) and the $count (1) with the url never changing. Can anyone help with why this script does not complete the redirection? I have also included the landing page code. Thanks in advance! Tom
CODE AT sfjlogin.php…
<?php session_start(); // IMPORT FORM VARIABLES $fusername=$_POST['fusername']; $fpassword=$_POST['fpassword']; // CONNECT TO SERVER AND SELECT DATABASE mysql_connect("localhost", "scripts3_public", "*******")or die("cannot connect"); mysql_select_db("scripts3_sfj")or die("cannot select DB"); // PROTECT AGAINST MYSQL INJECTION mysql_real_escape_string($fusername); mysql_real_escape_string($fpassword); // ENCRYPT FORM PASSWORD TO COMPARE WITH DATABASE ENCRYPTED PASSWORD $encrypt_fpassword=md5($fpassword); // LOOKUP USERNAME AND PASSWORD IN DATABASE COMPARED TO FORM ENTRIES $sql="SELECT * FROM `users` WHERE `User name` = '$fusername' AND `Password` = '$encrypt_fpassword'"; $result=mysql_query($sql); if(!mysql_num_rows($result)) {echo "No results returned.";} // COUNT RESULTS $count=mysql_num_rows($result); // IF COUNT IS 1,REGISTER USERNAME AND PASSWORD AND REDIRECT if($count==1){ echo $count; echo $fusername; $_SESSION['fusername']=$fusername; $_SESSION['fpassword']=$fpassword; header('Location: http://www.*******.com/login_success.php'); die(); } else { echo "Wrong Username or Password"; } ?>LANDING FILE CODE…
<?php session_start(); if(!$_SESSION['fusername']){ header("Location: sfjlogin.php"); die(); } ?> Login Successful