I am having a login page which has a form. The login data are send into a php script to check whether the user exists in the DB. If successful, it should redirect.
Here is my code:
<?php
$host="localhost:8889"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="TableName"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file
session_register("myusername");
session_register("mypassword");
header("location:../main.php");
die();
}
else {
echo "Wrong Username or Password";
}
?>
Count is 1, but redirection does not happen.
I have read this great answer and checked the following:
- there is not empty space before the opening tag, nor I am having another echo or print.
- i tried after the opening php tag to put
ob_start
but it didn't work either.
I didn't understand the thing with BOM. Can you help me on that?
I have been using the exact same code in the past and it worked and now that I am on mamp and mac does not work.
Thanks.
EDIT:
I did what people have said and after fixing these things and enabling reports, and the problem is that:
Call to undefined sesion_register.
So, I changed that as it is not defined nor and it is deprecated and it works now! Thanks all!