从外部脚本调用php会话变量


calling a php session variable from outer script

我在从另一个脚本调用会话变量时遇到问题。有人能在这件事上帮我吗。

下面是我创建会话并将时间存储在会话变量中的脚本。

<?php
session_start();
$orgtimestamp = date("Y-m-d h:i:sa");
$_SESSION['orgtimestamp'] = $orgtimestamp;
?>

这是我试图从它的一个函数访问这个会话变量的脚本

<?php
include '../../mydomain/myscript.php';
class survey_Tracksciprt
{
    public static function timeofclick(){
    session_start();
    $time_org = $_SESSION['orgtimestamp'];
    echo $time_org;
    }
}

到目前为止还没有效果,没有任何指纹。。。有人能提供一些技巧来找到这个吗?必须让这个函数超时点击

您没有在第二个文件add:上创建类

//I don't know what this does but if it already starts a session remove the session start inside the class.
include '../../mydomain/myscript.php';
$survey = new survey_Tracksciprt();
$survey::timeofclick();
class survey_Tracksciprt
{
    public static function timeofclick(){
    session_start();
    $time_org = $_SESSION['orgtimestamp'];
    echo $time_org;
    }
}

我还建议将session_start放在文件的顶部。

    <?php
    session_start();
    include '../../mydomain/myscript.php';
    class survey_Tracksciprt
    {
        public static function timeofclick(){
        $time_org = $_SESSION['orgtimestamp'];
        echo $time_org;
        }
    }

始终使用session_start();在页面的顶行。

首先,您需要一个init-session.php文件,其中包含:

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

其次,您需要在加载程序/布局开始时包含此文件(无论您有什么),因此在初始化会话之前不会执行任何操作。

第三,您应该这样初始化$orgtimestamp

<?php
$orgtimestamp = date("Y-m-d h:i:sa");
$_SESSION['orgtimestamp'] = $orgtimestamp;
?>

第四,您需要调用survey_Tracksciprt::timeofclick()