Session issues in Homestead 4.2

丶灬走出姿态 提交于 2019-12-25 01:43:06

问题


I am using Laravel Homestead for my php development and working on a project that uses Sessions extensively.

I've noticed that sessions do not persist at all. How can I fix this?

Example:

here's Index.php

    <?php

session_start();
$_SESSION['variable'] = 'This Variable';
?>
<html>
<body>
<h1>
<?php
echo $_SESSION['variable'];

?>
</h1>

</body>
</html>

Here's sessionCheck.php

<html>
<body>
<h1>
    <?php
    session_start();
    print_r($_SESSION);

    ?>
</h1>

</body>
</html>

Echoes empty array.


回答1:


Php sessions path did not have the right permissions, hence the the issue.

For homestead, ssh and set the correct permissions as:

sudo chmod 777 /var/lib/php/sessions 

To check your sessions path, use phpinfo();




回答2:


Always put session_start at top of line. See the below example

<?php
session_start();
        $_SESSION['variable'] = 'This Variable';
        ?>
        <html>
        <body>
        <h1>
        <?php
        echo $_SESSION['variable'];

        ?>
        </h1>

        </body>
    </html>

2nd part

<?php
session_start();
print_r($_SESSION);

?>


来源:https://stackoverflow.com/questions/36481865/session-issues-in-homestead-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!