简体   繁体   中英

Sessions don't work in PHP

This is my test script:

<?php
ob_start(); // with or without this is the same
$session = session_start();
$path = session_save_path();
if (headers_sent()) die ("Headers have already been sent");

if((isset($_GET["set"]))) {
    $_SESSION["var"] = " - testing - ";
}
echo $_SESSION["var"];

echo $session;
echo $path;

print_r($_SESSION);
print_r($_COOKIE);
setcookie("cookie", "value");

$session outputs 1 . Cookies are being set successfully. I have a PHPSESSID cookie set. Sessions (from the phpinfo) are enabled. session_start() is there.

When I set the session variable (by setting the set GET parameter), then the session variable successfully echoes as well directly from the $_SESSION array. But when I remove the set GET parameter, print_r-ing the $_SESSION outputs an empty array.

I've even gone so far as to set chmod 777 on /var/lib/php5 , and I chown -ed it to the same user that runs PHP. I even tried a completely different domain name, by defining it in my local /etc/hosts and setting up a new virtual host with it.

I found one answer on Stackoverflow that said to disable register_globals in php.ini , but I don't even have that setting in my phpinfo output, so that I didn't do...

I've tried changing the session directory with session_save_path() , but that DIDN'T WORK. I might not have privileges for that on this (VPS) hosting.

I don't know what else to try?

This is the session section of my phpinfo() output, if it helps:

session
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.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly Off Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    /dev/urandom    /dev/urandom
session.entropy_length  32  32
session.gc_divisor  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  0   0
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/php5   /var/lib/php5
session.serialize_handler   php php
session.upload_progress.cleanup On  On
session.upload_progress.enabled On  On
session.upload_progress.freq    1%  1%
session.upload_progress.min_freq    1   1
session.upload_progress.name    PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix  upload_progress_    upload_progress_
session.use_cookies On  On
session.use_only_cookies    On  On
session.use_trans_sid   0   0

Solved.

It seems my whole account doesn't actually have privileges to go up from the public_html folder, so the /var/lib/php5 folder was actually trying to open it as a "relative" folder inside the public_html . Which I finally realized after I turned on error_reporting .

So after I created a public_html/var/lib/php5 in my FTP root and made it writable, the sessions started working.

I don't think this is a really conventional environment, but then again I've had much stranger things on this hosting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM