当前位置:网站首页>Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
2022-07-05 22:11:00 【Selfish thoughts】
background
Recently, I found some user feedback , Can't get login information , So it was analyzed
analysis
Our login information is encrypted and stored in cookie Medium , Check this user's cookie The encrypted information contains plus “+” , however php $_COOKIE At the time of acquisition , It becomes a space , So decryption failed
Analyze the information in the request header and find , The value passed by the request is “+” Of , however :

such as cookie Stored in the “cWEolyrQ0l63FG+YWHA” ,$_COOKIE Get the displayed “cWEolyrQ0l63FG YWHA” , One more space
So I looked for information
Find the following introduction :
notes : Sending cookie when ,setcookie The value of will be automatically URL code . When received... Will be done URL decode . If you don't have to , have access to setrawcookie() Instead of .
notes :setrawcookie() And setcookie() almost the same , The difference is that it will not be sent to the client , Yes cookie Value automatically URL code . Use setrawcookie, Values within these characters cannot be used :(; \ t \ r \ n \ 013 \ 014)
Look at this introduction , I think after taking it out urldecode Just a moment
urldecode($_COOKIE['user_id'])
Actually, it doesn't work , So try urlencode , I found it successful , But if cookie There are other special characters in such as "/" , Not anymore.
urlencode($_COOKIE['user_id'])
So this scheme , Can't solve the problem
Solution
We analyzed the request header above cookie Is a full , So we can try to get from the request header cookie
<?php
// getallheaders yes apache Supported functions ,nginx You need to define yourself
if (!function_exists('getallheaders')) {
function getallheaders()
{
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
$cookieStr = getallheaders()['Cookie'];
$cookies = explode(';',$cookieStr);
foreach($cookies as $cookie)
{
$cookieTmp = explode('=',$cookie);
$_COOKIE[trim($cookieTmp[0])] = trim($cookieTmp[1]);
}
Other solutions
Yes cookie The value of the to base64_encode(), When you use it base64_decode()
Reference resources
边栏推荐
- database mirroring
- Sub total of Pico development
- 阿龙的感悟
- Drawing HSV color wheel with MATLAB
- Oracle is sorted by creation time. If the creation time is empty, the record is placed last
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- Pl/sql basic syntax
- Learning of mall permission module
- About the writing method of SQL field "this includes" and "included in" strings
- Deeply convinced plan X - network protocol basic DNS
猜你喜欢

Storage optimization of performance tuning methodology

Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~

华为云ModelArts文本分类–外卖评论

AD637使用筆記

A trip to Suzhou during the Dragon Boat Festival holiday

Pl/sql basic case

ICMP introduction

Implementation technology of recovery

CA certificate trampled pit

The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
随机推荐
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Tips for using SecureCRT
database mirroring
A long's perception
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Shell script, awk condition judgment and logic comparison &||
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
ICMP introduction
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Shell script, awk uses if, for process control
Implementation technology of recovery
Oracle views the data size of a table
数据泄露怎么办?'华生·K'7招消灭安全威胁
Ad637 notes d'utilisation
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
How to organize an actual attack and defense drill
Comment développer un plug - in d'applet
The real situation of programmers
Poj 3237 Tree (Tree Chain Split)