当前位置:网站首页>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
边栏推荐
- 科技云报道:算力网络,还需跨越几道坎?
- Installation of VMware Workstation
- database mirroring
- Concurrency control of performance tuning methodology
- Performance testing of software testing
- Comment développer un plug - in d'applet
- NET中小型企业项目开发框架系列(一个)
- 装饰器学习01
- AD637 usage notes
- The statistics of leetcode simple question is the public string that has appeared once
猜你喜欢
Drawing HSV color wheel with MATLAB
Pl/sql basic case
What changes has Web3 brought to the Internet?
Livelocks and deadlocks of concurrency control
Common interview questions of redis factory
PyGame practical project: write Snake games with 300 lines of code
A substring with a length of three and different characters in the leetcode simple question
Oracle triggers
Calculation method of boundary IOU
每日刷题记录 (十四)
随机推荐
Matlab draws a cute fat doll
Interview questions for famous enterprises: Coins represent a given value
Sentinel production environment practice (I)
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
Leetcode simple question: find the nearest point with the same X or Y coordinate
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
笔记本电脑蓝牙怎么用来连接耳机
Basic grammar of interview (Part 1)
如何开发引入小程序插件
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
How can Bluetooth in notebook computer be used to connect headphones
每日刷题记录 (十四)
Regular expressions and re Libraries
PyGame practical project: write Snake games with 300 lines of code
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Interview questions for basic software testing
What changes has Web3 brought to the Internet?
Overview of concurrency control
Recovery technology with checkpoints