-1

I am currently building a small script for cron jobs, i am testing everything in the terminal (Linux) and i cant figure out why sessions are not working properly.

session_start()
$_SESSION['cron'] = 'test';
echo $_SESSION['cron'];
Zoric
  • 61
  • 12
  • 2
    have you started the session? (`session_start()`) – treyBake Apr 06 '18 at 12:24
  • did you start the session and does that array contain value in all other pages using sessions? – Funk Forty Niner Apr 06 '18 at 12:24
  • 1
    I just don't understand using sessions with cron though. Does that actually work? – Funk Forty Niner Apr 06 '18 at 12:24
  • i have session_start before everything, i need sessions to set old timestamp for each job – Zoric Apr 06 '18 at 12:26
  • 2
    Session would not work from terminal , it works from browsers. However you can do like [this solution](https://stackoverflow.com/a/7578766/2815635) – Niklesh Raut Apr 06 '18 at 12:27
  • check your logs then update your post to contain what those errors were, if any. Make sure that your system is setup to catch and log errors. – Funk Forty Niner Apr 06 '18 at 12:29
  • check once cron job is started or not, use this (crontab -e) command to your terminal – parthi Apr 06 '18 at 12:33
  • Sessions are a way of identifying the same visitor using a web browser to make multiple requests to a website. It's not clear how this would relate to the scenario of running under a terminal or a cron job, because there is no browser, and no visitor to track. I therefore think this is [an X/Y Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): you need to store some information between cron jobs, and sessions are the wrong tool for that job. – IMSoP Apr 06 '18 at 12:33
  • @IMSoP yes i agree this is the wrong way to store it but the only way i can think that would work is to write a temp file with the data i need – Zoric Apr 06 '18 at 12:40
  • @Zoric Yes, a file, or an SQL database, or a NoSQL database, or an in-memory key-value store, or literally anything where you can read and write some data. Note that that's all a session does anyway: by default, each session is a file on disk, and you can set up session storage handlers for pretty much anything; the session part is entirely about keeping track of a visitor to a website, which is irrelevant to a cron job. Sessions gain you nothing here. – IMSoP Apr 06 '18 at 12:52

2 Answers2

0

This is my solution

private function get($key){
    $content = @file_get_contents( __DIR__ . '/../../cron/data.json' );
    if ($content) {
        $content = json_decode($content, true);
        if ( is_array($content)) {
            return $content[$key];
        }else{
            return false;
        }           
    }else{
        return false;
    }
}

private function request($key){
    $content = @file_get_contents( __DIR__ . '/../../cron/data.json' );
    if ($content) {

        $content = json_decode($content, true);
        if ( is_array($content)) {
            $content[$key] = time();
            file_put_contents( __DIR__ . '/../../cron/data.json' , json_encode($content) );
        }else{
            return false;
        }

    }else{
        file_put_contents( __DIR__ . '/../../cron/data.json' , json_encode(['demo' => time()]) );
    }
}
Zoric
  • 61
  • 12
-3

If I remember PHP correctly, you need to initialize sessions first :

session_start();

See the doc.

Mickaël Bénès
  • 147
  • 1
  • 2
  • 7