0

i have a problem calling a session variable from another script. can anybody help me on this matter.

Below is the script that i create the session and store the time in a session variable.

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

Here is the script that i try to access this session variable from a function of it. till now nothing worked

<?php

include '../../mydomain/myscript.php';

class survey_Tracksciprt
{
    public static function timeofclick(){
    session_start();
    $time_org = $_SESSION['orgtimestamp'];
    echo $time_org;
    }
}

this hasnt worked upto now, nothing prints...can anybody give tips to sought this out and its compulsory to have this function timeofclick

3
  • session variables are browser specifics, so any php file should be able to access them. Did you check if it is setting properly in your first script. And yes, as Stjin pointed, you are not calling that function anywhere in your second script. Commented Sep 11, 2014 at 8:31
  • put session_start() in this file too at first line Commented Sep 11, 2014 at 8:31
  • Calling session_start twice is not a good idea Commented Sep 11, 2014 at 8:32

3 Answers 3

2

You're not creating your class on your second file 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;
    }
}

I also advice putting session_start at the top of your file.

Sign up to request clarification or add additional context in comments.

1 Comment

survey_Tracksciprt::timeofclick(); as it is static
1
    <?php

    session_start();

    include '../../mydomain/myscript.php';

    class survey_Tracksciprt
    {
        public static function timeofclick(){
        $time_org = $_SESSION['orgtimestamp'];
        echo $time_org;
        }
    }

Always use the session_start(); at the top line of the page.

Comments

1

First, you need an init-session.php file, containing:

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

Second, you need to include this file at the start of your loader/layout (whatever you have there), so no operation will be executed before you initialize your session.

Third, you should initialize $orgtimestamp like this:

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

Fourth, you need to call survey_Tracksciprt::timeofclick().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.