Home > Backend Development > PHP Tutorial > The Tp framework cannot obtain the session. Please give me the answer.

The Tp framework cannot obtain the session. Please give me the answer.

WBOY
Release: 2016-08-04 09:20:42
Original
2977 people have browsed it

I am using the new version of Tp 3.2.3. I have multiple methods in the same controller. I wrote a session in method 1. Why can't I read it in method 2? Please explain...

The code is very simple. I just wrote a controller and inherited Controller. Then there are 2 methods written in it, the first method writes a session. The second method reads the session. The syntax is session("kkk",999); The other is to directly echo session("kkk"); The result is that the session cannot be read out.

Reply content:

I am using the new version of Tp 3.2.3. I have multiple methods in the same controller. I wrote a session in method 1. Why can't I read it in method 2? Please explain...

The code is very simple. I just wrote a controller and inherited Controller. Then there are 2 methods written in it, the first method writes a session. The second method reads the session. The syntax is session("kkk",999); The other is to directly echo session("kkk"); The result is that the session cannot be read out.

ThinkPHPWhen the application is initialized, in the ThinkPHP\Library\Think\App.class.php class, if the current running environment is not cli, it will automatically register session_start from the configuration file,

The SESSION_AUTO_START set in the default configuration file ThinkPHP\Conf\convention.php is TRUE, that is, the session will be automatically enabled by default. Please check whether your configuration file covers this configuration item, thinkphp When loading the configuration file, user-defined ones will overwrite the default ones,

The default user-defined configuration file Application\Common\Conf\config.php, check whether SESSION_AUTO_START is set to false or 0

If you don’t want to modify the configuration file, you only need to call session_start before starting the method

<code class="php">
<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {

    public function index(){
        $this->show('.........');
    }

    public function test()
    {
        //若配置文件SESSION_AUTO_START为false, 则调用session方法时都学要手动开启session
        session_start();

        session('xxxxx', 'wodelaojia');
    }


    public function test2()
    {
        session_start();
        
        echo session('xxxxx');
    }
}</code>
Copy after login

Configure SESSION_AUTO_START to TRUE or when using session session_start

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template