Home  >  Article  >  Backend Development  >  What to do if php session cannot pass value

What to do if php session cannot pass value

藏色散人
藏色散人Original
2021-09-19 10:36:232749browse

Solution to the problem that php session cannot pass value: 1. Check the parameter configuration of the server; 2. Change the value of "session.use_trans_sid" in the configuration file php.ini to 1; 3. Check "var /tmp" folder can be written and modified.

What to do if php session cannot pass value

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

What should I do if the php session cannot pass a value?

Solution to PHP session failure and not passing

In PHP, the session cannot be passed to the next page. There are generally two situations:

Let’s first write a php file: , and pass it to the server to see the server’s parameter configuration.

Go to the session section and see that the session.use_trans_sid parameter is set to zero.

This parameter specifies whether to enable transparent SID support, that is, whether the session is passed along with the URL. My personal understanding is that once this parameter is set to 0, a session will be started for each URL. In this way, subsequent pages cannot track the session of the previous page, which is what we call undeliverable. The two pages generate two session files on the server side and are not related. (I don’t know if this is correct? Please give me some advice.)

So one way is to change the value of session.use_trans_sid to 1 in the configuration file php.ini.

Of course we know that not everyone has the authority to change the configuration of PHP, so what are the indirect solutions?

Let’s use two examples to illustrate:

File 1 test1.php

//Indicates that the session is identified by the user ID
session_id(SID);
//Start the session
session_start();

//Assign the name of the session to Havi
$_SESSION['name']="Havi";
//Output the session and set a hyperlink to the second page test2.php
echo " ”.$_SESSION['name'].””;
?>

File 2: test2.php

Indicates that the session is identified by the user ID
session_id(SID);
//Start the session
session_start();

//Output the session passed in test1.php.
echo “This is “.$_SESSION['name'];
?>

Every page must write to open the session. . Otherwise it will still not work properly

So, the key point is to add session_id(SID); before session_start();, so that when the page is converted, the server uses the session file saved by the user on the server. The session in the folder solves the problem of transmission.

However, some colleagues will report that if the sessions of multiple users are written in one SID, then the value of the Session will not be exerted. So there is another way to solve this problem. There is no need to add session_id (SID); the premise is that you have configuration permissions on the server's php.ini:

Change output_buffering to ON, the truth is not obvious.

The second possible reason is that there is no read permission for the folder where the server saves the session , or go back In phpinfo.php, check the address where the session is saved:

session.save_path: var/tmp

So check whether the var/tmp folder is writable.

Write a file: test3.php to test it:

echo var_dump(is_writeable(ini_get(“session.save_path”)));
?> ;

If bool(false) is returned, it proves that the folder write permission is restricted, then change the folder and add:

//Set the current directory to the web page you wrote The session subfolder is the session saving path.
$sessSavePath = dirname(__FILE__).'/session/';

//If the new path is readable and writable (can be achieved by changing the folder attribute to 777 on FTP), then make the path Take effect.
if(is_writeable($sessSavePath) && is_readable($sessSavePath))
{session_save_path($sessSavePath);}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if php session cannot pass value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn