当前位置:网站首页>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
边栏推荐
- Implementation technology of recovery
- How to develop and introduce applet plug-ins
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- MySQL服务莫名宕机的解决方案
- Tips for using SecureCRT
- 如何向mongoDB中添加新的字段附代码(全)
- The simple problem of leetcode is to split a string into several groups of length K
- What about data leakage? " Watson k'7 moves to eliminate security threats
- Pl/sql basic syntax
猜你喜欢

Index optimization of performance tuning methodology

Meituan dynamic thread pool practice ideas, open source

Type of fault

Lightweight dynamic monitorable thread pool based on configuration center - dynamictp

Interview questions for famous enterprises: Coins represent a given value

K210学习笔记(四) K210同时运行多个模型

Sentinel production environment practice (I)

Blocking protocol for concurrency control

database mirroring

华为云ModelArts文本分类–外卖评论
随机推荐
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
如何组织一场实战攻防演练
Pl/sql basic syntax
Learning of mall permission module
K210学习笔记(四) K210同时运行多个模型
Storage optimization of performance tuning methodology
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
poj 3237 Tree(树链拆分)
A long's perception
微服务链路风险分析
How to organize an actual attack and defense drill
Unique occurrence times of leetcode simple questions
Codeforces 12D Ball 树形阵列模拟3排序元素
Database recovery strategy
ICMP 介绍
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Poj 3237 Tree (Tree Chain Split)
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Oracle views the data size of a table
如何向mongoDB中添加新的字段附代码(全)