当前位置:网站首页>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
边栏推荐
- Evolution of large website architecture and knowledge system
- Meituan dynamic thread pool practice ideas, open source
- The American Championship is about to start. Are you ready?
- Cross end solutions to improve development efficiency
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- Ad637 notes d'utilisation
- Pl/sql basic case
- A substring with a length of three and different characters in the leetcode simple question
猜你喜欢

MMAP learning

PyGame practical project: write Snake games with 300 lines of code

Calculation method of boundary IOU

Talking about MySQL index

What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial

database mirroring

Concurrency control of performance tuning methodology

What about data leakage? " Watson k'7 moves to eliminate security threats

boundary IoU 的计算方式

数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
随机推荐
Database tuning solution
What about data leakage? " Watson k'7 moves to eliminate security threats
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
How to organize an actual attack and defense drill
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
AD637使用筆記
A long's perception
Common interview questions of redis factory
Leetcode simple question: the minimum cost of buying candy at a discount
Database recovery strategy
Text组件新增内容通过tag_config设置前景色、背景色
POJ 3237 tree (tree chain splitting)
Stored procedures and stored functions
"Chris Richardson microservices series" uses API gateway to build microservices
CRM creates its own custom report based on fetch
About the writing method of SQL field "this includes" and "included in" strings
The simple problem of leetcode is to split a string into several groups of length K
Type of fault
Bitbucket installation configuration