I try to make a php user login with sessions. I have 3 files. main_login.php, checklogin.php and index.php. After the username and the password are registered it should direct me to the index.php file, but it doesn't do so.
main_login.php code:
<html>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
checklogin.php code:
<?php
session_start()
include('connection.php');
if(!$conn)
{
die('Could not connect: ' . mysql_error());
}
$tbl_name="user"; // Table name
// 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 "login_success.php"
$_SESSION['myusername'] = "$myusername";
$_SESSION['mypassword'] = "$mypassword";
print_r($_SESSION);
if ($_SESSION['myusername']) {
header("location:login_success.php");
}
}
else {
echo "Wrong Username or Password";
}
?>
login_success.php code:
<?php
session_start();
include('connection.php');
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
I don't get any error messages. It connects fine to the database. I am not sure what the problem is. I think either the sessions are wrong registered or it has something to do with the header.
Help would be very much appreciated.