PHP Session data not being saved

2019-01-02 18:09发布

I have one of those "I swear I didn't touch the server" situations. I honestly didn't touch any of the php scripts. The problem I am having is that php data is not being saved across different pages or page refreshes. I know a new session is being created correctly because I can set a session variable (e.g. $_SESSION['foo'] = "foo" and print it back out on the same page just fine. But when I try to use that same variable on another page it is not set! Is there any php functions or information I can use on my hosts server to see what is going on?

Here is an example script that does not work on my hosts' server as of right now:

<?php
session_start();
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
echo '<p><a href="page1.php">Refresh</a></p>';
?>

The 'views' variable never gets incremented after doing a page refresh. I'm thinking this is a problem on their side, but I wanted to make sure I'm not a complete idiot first.

Here is the phpinfo() for my hosts' server (PHP Version 4.4.7): alt text

标签: php session
22条回答
裙下三千臣
2楼-- · 2019-01-02 18:35

I had following problem

index.php

<?
    session_start();
    $_SESSION['a'] = 123;
    header('location:index2.php');
?>

index2.php

<?
  session_start();
  echo $_SESSION['a'];
?>

The variable $_SESSION['a'] was not set correctly. Then I have changed the index.php acordingly

<?
    session_start();
    $_SESSION['a'] = 123;
    session_write_close();
    header('location:index2.php');
?>

I dont know what this internally means, I just explain it to myself that the session variable change was not quick enough :)

查看更多
梦醉为红颜
3楼-- · 2019-01-02 18:35

I had this problem when using secure pages where I was coming from www.domain.com/auth.php that redirected to domain.com/destpage.php. I removed the www from the auth.php link and it worked. This threw me because everything worked otherwise; the session was not set when I arrived at the destination though.

查看更多
倾城一夜雪
4楼-- · 2019-01-02 18:36

Check who the group and owner are of the folder where the script runs. If the group id or user id are wrong, for example, set to root, it will cause sessions to not be saved properly.

查看更多
后来的你喜欢了谁
5楼-- · 2019-01-02 18:37

If you set a session in php5, then try to read it on a php4 page, it might not look in the correct place! Make the pages the same php version or set the session_path.

查看更多
无色无味的生活
6楼-- · 2019-01-02 18:39

Another few things I had to do (I had same problem: no sesson retention after PHP upgrade to 5.4). You many not need these, depending on what your server's php.ini contains (check phpinfio());

session.use_trans_sid=0 ; Do not add session id to URI (osc does this)
session.use_cookies=0;  ; ensure cookies are not used
session.use_only_cookies=0 ; ensure sessions are OK to use IMPORTANT
session.save_path=~/tmp/osc; ; Set to same as admin setting
session.auto_start = off; Tell PHP not to start sessions, osc code will do this

Basically, your php.ini should be set to no cookies, and session parameters must be consistent with what osc wants.

You may also need to change a few session code snippets in application_top.php - creating objects where none exist in the tep_session_is_registered(...) calls (e eg. navigation object), set $HTTP_ variables to the newer $_SERVER ones and a few other isset tests for empty objects (google for info). I ended up being able to use the original sessions.php files (includes/classes and includes/functions) with a slightly modified application_top.php to get things going again. The php.ini settings were the main problem, but this of course depends on what your server company has installed as the defaults.

查看更多
裙下三千臣
7楼-- · 2019-01-02 18:41

I know one solution I found (OSX with Apache 1 and just switched to PHP5) when I had a similar problem was that unsetting 1 specific key (ie unset($_SESSION['key']);) was causing it not to save. As soon as I didn't unset that key any more it saved. I have never seen this again, except on that server on another site, but then it was a different variable. Neither were anything special.

Thanks for this one Darryl. This helped me out. I was deleting a session variable, and for some reason it was keeping the session from committing. now i'm just setting it to null instead (which is fine for my app), and it works.

查看更多
登录 后发表回答