PHP is unable to get cookie problem processing for the first time, phpcookie_php tutorial

Source: Internet
Author: User

PHP is unable to get cookie problem processing for the first time, Phpcookie


First write the following simple code:

Copy the Code code as follows:
<?php
Setcookie (' A ', ' value ');
Print $_cookie[' a '];

On the first visit, an error occurred:

The reason for the error is that the value of $_cookie[' a '] does not exist. Second visit:

Q: Why are there no cookies when I visit for the first time?? Didn't I set it up first and then get it??

A: Use the firebug of Firefox to View "network":

Client:

As you can see, the browser (client) makes a request to the server, makes the request, takes various parameters in the request header message, tells the server what text (accept) I want to receive, what encoding format (accept-encoding), What language (accept-language) and so on, and of course, the cookie is also uploaded to the server (cookie).

Server-side:

First step: Setcookie (' A ', ' value ')

Because the cookie is set on the client, the Setcookie function cannot set the cookie itself, it can only tell the browser via the header message that: "Brother, I want to set a cookie, the key is a, value is, you help me to set up a bit." You can also understand that: "Come, I am happy today, I reward you a cookie."

Step two: $_cookie[' a ']$_cookie[' a ']

Quite simply, the action is to search for a cookie in the cookie string that the browser brings in, and return its value.

Obviously, this "key is a" cookie cannot be found, because when the client accesses the server, the cookie does not exist at all, and the first step in the previous setting of the cookie header information is not returned to the client (PHP to execute from the top to bottom of the statement will be returned to the client)

Step Three: Server return information

Where the header information returned with "Set-cookie A=value", the browser received this header information, the cookie stored in a computer file, for different browser Cookie storage location seems different, this is not in the scope of this article.


Refresh the browser, once again to access the server, the same, will be a lot of header information to the server, but this time with the past in the cookie, there is more than one a=value. The heart $_cookie[' a '] will naturally be able to find the value of this cookie with the key A in the cookie string.

http://www.bkjia.com/PHPjc/928232.html www.bkjia.com true http://www.bkjia.com/PHPjc/928232.html techarticle The first time PHP failed to get COOKIE problem processing, Phpcookie first wrote the following simple code: Copy code code as follows: Php Setcookie (' A ', ' value '); print $_cookie[' a ']; First visit ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.