我的会话无效。每次我重新加载页面时,它都会生成一个新的会话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
答案 0 :(得分:1)
为什么你要这么多查询...试试这个
$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
并且标头已发送错误请检查 How to fix "Headers already sent" error in PHP
所以你将完成.. :))
如果您需要任何进一步的指导,请告诉我。
答案 1 :(得分:0)
你确定包含“layout_top.php”吗?可能是路径问题,如果您使用include
代替require
并且您已禁用error_reporting
,那么此问题可能会发生,因为包含失败将导致警告,而不是错误,并且它不会显示警告信息。
请在>> <{strong> session_start
之后>为您的layout_top.php添加一行,以查看会话是否已开始或仅使用echo "hello! session started!";
代替require
因此,如果找不到该文件,您的脚本将因致命错误而死亡。
答案 2 :(得分:0)
这是一种模糊的答案,但有时可能会有所帮助。
不要在包含layout_top.php的每个文件上使用sesison_start()
。尝试在第一次加载时启动会话,然后再避免再次调用session_start()
。