[英]PHP Sessions not saved
我的會議沒有進行。 每次我重新加載頁面時,它都會生成一個新的會話ID並刪除所有會話變量。.我不確定我的代碼或配置中是否有錯誤。 我在Mac上使用MAMP作為帶有php 5.2.17的本地服務器軟件
每個php文件首先都包含layout_top.php。 layout_top每次都會啟動會話:
<?php
session_start();
?>
還有一個這樣的登錄站點:
<?php
include 'layout_top.php';
include 'db_connect.php';
$username = $_POST["li_username"];
$pw = $_POST["li_passwort"];
if ($username == "" || $pw == "") {
echo "Fülle bitte alle Felder aus!";
}
else {
$check_pw_query = mysql_query("SELECT password FROM user WHERE username = '$username'") or die(mysql_error());
$check_pw = mysql_fetch_assoc($check_pw_query);
if ($pw == $check_pw{'password'}) {
echo "Juhu! </br>";
$first_name_query = mysql_query("SELECT first_name FROM user WHERE username = '$username'") or die(mysql_error());
$first_name = mysql_fetch_assoc($first_name_query);
$second_name_query = mysql_query("SELECT second_name FROM user WHERE username = '$username'") or die(mysql_error());
$second_name = mysql_fetch_assoc($second_name_query);
$id_query = mysql_query("SELECT id FROM user WHERE username = '$username'") or die(mysql_error());
$id = mysql_fetch_assoc($id_query);
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = $username;
}
if (!isset($_SESSION['first_name'])) {
$_SESSION['first_name'] = $first_name{'first_name'};
}
if (!isset($_SESSION['second_name'])) {
$_SESSION['second_name'] = $second_name{'second_name'};
}
if (!isset($_SESSION['id'])) {
$_SESSION['id'] = $id{'id'};
}
if (!isset($_SESSION['gender'])) {
$_SESSION['gender'] = $gender{'gender'};
}
}
}
在那里設置了會話變量..但是如果我重新加載頁面,所有會話變量都消失了。 希望你理解我的問題。 感謝您的幫助。
編輯:這是phpinfo(),也許這將有助於發現問題。
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character|4 4
session.hash_function 0 0
session.name PHPSESSID|PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /Applications/MAMP/tmp/php /Applications/MAMP/tmp/php
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0
編輯2:
Strict Standards: session_start() [function.session-start]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /Applications/MAMP/htdocs/network/layout_top.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/network/layout_top.php:1) in /Applications/MAMP/htdocs/network/layout_top.php on line 6
Strict Standards: session_start() [function.session-start]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /Applications/MAMP/htdocs/network/layout_top.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/network/layout_top.php:1) in /Applications/MAMP/htdocs/network/layout_top.php on line 6
為什么您要查詢這么多...試試這個
$query = mysql_query("SELECT * FROM user WHERE username = '$username'") or die(mysql_error());
while($row = mysql_fetch_array($first_name_query))
{
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = $row['username'];
}
if (!isset($_SESSION['first_name'])) {
$_SESSION['first_name'] = $row['first_name'];
}
if (!isset($_SESSION['second_name'])) {
$_SESSION['second_name'] = $row['second_name'];
}
if (!isset($_SESSION['id'])) {
$_SESSION['id'] = $row['id'];
}
if (!isset($_SESSION['gender'])) {
$_SESSION['gender'] = $row['gender'];
}
}
而且我不建議您在會話上保存多個數據 ..只需將userid
保存在會話變量中,因為會話變量保存在服務器上,因此會給服務器帶來負擔。
如果您的會話目錄(最像您所說的/ tmp)是不可寫的,那么它將無法保存,並且每次都必須重新生成一個新目錄。 這是驗證方法:
if ( !is_writable(session_save_path()) ) { echo 'Session save path "'.session_save_path().'" is not writable!'; }
如果不是可寫的 ,則請更改目錄權限 ...
對於時區錯誤..請查看此鏈接..您需要set default timezone
http://pl.php.net/manual/en/function.date-default-timezone-set.php
對於已發送的標頭錯誤,請檢查如何修復PHP中的“已發送 標頭 ”錯誤
所以你會完成的.. :)
如果您需要任何進一步的指導,請告訴我。
您確定實際上包含“ layout_top.php”嗎? 可能是路徑問題,如果使用include
而不是require
並禁用了error_reporting
則可能會出現此問題,因為include
的失敗將導致警告而不是錯誤,並且不會顯示警告消息。
請在session_start
之后添加一行代碼到您的layout_top.php中,例如echo "hello! session started!";
查看會話是否已開始或僅使用require
而不是include
因此在找不到文件的情況下,您的腳本將因致命錯誤而死亡。
這是一個模糊的答案,但有時可能會有所幫助。
不要通過包含layout_top.php在每個文件上使用sesison_start()
。 嘗試在第一次加載時啟動會話,然后避免再次調用session_start()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.