Home Backend Development PHP Tutorial 遇到一个有意思的有关问题,不知道如何说,大家进来看上吧

遇到一个有意思的有关问题,不知道如何说,大家进来看上吧

Jun 13, 2016 pm 12:53 PM
cookie nbsp

遇到一个有意思的问题,不知道怎么说,大家进来看下吧
先看代码再说吧:

$catid = $_GET['id'];<br />
	if( !isset( $_COOKIE[ $cookie_name ] ) ){<br />
		$cart_ary = array();<br />
		$cart_ary['cart'] = array();<br />
		$cart_ary['item_num'] = 0;<br />
		$cart_ary['total_price'] = 0.00;<br />
		setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );<br />
	}<br />
	//第一次获取不到值,所以需要抑制第一次报错<br />
	$cart_ary = json_decode( str_replace( "\\","",@$_COOKIE[ $cookie_name ] ),true );<br />
	if( isset( $cart_ary['cart'][ $catid ] ) ){<br />
		$cart_ary['cart'][ $catid ]++ ;<br />
	}else{<br />
		$cart_ary['cart'][ $catid ] = 1;<br />
	}<br />
	$cart_ary['item_num'] = count_cart_item_num( $cart_ary['cart'] );<br />
	$cart_ary['total_price'] = count_cart_total_price( $cart_ary['cart'] );<br />
	setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );
Copy after login


具体问题是这么地:
因为 $cart_ary 是个二维数组,所以当我往 cookie 里面存的时候需要先序列化一下,开始时用 serialize() 和 unserialize() 这两个函数来的,但是不知道怎么回事在 反序列化 的时候报错了“offset xxx” 这种错误,后来百度了一下说是编码问题,但是我这是在同一个页面里面啊,而且都是是UTF-8码,所以这个问题个人觉得不是,那么第一问:大家怎么能看这个问题??

然后就是后来我改用了 json_encode() 和 json_decode() 这个两个函数,但是同时有发现了一个问题就是在编码之后往 cookie 里面存的时候 cookie 的内置机制应该是对序列化的数据做了转义了,就是在双引号前面加了反斜杠“\”,所以我不得不去掉它,就像这样;

str_replace( "\\","",@$_COOKIE[ $cookie_name ] )
Copy after login


然后第二问就是:
我上面说 cookie 的内置机制会对将要存入的字符串做些什么东西??


好吧,有些我知道百度,但是我一直没找到好的,所以拿出来跟大家讨论下!高手莫笑~
php json


------解决方案--------------------
你在开玩笑?
至少需要这样
$catid = $_GET['id'];
if( !isset( $_COOKIE[ $cookie_name ] ) ){
$cart_ary = array();
$cart_ary['cart'] = array();
$cart_ary['item_num'] = 0;
$cart_ary['total_price'] = 0.00;
setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );
}else {
$cart_ary = json_decode( str_replace( "\\","",@$_COOKIE[ $cookie_name ] ),true );
if( isset( $cart_ary['cart'][ $catid ] ) ){
$cart_ary['cart'][ $catid ]++ ;
}else{
$cart_ary['cart'][ $catid ] = 1;
}
$cart_ary['item_num'] = count_cart_item_num( $cart_ary['cart'] );
$cart_ary['total_price'] = count_cart_total_price( $cart_ary['cart'] );
setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );
}
Copy after login

又:count_cart_item_num 是个什么玩意?
------解决方案--------------------
1. 这个错误是因为你原先的序列化数据被破坏了,怎么被破坏的?参看第二
2. 你开启了自动转义,即魔术引号,在程序开始前,PHP已经自动帮你转义了外部提交数据,即 $_GET, $_POST, $_COOKIE 。所以你会看到莫名其妙多了个 \

你需要关掉魔术引号,修改 php.ini magic_quote_gpc=Off

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

See all articles