2013-02-27 148 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