当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel 5.4.36 session 生效问题

Laravel 5.4.36 session 生效问题

2018年02月10日  | 移动技术网IT编程  | 我要评论

在测试过程中发现 如果方法有echo 等函数输出到PHP的输出缓存中 存在  sessionID 不会放到http的请求头中  下次请求也就拿不到sessionid问题  

问题的原因

代码位置:public/index.php  

$response->send();

该方法代用方法   代码:vendor/symfony/http-foundation/Response.php

    /**
     * Sends HTTP headers.
     *
     * @return $this
     */
    public function sendHeaders()
    {
        // headers have already been sent by the developer
        if (headers_sent()) {
            return $this;
        }

        // headers
        foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
            foreach ($values as $value) {
                header($name.': '.$value, false, $this->statusCode);
            }
        }

        // status
        header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);

        // cookies
        foreach ($this->headers->getCookies() as $cookie) {
            if ($cookie->isRaw()) {
                setrawcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
            } else {
                setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
            }
        }

        return $this;
    }

以前的原因出现在  headers_sent() 中  

有兴趣的同学可以测试一下 如果输出缓存存在数据 (在方法使用echo 之类的函数有打印行为)  headers_sent() 函数则返回ture  

这样就解释了 在方法中存在打印的函数时候  你的session始终没有生效问题

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网