尽管设置了它,laravel 会话仍返回 null [英] laravel session returning null inspite of setting it

查看:61
本文介绍了尽管设置了它,laravel 会话仍返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的原生 php 函数

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'];
    }
 }

在第一次运行时,它将按预期返回 bool(true) 然后 "some value".

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

将同样的方法应用到 Laravel 会话中,

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;
    }
 }

按逻辑,在第一次运行时,$a 将为 null.然后将 "some value" 放入 abc 键并按预期返回 bool(true).

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

但是在随后的访问中,Session::get('abc') 每次都返回 null. 所以每次尝试都返回 bool(真的).

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

我认为可能是 session 没有正确写入,所以我在设置后立即检查了获取键值.

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

通过注释掉上面代码中的行,它返回我在 Session::put('abc') 中输入的任何值.所以写入数据没有错误.

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.

问题 这里试图检索值总是返回 null 尽管我在第一个请求之后设置了.

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

我在这里遗漏了什么吗?

Am i missing something here?

使用数据库作为驱动程序,会话不会保存在数据库中.没有错误输出.数据库实现与文档中描述的会话表架构一致.

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.

推荐答案

经过很多论坛解决同样的问题后,我通过将会话驱动程序设置为数据库来解决它.

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

    'driver' => '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;

这篇关于尽管设置了它,laravel 会话仍返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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