不会在页面之间保存PHP会话 [英] PHP session not being saved between pages

查看:101
本文介绍了不会在页面之间保存PHP会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚购买了一些新的服务器空间,并正在迁移一个非常简单的PHP应用程序,它在我的其他服务器上正常工作。

I've just bought some new server space and am migrating a very simple PHP app to it that works fine on my other server.

由于某种原因,会话数据未存储在新服务器上的页面之间的$ _SESSION变量中。我查看了这个类似的SO文章和许多其他人尝试解决问题。

For some reason the session data is not being stored in the $_SESSION variable between pages on the new server. I have looked at this similar SO article and many others to try and fix the issue.

我可以确认:


  1. 东西在我的其他网站工作正常。我已经查看了由session.save_path定义的目录,并且有一些会话文件(在OLD服务器上)。

  2. 我已经回复了$ _SESSION变量的内容

  3. 我为每个页面都echo_d session_id(),并获得相同的输出。
  4. >
  5. 我看看在新服务器的session.save_path文件夹(/ usr / lib / php / session)指定的文件夹中,并且没有任何文件。我已经检查了权限,它设置在drwxrwx ---,这应该意味着php程序可以写它,对吗?

  6. 我没有.htaccess文件。

  7. 我正在运行CentOS 5.5

  1. The session stuff is working fine on my other site. I have looked into the directory defined by session.save_path and there are loads of session files in there (on the OLD server).
  2. I have echoed the contents of the $_SESSION variable at the end of a page, and it is holding the correct data, it's just not being saved.
  3. I have echo'd session_id() for every page and get the same output.
  4. I have had a look in the folder specified by the NEW server's session.save_path folder (/usr/lib/php/session) and there aren't any files in there. I have checked the permissions and it's set at drwxrwx---, which should mean that the php program can write to it, right?
  5. I have no .htaccess file.
  6. I am running CentOS 5.5

这是来自phpinfo()输出的相关部分: p>

This is from the relevant part of the phpinfo() output:

Session Support     enabled
Registered save handlers    files user
Registered serializer handlers  php php_binary wddx

Directive   Local Value Master Value
session.auto_start  Off Off
session.bug_compat_42   Off Off
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_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  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character 5   5
session.hash_function   0   0
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    files   files
session.save_path   /var/lib/php/session    /var/lib/php/session
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    Off Off
session.use_trans_sid   0   0

我联系了托管公司,他们不会调查,因为他们说这是一个软件问题,它不是一个管理帐户。 Grrrrr ...

I've contacted the hosting company and they won't look into it because they say it's a software issue and it's not a managed account. Grrrrr...

只是为了看到一些不起作用的代码,我已经从我链接到的文章中偷了代码:

Just so you can see some code that isn't working, I have stolen code from the article I have linked to:

// info.php
<?
session_start();

$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info2.php">info 2</a>

和一个名为info2的代码:

and one called info2 with this code:

// info2.php
<?
session_start();

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info.php">info</a>

结果是在info.php中打印数组数据,但info2.php中没有输出

result is printing the array data in info.php, but no output in info2.php (apart from the link of course).

推荐答案

在脚本结束之前尝试显式关闭会话;您可能会看到一些错误讯息。

Try explicitly closing the session before the script ends; maybe you see some error message then.

<?php
error_reporting(E_ALL); ini_set('display_errors', true);
session_start();
echo '<a href="?', time(), '">refresh</a>', "\n";

echo '<pre>';
echo 'session id: ', session_id(), "\n";

$sessionfile = ini_get('session.save_path') . '/' . 'sess_'.session_id();
echo 'session file: ', $sessionfile, ' ';
if ( file_exists($sessionfile) ) {
    echo 'size: ', filesize($sessionfile), "\n";
    echo '# ', file_get_contents($sessionfile), ' #';
}
else {
    echo ' does not exist';
}

var_dump($_SESSION);
echo "</pre>\n";

$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";

session_write_close();
echo 'done.';

这篇关于不会在页面之间保存PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆