非工作会话变量

时间:2013-02-27 14:43:15

标签: php mysql

任何人都可以告诉我为什么没有设置这个会话变量?当登录详细信息正确时,商品页面会将用户重新定向回索引,就像未设置会话变量一样。

<?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代码

<?php
if ($_SESSION["username"]=="") 
{
header ('Location: index.php');
}
?>

2 个答案:

答案 0 :(得分:2)

您需要将session_start();放在offers.php页面的顶部。

答案 1 :(得分:1)

尝试添加:

session_start();

到offers.php的顶部