PHP 会话不保存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8976930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Session not Saving
提问by Samir
I have this written at the very first line on every page of my website.
我在我网站的每一页的第一行写了这个。
include("restd.php");
and restd.php contains the following lines :
和 restd.php 包含以下几行:
@session_start();
if(isset($_SESSION['id']))
{
}
else
{
header("location:index.php");
}
The problem i'm facing is that when ever i click or do something on my website. it logs me out and takes me to index.php.
我面临的问题是,当我在我的网站上单击或执行某些操作时。它让我退出并带我到 index.php。
im sure its something to do with the session. ive tried every single thing to avoid this problem but i ahve used restd.php because i dont want anyone to copy the url of someone and paste and get into the website.
我确定它与会议有关。我尝试了每件事来避免这个问题,但我已经使用了restd.php,因为我不希望任何人复制某人的网址并粘贴并进入网站。
anyone who is logged in only can view other's pages. if they arent logged in then they'll be redirected to index.php
任何登录的人都可以查看其他人的页面。如果他们没有登录,那么他们将被重定向到 index.php
EDIT : and guys a confusing thing is that all this is working fine on my testing server which is easyPHP-5.3.8.0 but this problem is coming up when i upload all the files to my server.
编辑:伙计们一个令人困惑的事情是,这一切在我的测试服务器上运行良好,这是 easyPHP-5.3.8.0,但是当我将所有文件上传到我的服务器时,这个问题就出现了。
回答by powtac
Your session directory (probably /tmp/
) is not writable.
您的会话目录(可能/tmp/
)不可写。
Check with session_save_path()
if it is writable.
检查session_save_path()
它是否可写。
if (!is_writable(session_save_path())) {
echo 'Session path "'.session_save_path().'" is not writable for PHP!';
}
回答by Mathieu Dumoulin
Do you actually set $_SESSION['id'] on a page...
你真的在页面上设置了 $_SESSION['id'] 吗...
What you are trying to do here is:
您在这里尝试做的是:
- Start a session and load the $_SESSION from the session handler
- Check if $_SESSION contains key 'id'
- Redirect to index.php if $_SESSION['id'] is not set
- 启动会话并从会话处理程序加载 $_SESSION
- 检查 $_SESSION 是否包含键 'id'
- 如果 $_SESSION['id'] 未设置,则重定向到 index.php
Do you actually do this in index.php?
你真的在 index.php 中这样做了吗?
session_start();
$_SESSION['id'] = something;
回答by Zulkhaery Basrul
you need declare $_SESSION['id']
:
你需要声明$_SESSION['id']
:
file1.php
文件1.php
session_start();
$_SESSION['id'] = '123'
file2.php
文件2.php
include 'file1.php'
if(isset($_SESSION['id']))
{
}
else
{
header("location:index.php");
}
回答by Dee Ell
I know this is an old thread, but the following helped me with the same problem after hours of despair. Found on: http://php.net/manual/de/function.session-save-path.php
我知道这是一个旧线程,但是经过数小时的绝望之后,以下内容帮助我解决了同样的问题。发现于:http: //php.net/manual/de/function.session-save-path.php
I made a folder next to the public html folder and placed these lines at the very first point in index.php
Location of session folder:
/domains/account/session
location of index.php
/domains/account/public_html/index.php
What I placed in index.php at line 0:
我在公共 html 文件夹旁边创建了一个文件夹,并将这些行放在 index.php 的第一点
会话文件夹的位置:
/域/帐户/会话
index.php 的位置
/domains/account/public_html/index.php
我在 index.php 第 0 行放置的内容:
<?php
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
session_start();
?>
Hopefully this will save you time.
希望这会节省您的时间。
回答by Hamid
Check maybe your session path does not exist so you can save PHP session path using:
检查您的会话路径是否不存在,以便您可以使用以下方法保存 PHP 会话路径:
ini_set(' session.save_path','SOME WRITABLE PATH');
回答by Michael Bolli
In my case I forgot that I had the PHP flag session.cookie_secure
set to on
, while the development environment was not TLS-secured.
就我而言,我忘记了我将 PHP 标志session.cookie_secure
设置为on
,而开发环境未受 TLS 保护。
回答by Sylverdrag
Couple things:
夫妇的事情:
your include file doesn't have the
<?php ?>
tags, so the content will not be evaluated as PHPSession_start must be called before you start outputting anything. Is that the case?
您的包含文件没有
<?php ?>
标签,因此内容不会被评估为 PHP在开始输出任何内容之前,必须调用 Session_start。是这样吗?
回答by David Nguyen
You still don't even answer where you SET $_SESSION['id']. $pid = $_SESSION['id']
does not set the session variable. session_start()
comes before ANYTHING session related, it's not shown before your include.
你仍然没有回答你在哪里设置 $_SESSION['id']。$pid = $_SESSION['id']
不设置会话变量。session_start()
出现在任何与会话相关的之前,它不会在您的包含之前显示。
回答by Zyox
I had the same problem and found a work-around for it. If anybody can explain why the session is not read even when the cookie is there, please let me know.
我遇到了同样的问题,并找到了解决方法。如果有人可以解释为什么即使有 cookie 也无法读取会话,请告诉我。
<?php
// logged.php
// The PHP session system will figure out whether to use cookies or URLs to pass the SID
if(!isset($_COOKIE['PHPSESSID']) && !isset($_GET['PHPSESSID']) && authenticationRoutine(/* Returns true if succesfully authenticated */) ) {
session_id(uniqid("User--"));
session_start();
$_SESSION['id']=session_id();
}
?>
<?php
// Insecure restd.php (The user can forge a stolen SID cookie or URL GET request, but that is inherent with PHP sessions)
if(!isset($_COOKIE['PHPSESSID']) && !isset($_GET['PHPSESSID']) {header('Location: index.php')}
?>
.
.
[EDIT]
[编辑]
Even though the cookie was there and I prevented starting a new session, the session had not been read and started, so no session variables were available. In this case I check if the session has been started first (not using session_status()
because it doesn't exist in PHP 3.5, which for some reason is the most widespread among hosts). If no session has been started within PHP, I check if it had been started before by testing the cookies and GET variables. If a session ID was found, the script resumes the session with that ID. If no ID is available, the user gets redirected to the index.
即使 cookie 存在并且我阻止了启动新会话,但该会话还没有被读取和启动,因此没有可用的会话变量。在这种情况下,我检查会话是否已首先启动(不使用,session_status()
因为它在 PHP 3.5 中不存在,出于某种原因,它在主机中最为普遍)。如果在 PHP 中没有启动会话,我会通过测试 cookie 和 GET 变量来检查它之前是否已启动。如果找到会话 ID,脚本将使用该 ID 恢复会话。如果没有可用的 ID,用户将被重定向到索引。
<?php
// restd.php
if(empty(session_id())) {
if(isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) {session_id($_COOKIE['PHPSESSID']);}
elseif(isset($_GET['PHPSESSID']) && !empty($_GET['PHPSESSID'])) {session_id($_GET['PHPSESSID']);}
else {header('Location: index.php'); exit(0);}
session_start();
}