问题
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