Solution to the problem that php cannot pass session: 1. Enable cookies in the client; 2. Check browser issues and access cookies; 3. Enable session.use_trans_sid in php.ini.
Solution to the problem that SESSION cannot be passed across pages in PHP
1. Cookies are disabled on the client
2. There is a problem with the browser and cookies cannot be accessed temporarily
3. The session in php.ini. use_trans_sid = 0 or the --enable-trans-sid option was not turned on during compilation
The session in PHP uses the client's cookie to save the session id by default, so when there is a problem with the client's cookie, it will affect the session. It must be noted that session does not necessarily have to rely on cookies, which is also the brilliance of session compared to cookies. When the client's cookies are disabled or there is a problem, PHP will automatically attach the session id to the URL, so that the session variable can be used across pages through the session id. But this attachment also has certain conditions, namely "session.use_trans_sid = 1 in php.ini or the --enable-trans-sid option is turned on during compilation".
2. Manually pass the value through the URL and pass the session id through the hidden form.
3. Save session_id in a file, database, etc., and call it manually during the cross-page process.
## phpNow if you manually close the cookie on the client and run it again, you may not get the result. If you can't get the result, then "set session.use_trans_sid = 1 in php.ini or turn on the --enable-trans-sid option when compiling", and you will get the result "People's Republic of China"
session_start (); $_SESSION [ ' var1 ' ] = " People's Republic of China " ; ;
$url = " Next page " ;
echo $url ; ?>
![]()
![]()
Run the above code. If the client cookie is normal, you should be able to get the result "People's Republic of China".
php
session_start (); echo " The value of the passed session variable var1 is: " . $_SESSION [ ' var1 ' ];
?>
![]()
This This is the approach 1 mentioned above.
Let’s talk about approach 2:
The modified code is as follows:
s1.phpphp
session_start (); $_SESSION [ ' var1 ' ] = " People's Republic of China " ; ; $sn = session_id (); $url = " Next page " ;
echo $url ; ?>
![]()
s2.php method 3:login.html
php
session_id ( $_GET [ ' s ' ]);
session_start ();
echo " 传递的session变量var1的值为: " . $_SESSION [ ' var1 ' ];
?>
![]()
![]()
Login title >
![]()
head >
请登录:
![]()
action = " mylogin1.php " >
用户名 :
口 令 :
![]()
form >
body >
html >
mylogin1.php
<? php $name = $_POST [ ' name ' ]; $pass = $_POST [ ' pass ' ]; if ( ! $name || ! $pass ) { echo " 用户名或密码为空,请<a href="login.html">重新登录</a> " ; die (); } if ( ! ( $name == " laogong " && $pass == " 123 " )) { echo " 用户名或密码不正确,请<a href="login.html">重新登录</a> " ; die (); } // 注册用户 ob_start (); session_start (); $_SESSION [ ' user ' ] = $name ; $psid = session_id (); $fp = fopen ( " e:/tmp/phpsid.txt " , " w+ " ); fwrite ( $fp , $psid ); fclose ( $fp ); // 身份验证成功,进行相关操作 echo " 已登录<br> " ; echo " <a href="mylogin2.php">下一页</a> " ; ?>mylogin2.php
php
$fp = fopen ( " e:/tmp/phpsid.txt " , " r " );
$sid = fread ( $fp , 1024 );
fclose ( $fp );
session_id ( $sid );
session_start ();
if ( isset ( $_SESSION [ ' user ' ]) && $_SESSION [ ' user ' ] = " laogong " ) {
echo " Has logged! " ;
}
else {
// Successfully log in to perform related operations
echo " Not logged in, no access rights " ;
echo " Pleaselog in to browse " ;
Die ();
}
?>
![]()
Please also turn off the cookie test. Username: laogong Password: 123 This is to save the session id through a file. The file is: e:/tmp/phpsid.txt. Please decide the file name or path according to your own system. .
As for the database method, I will not give an example, it is similar to the file method.
To summarize, the above methods have one thing in common, which is to get the session id on the previous page, and then find a way to pass it to the next page. Add the code session_id (pass it) before the session_start(); code on the next page. Session id that came over);
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What should I do if php cannot pass session?. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
