PHP会话文件为空

时间:2014-06-05 16:51:57

标签: php

我有启用会话的PHP代码。会话文件正在tmp目录中创建,但它是空的,这就是为什么我的浏览器Internet Explorer和Chrome无法接收会话并在页面中显示它。

这是我的PHP代码。

第2页。

<html>
<body>
<form action="test3.php" method="post">
Username: <br><input type="text" name="username"></br>
<input type="submit" name = 'submit1' value= 'Login'>
</form>
</body>
</html>

第3页。

<?php
session_start();
?>

<html>
<body>

<?php
$username = $_POST['username'];
$_SESSION['username']= $_POST['username'];
echo "<br> Hi $username.</br>";
?>

<form action="test4.php" method="post">
<input type="submit" name = 'submit' value= 'click me'>
</form>

</body>
</html>

第4页。

<?php
session_start();
$username = $_SESSION['username'];
echo "<br> Hi $username.</br>";
?>

2 个答案:

答案 0 :(得分:2)

检查服务器session.save_path文件中的php.ini

您可以通过查看phpinfo()输出来查看现在设置的内容。

<?php

phpinfo();

?>

查找看起来像这样的session块;我的MAMP设置截图:

enter image description here

然后在你的php.ini中应该是这样的一大块配置:

; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this 
; variable in order to use PHP's session functions.
; As of PHP 4.0.1, you can define the path as:
;     session.save_path = "N;/path"
; where N is an integer.  Instead of storing all the session files in 
; /path, what this will do is use subdirectories N-levels deep, and 
; store the session data in those directories.  This is useful if you 
; or your OS have problems with lots of files in one directory, and is 
; a more efficient layout for servers that handle lots of sessions.
; NOTE 1: PHP will not create this directory structure automatically.
;         You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
;         use subdirectories for session storage
;

session.save_path = /Applications/MAMP/tmp/php

确保您的系统上实际存在路径&amp; Apache服务器可以写入它。如果目录不存在或无法写入,请根据需要进行调整以匹配您的设置。

我发现this site有很多关于如何处理这类问题的好信息。

答案 1 :(得分:1)

问题已解决我必须将session.cookie_path中的php.ini改为/而不是/tmp