我做了一個名爲The Recipe Center的localhost網站。在這個網站上,您可以發佈食譜並在食譜上發表評論。 。爲了發佈一個配方或評論,您必須登錄這對於驗證的登錄名,或者說PHP文件中的代碼,設置會話cookie:PHP網頁沒有設置SESSION cookie
<?php
$con = mysql_connect("localhost", "test", "test") or die('Could not connect to server');
mysql_select_db("recipe", $con) or die('Could not connect to database');
$userid = $_POST['userid'];
$password = $_POST['password'];
$query = "SELECT userid from users where userid = '$userid' and password = PASSWORD('$password')";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
{
echo "<h2>Sorry, your user account was not validated.</h2><br>\n";
echo "<a href=\"index.php?content=login\">Try again</a><br>\n";
echo "<a href=\"index.php\">Return to Home</a>\n";
} else
{
$_SESSION['valid_recipe_user'] = $userid;
echo "<h2>Your user account has been validated, you can now post recipes and comments</h2><br>\n";
echo "<a href=\"index.php\">Return to Home</a>\n";
}
?>
每當我對數在網站上,它說:「您的用戶帳戶已被驗證,您現在可以發佈食譜和評論。」但是,每當我瀏覽到不同的網頁,嘗試發佈一個配方或一個評論,它說我沒有登錄,這是配方後的PHP文件中的代碼:
<?php
if(!isset($_SESSION['valid_recipe_user'])){
echo "<h2> Sorry, you do not have permission to post recipes.</h2>\n";
echo "<a href=\"index.php?content=login\">Please login to post recipes</a>\n";
}
else {
...
}
?>
誰能解釋我爲什麼會話cookie在登錄頁面中設置,但當我導航到不同的頁面時消失?
ah/facepalm我怎麼會錯過這個!? – meteorainer