Home > Backend Development > PHP Tutorial > Laravel 5.4 session validity problem

Laravel 5.4 session validity problem

PHP中文网
Release: 2023-03-20 10:44:02
Original
1947 people have browsed it

During the testing process, it was found that if the method has echo and other functions output to the output cache of PHP, the sessionID will not be placed in the http request header, and the sessionid will not be obtained in the next request.

Problem Reason

Code location: public/index.php

$response->send();
Copy after login

This method substitute method Code: 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;
    }
Copy after login

The previous reason appeared in headers_sent()

Interested students can test if there is data in the output cache (in the method Using functions such as echo has printing behavior) The headers_sent() function returns true

This explains the problem that your session never takes effect when there is a printing function in the method

The above is the detailed content of Laravel 5.4 session validity problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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