PHP会话数据未存储
我正在创建一个登录系统,但是当用户登录时,它实际上并没有存储我想要在会话中存储的任何数据.我什至检查了会话的文件,它是空的.我有session_start();在所有页面上.还有什么我可能做错了.这是两个主页的代码.
I'm making a login system, but when a user logs in, it doesn't actually store any of the data i want it to in the session. I even checked the session's file, and it was empty. I have session_start(); on all the pages. what else could i be doing wrong. Heres the code for the two main pages.
登录代码:
<?
if ($DEBUG == true) {
error_reporting(E_ALL);
}
require "header.php";
require_once "dbinterface.php";
require_once "user.class.php";
require_once "config.inc.php";
$db = new db($DB['host'], $DB['user'], $DB['pass'], $DB['database']);
$u_result = $db->run("select user_id from users where user_name = '" . $db->escape($_POST['user_name']) . "'");
if ($u_result == false) {
$url = 'Location: error.php?id=8';
header($url);
}
if (count($u_result) < 1) {
$url = 'Location: error.php?id=3';
header($url);
}
$user = new user($u_result[0]['user_id']);
if ($user->match_password($_POST['pass']) == true) {
$_SESSION['authenticated'] = true;
$_SESSION['user_id'] = $u_result[0]['user_id'];
$_SESSION['user'] = $user;
} else {
$url = 'Location: error.php?id=4';
header($url);
}
session_write_close();
header('Location: index.php');
?>
每个页面包含的标题:
<?php
if (!session_start()) {
$url = "Location: error.php?id=13";
header($url);
}
?>
一些背景:
- windows 7(也在Windows上尝试过
- 服务器2008,但当前使用7)PHP
- 存在5个本地托管问题
- 所有人都存在问题
- 浏览器
以下是一些建议(我真的不知道发生了什么和/或为什么;所以它们只是建议;也许一个人可以解决问题^^).
首先,有两个问题:
(至少这些建议中的任何一项都不能解决问题)
First of all, a couple of questions :
(They matter at least if none of these suggestion does the trick)
- 您正在使用哪个版本的PHP/Apache?
- 您在Windows上吗? Linux吗?
- 如果您在生产"服务器上,那么您正在使用什么托管服务?也许有一些特别的地方吗?
- 每个人都存在问题吗?
- 浏览网站时总是有问题吗?
- 当您从另一个浏览器访问该网站时,它仍然存在吗?
- 从另一台计算机怎么样?
- Which version of PHP / Apache are you using ?
- Are you on Windows ? Linux ?
- If you are on your "production" server, what hosting service are you using ? Maybe there's something special about it ?
- Is the problem present for every one ?
- Is there always a problem when you are browsing the site ?
- Is it still present when you are accessing the site from another browser ?
- What about from another computer ?
第一个想法:如果您设置一些标头以禁用浏览器的缓存该怎么办?
像这样的东西:First idea : what if you set some header to disable caching by the browser ?
Stuff like this, for instance :session_start(); header("Cache-control: private");
第二个想法(至少在Windows上是这样):您是否尝试禁用防病毒/防火墙功能?
会话cookie是否在客户端的浏览器中正确创建?
如果您正在使用子域(或不使用子域):cookie的域可以吗?那它的到期日呢?
Second idea (at least if you are on windows) : did you try disabling you antivirus / firewall ?
Is the session cookie correctly created in the client's browser ?
If you are using sub-domains (or not) : is the cookie's domain OK ? What about it's expiration date ?
第三个想法:
Third idea :- 您说
error_reporting
设置为E_ALL
,这很好 -
display_errors
怎么样?是否将其设置为开"以便显示错误? - PHP/Apache的
error_log
中有什么有趣的东西吗?
- you said
error_reporting
is set toE_ALL
, which is nice - what about
display_errors
? Is it set to On so that errors get displayed ? - Is there anything interesting in PHP/Apache's
error_log
?
另一个:您确定在session_start
之前绝对没有任何东西到达输出吗?甚至没有空格?
Another one : Are you sure there is absolutly nothing that gets to the output before thesession_start
? Not even white spaces ?
还有一个:您确定目录/文件的权限吗?
Yet another one : Are you sure about permissions on the directories / files ?- 具有写入目录的权限,意味着您可以创建新文件和/或删除旧文件.
- 但是,如果我没记错的话,就不能修改它们
- Permission to write in a directory means you can create new files, and/or delete old ones.
- But, if I remember correctly, not that you can modify them
- 实际上,您的网络服务器需要对这些文件的写入权限^^
会话目录以及所创建的(空)文件具有什么权限?
What are the permissions on the session's directory, and on the (empty) files that get created ?
我开始用尽主意了……运气好的话,也许其中一种会是正确的……或者可以帮助您找出正确的想法!
I'm beginning to run out of ideas... With a bit of luck, maybe one of those will be the right one... Or help you find out what the right one would be !祝你好运!