PHP session not being saved between pages

2019-02-25 20:38发布

问题:

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.

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.

I can confirm that:

  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

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

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>

and one called info2 with this code:

// info2.php
<?
session_start();

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

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

Many thanks in advance for any help.

回答1:

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.';


回答2:

If your session file has zero size, make sure there is still disk space available on your server. That was the problem I had.

Check disk space with df -h on a linux server.