Can anyone please tell me why this session variable isn't being set? When the login details are correct the offers page is re-directing users back to the index as though the session variable isn't set.
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
mysql_connect("localhost", "******", "******") or die("Could not connect.");
mysql_select_db("*******") or die("Could not find database.");
if(($username=='')||($password==''))
{
echo"<script type='text/javascript'>;
alert('Please check and re-enter details');
window.location = 'index.php';
</script>";
}
$qry="SELECT*FROM login WHERE username = '$username' and password = '$password'";
$result=mysql_query($qry);
if(mysql_num_rows($result)==0)
echo "<script type='text/javascript'>;
alert('The username you have entered does not exist in our database. Please check ad re-enter details.');
window.location = 'index.php';
</script>";
if(mysql_num_rows($result)> 0)
{
$_SESSION['username'] = $username;
header('location: offers.php');
}
?>
offers.php code
<?php
if ($_SESSION["username"]=="")
{
header ('Location: index.php');
}
?>
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.