Home > Backend Development > PHP Problem > Solution to PHP session failure and not delivery

Solution to PHP session failure and not delivery

卡哇伊
Release: 2023-03-02 06:24:01
Original
3455 people have browsed it

The solution to the problem that PHP session fails and is not passed: First write a php file with the content "<?=phpinfo()?>"; then pass it to the server to see the server's parameter configuration, indicating that the user ID is The identified session; finally assign the name of the session to Havi and output the session.

Solution to PHP session failure and not delivery

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

Let’s first write a php file: <?=phpinfo()?>, 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 0.

This parameter specifies whether to enable transparent SID support, that is, whether session is passed along with the URL. My personal understanding is that once this parameter is set to 0, then each URL will start a session. 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.

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

<?php
//表明是使用用户ID为标识的session
session_id(SID);
//启动session
session_start();
//将session的name赋值为Havi
$_SESSION[&#39;name&#39;]=”Havi”;
//输出session,并设置超链接到第二页test2.php
echo “<a href=\”test2.php\”>”.$_SESSION[&#39;name&#39;].”</a>”;
?>
Copy after login

File 2: test2.php

<?php
表明是使用用户ID为标识的session
session_id(SID);
//启动session
session_start();
//输出test1.php中传递的session。
echo “This is “.$_SESSION[&#39;name&#39;];
?>
Copy after login

Every page must be written to open the session, otherwise it will still not work properly

So, the key point Add session_id(SID); before session_start();, so that when the page is converted, the server uses the user saved in the serversession folder The session solves the problem of delivery.

However, some colleagues will report that if the session of multiple users is written in one SID, then the value of the Session will be Can't perform anymore. So there is another way to solve this problem. No need to add session_id(SID);The premise is that you have configuration permissions on the server's php.ini:

If output_buffering<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;"></span><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;"></span># is changed to ON, the truth will not be clear.

The second possible reason is that there is no read permission to the folder where the server saves session, so go back to phpinfo.php, View the saved address of session:

<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">session.save_path: var/tmp</span>

所以就是检查下var/tmp文件夹是否可写。

写一个文件:test3.php来测试一下:

<?
echo var_dump(is_writeable(ini_get(“session.save_path”)));
?>
Copy after login

如果返回bool(false),证明文件夹写权限被限制了,那就换个文件夹咯,在你编写的网页里加入:

//设置当前目录下session子文件夹为session保存路径。$sessSavePath = dirname(__FILE__).’/session/’;
//如果新路径可读可写(可通过FTP上变更文件夹属性为777实现),则让该路径生效。if(is_writeable($sessSavePath) && is_readable($sessSavePath)){session_save_path($sessSavePath);}
Copy after login

The above is the detailed content of Solution to PHP session failure and not delivery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template