4

Just a simple function in native php

protected function some_function(){
    session_start();
    if(!isset($_SESSION['a']))
    {
        $_SESSION['a'] = 'some value';
        return true;
    } else {
        return $_SESSION['a'];
    }
 }

on the 1st run, it will return bool(true) and then "some value" as expected.

Applying the same to laravel session,

protected function some_function(){ 
    $a = Session::get('abc');
    if(is_null($a)){
        Session::put('abc', 'some value');
        //return Session::get('abc');
        return true;
    } else {
        return $a;
    }
 }

By logic, on the 1st run, $a will be null. and then puts "some value" in abc key and returns bool(true) as expected.

However on consequent access, the Session::get('abc') returns null every time. so every attempt returns bool(true).

I assumed may be session wasn't writing properly so i checked with getting the key value right after setting it.

by commenting out the line in the above code, it returns whatever value i put in Session::put('abc'). So there is no error in writing the data.

Problem here is trying to retrieve the value always returns null though i have set after the 1st request.

Am i missing something here?


EDIT

Using database as the driver, The sessions does not get save in database. No error is outputted. Database implementation has been correct with sessions table schema as described in docs.

itachi
  • 6,323
  • 3
  • 30
  • 40
  • Have you setup your session configuration correctly? For example, is the application key set? http://laravel.com/docs/session/config – Niklas Modess Dec 07 '12 at 10:10
  • @nerdklers I am using the `file` driver as of now. application key is set. – itachi Dec 07 '12 at 10:12
  • are you still having this issue, because I am having similar issues – mattl Feb 14 '13 at 10:21
  • @mattl No. I couldn't resolved it and had to make my own library. But the newest version doesn't have it and working pretty well. One thing to consider. While putting something in session, do not exit prematurely. Suppose if i put something in session and call `dd()`, it won't get save. If you indeed need to save, you need to call `Session::save()` manuely after putting the value but before exiting. Let me know if it was hard to follow. – itachi Feb 14 '13 at 10:38
  • I've also found situations where using a default response with the `Session::get()` method has replaced other put methods that were acting up. – mattl Feb 14 '13 at 10:44

2 Answers2

0

Try this snippet. Untested, but pretty sure it works.

if(Session::has('abc'))
{
    return Session::get('abc');
} else {
    Session::put('abc', 'some value');
    return true;        
}
aebersold
  • 11,286
  • 2
  • 20
  • 29
  • 2
    Yes. This will work as `Session::has` will return `false` everytime for the key. But that will mean, i have to write the value each time i need to retrieve it. Defeats the purpose of a session. isn't it? – itachi Dec 07 '12 at 10:21
  • `Session::has('abc')` should return true, if a value for that key is stored (and therefore use the stored session-var). – aebersold Dec 07 '12 at 10:25
  • Exactly what i expected to return true when i was implementing it. But on practice, it is returning false. Hence the key gets default `null`. and to make it simple, there is no chance of destructing it in other places as it is the only function which uses session. – itachi Dec 07 '12 at 10:27
  • I've simplified the snippet a bit more. This should definetly work, otherwise something with the laravel session config is wrong. Also make sure, that the 'some value'-string is not empty. – aebersold Dec 07 '12 at 10:45
  • Yes. it works. But the problem here is, `session::has` returning false even though it was set. Session should be written once and then retrieve it by calling it. Not writing everytime before calling it. It is behavin similiar to a local variable now and not that of a session var. – itachi Dec 07 '12 at 10:55
  • I now what you want to achive. If `session::has` returns false, laravel is not able to store the session properly. If you use the file driver, please make sure storage/sessions is writeable and the application-key is set. – aebersold Dec 07 '12 at 10:58
  • I changed the driver to database and yes, as expected, it isn't storing any values hence acting as local variables.... *sigh* thanks for the help. I guess i will need to open up the whole driver now. – itachi Dec 07 '12 at 11:12
  • Yes. Cookie, file and database. All have same issue. Right now, i wrote a custom session handler and saving sessions into database but i am looking into why it isn't working. Atleast database should have work as credentials are perfectly fine. So there is some failed logic going on inside the basic level. – itachi Dec 08 '12 at 03:54
0

After going through many forum to solve same issue, I solved it by setting my session driver to database.

    'driver' => 'database',

and created table in database

 CREATE TABLE `sessions` (
  `id` varchar(40) NOT NULL,
  `last_activity` int(10) NOT NULL,
  `data` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Invincible
  • 1,418
  • 18
  • 23