PHP _Session 变量不再持久化 [英] PHP _Session variable no longer persistate

查看:42
本文介绍了PHP _Session 变量不再持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的代码在过去 3 个月内一直有效.现在突然间,我注意到我的 PHP _Session 变量不再存在于我登录的不同页面之间.请注意,该代码实际上适用于本地,但不适用于我的服务器(papahost).

I have wrote a code that has been working for the last past 3 month. Now suddenly, I noticed that my PHP _Session variable no longer persists between the different page for my login. Please note that the code actually works on local, but not on my server (papahost).

这是我正在尝试做的一个非常简单的版本(会话从 A 类持续到 B 类):

class function.php

Here's a very simple version of what i'm trying to do (SESSION persists from class A to B):

class function.php

<?php 
function sec_session_start()
{
        //other code goes here 
        session_name('sec_session_id');
        $secure = SECURE;

        // This stops JavaScript being able to access the session id.
        $httponly = true;

        if (ini_set('session.use_only_cookies', 1) === FALSE) 
        {
           header("Location: ../error.php?err=8");
           session (ini_set)
           exit();
        }
       $cookieParams = session_get_cookie_params();
       session_set_cookie_params($cookieParams["lifetime"], 
       $cookieParams["path"], 
       $cookieParams["domain"], $secure, $httponly);
       //end of other code
    session_start();
}
?>



class a.php

<?php 
include_once 'function.php';
sec_session_start();
$_SESSION["test"] = "this is a test";
$url = "b.php";
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; 
exit;
?>



类 b.php

<?php 
include_once 'includes/functions.php';
sec_session_start();
echo($_SESSION["test"]); 
?>

class b.php 的预期输出是这是一个测试",但我的服务器上的实际输出是",因为由于未知原因,会话数据不是持久的.正如我所说,它在本地完美运行.任何人都知道可能是什么问题?是 php.it 的问题还是我遗漏的 CPanel 属性?

The expected output for class b.php is "this is a test" but the actual output is "" on my server since for an unknown reason, the session data is not persistant. As I said it works perfectly on local. Anyone has a clue what could be the problem? Could it be a problem from php.it or a CPanel property I missed?

我注意到在 function.php 中执行 session_start() 行之后没有创建 PHPSESSID.

I noticed that the PHPSESSID is not created after the line session_start() is executed in function.php.

感谢您的帮助!

推荐答案

Bring session_start() over $secure = SECURE; in sec_session_start()代码>.您不应在 session_start() 之前定义和分配任何变量.

Bring session_start() over $secure = SECURE; in sec_session_start(). You should not define and assign any vars before session_start().

实际上尝试将其逐步提升到更高级别,并以某种方式与其他会话功能结合使用.在它之前不要使用标题.

Actually try to bring it step by step to upper levels and to somehow juggle in conjunction with the other session functions. Use no headers before it.

请参阅此处(第 1 点) 第二行 session_start() 引起的大问题,解决很简单.

See here (point 1) the big problem caused by session_start() on second line, resolved very simple.

这篇关于PHP _Session 变量不再持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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