PHP session 文件为空

标签 php

我有启用了 session 的 PHP 代码。正在 tmp 目录中创建 session 文件,但它是空的,这就是我的浏览器 Internet Explorer 和 Chrome 无法获取 session 并在页面上显示它的原因。

这是我的 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>";
?>

最佳答案

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

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

<?php

phpinfo();

?>

寻找应该看起来像这样的 session block ;我的 MAMP 设置的屏幕截图:

enter image description here

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

; 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

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

我找到了 this site还有很多关于如何处理此类问题的有用信息。

关于PHP session 文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065881/

相关文章:

php - 限制选择复选框(一次一个类别和一个子类别)

php - function_exists() 缓存它的查询吗?

php - PHP Nominatim API 每秒可以响应多少次调用?

php - 如何在php中将mysql结果转换为数组?

php - 使用 PHP 连接 MYSQL 时出错

php - 如何在 PHP 中生成 PDF/A-1a pdf?

php - 在使用 PHP 发布之前检查 MySQL 中的多个唯一字段

php - 如何让用户在登录后停留在同一页面上

php - 帮助将 javascript 函数转换为 php

php - Ajax/Internet Explorer 编码问题