2013-02-27 75 views
0

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

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

[**在新的代码,请不要使用'mysql_ *'功能**](HTTP的顶部://位.ly/phpmsql)。他们不再被维护[并且被正式弃用](http://j.mp/XqV7Lp)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。 – Kermit 2013-02-27 14:44:10

+0

^至少消毒您的用户输入 – Nick 2013-02-27 14:46:50

+0

Thankyou - Aarolama,那么我只是用mysqli替换mysql然后在上面的实例中? – maccame 2013-02-27 15:07:24

回答

2

你需要把session_start();你offers.php页面的顶部。

1

尝试增加:

session_start(); 

到offers.php