簡體   English   中英

PHP會話文件為空

[英]PHP session files are empty

我有啟用會話的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>";
?>

檢查服務器的php.ini文件中的session.save_path

您可以通過檢查phpinfo()輸出來查看它現在設置的內容。

<?php

phpinfo();

?>

尋找session看起來像這樣的session塊; 我的MAMP設置截圖:

在此輸入圖像描述

然后在你的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

確保您的系統上實際存在路徑,並且Apache服務器可以寫入該路徑。 如果目錄不存在或無法寫入,請根據需要進行調整以匹配您的設置。

我發現這個網站有很多關於如何處理這類問題的好信息。

問題解決了我不得不將php.ini session.cookie_path更改為/ instead /tmp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM