当前位置:网站首页>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
边栏推荐
- Net small and medium-sized enterprise project development framework series (one)
- Tips for using SecureCRT
- Decorator learning 01
- MySQL actual combat 45 lecture learning (I)
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
- How to add new fields to mongodb with code (all)
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- Summary of El and JSTL precautions
- DataGrid directly edits and saves "design defects"
猜你喜欢

The American Championship is about to start. Are you ready?

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture

Storage optimization of performance tuning methodology

Technology cloud report: how many hurdles does the computing power network need to cross?
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/56/d596e7c7bec9abd888e8f18f9769f8.png)
[Yugong series] go teaching course in July 2022 004 go code Notes

元宇宙中的三大“派系”

Unique occurrence times of leetcode simple questions

科技云报道:算力网络,还需跨越几道坎?

Daily question brushing record (XIV)
随机推荐
Evolution of large website architecture and knowledge system
Two stage locking protocol for concurrency control
Net small and medium-sized enterprise project development framework series (one)
Common interview questions of JVM manufacturers
Shell script, awk uses if, for process control
A long's perception
装饰器学习01
MySQL actual combat 45 lecture learning (I)
华为云ModelArts文本分类–外卖评论
How to organize an actual attack and defense drill
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
极狐公司官方澄清声明
Codeforces 12D ball tree array simulation 3 sorting elements
Image editor for their AutoLayout environment
如何開發引入小程序插件
Leetcode simple question: check whether each row and column contain all integers
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
每日刷题记录 (十四)
AD637使用笔记
Sentinel production environment practice (I)