Notes for assigning $ _ SERVER variables to $ _ SESSION variables in PHP

Source: Internet
Author: User
Tags network function
Save the following code as test. php {code ...} test this code locally and access urllocalhosttest. phpa $ _ SERVER [& #039; PATH_INFO & #039;] value: & #039; a & #039;. After the value is assigned, $ _ SESSION [& #039; the value of test & #039;] Is & #039; a & #039;. After the page is refreshed, $ _ SESSION [...

Save the following code as test. php.

 ';$_SESSION['test'] = $_SERVER['PATH_INFO'];var_dump($_SESSION['test']);?>

Test this code locally and access url http: // localhost/test. php/.
The value of $ _ SERVER ['path _ info'] is '/A'. After the value is assigned, the value of $ _ SESSION ['test'] is'/',After the page is refreshedOutput $ _ SESSION ['test'] at the beginning of the page should also be '/A' in theory, but $ _ SESSION ['test'] becomes 'notice '.

The value of notice is the same as the value of src in it, and the value of src is changed to other values, and the same value will be output.


Reply content:

Save the following code as test. php.

 ';$_SESSION['test'] = $_SERVER['PATH_INFO'];var_dump($_SESSION['test']);?>

Test this code locally and access url http: // localhost/test. php/.
The value of $ _ SERVER ['path _ info'] is '/A'. After the value is assigned, the value of $ _ SESSION ['test'] is'/',After the page is refreshedOutput $ _ SESSION ['test'] at the beginning of the page should also be '/A' in theory, but $ _ SESSION ['test'] becomes 'notice '.

The value of notice is the same as the value of src in it, and the value of src is changed to other values, and the same value will be output.


Key:

 

When accessing the browser, the/test. php/notice
At this time, test. php is triggered,

$ _ SESSION ['test'] = $ _ SERVER ['path _ info']; // notice

So even if you refresh the page again, notice is printed for the first time.

I know the problem. The problem lies in the one you mentioned.imgIn the label, the src of your img label isnotice. In html, if you do not addhttp://xxxIf the prefix is directly put here, it will access this file in the current directory, that is, when you refresh this page, this page is connected againimgAccessedhttp://localhost/test.php/notice.

Because the browser recognizes the last slash/So the current directory ishttp://localhost/test.php/. It automaticallynoticeThe url for this access is displayed at the end. Then accesshttp://localhost/test.php/noticeOf coursepathinfoOfnoticeSet it to the session, so what you see every refresh is actuallyimgTag refresh session.

Thanks for the invitation. I have answered a lot before. I would like to add a few points:

  1. Your questions are very familiar. From the sentence "The value of notice is the same as the value of src, and the value of src is changed to other values, the same value will be output, you have studied the pattern of repeated errors by yourself. The browser also opens "the art of question", which deserves encouragement.
  2. Your code segment is not rigorous enough. You need to criticize it. The seventh line (the line above the img tag) is obviously a syntax error. It should be the end of PHP. "?> "Character, in the programming world, the difference is a thousand miles away. If it is not a syntax error and the compiler does not report an error, you will be wasting your time.

Analyze your code in detail:

During the first execution, the browser initiated two HTTP requests: http: // localhost/test. php/a and http: // localhost/test. php/notice:

In this case, the content of the session file on the server side is as follows:

For the first HTTP Request (http: // localhost/test. php/a), the page displays "Notice: Undefined index" because it is printed after session_start () is not assigned a value for $ _ SESSION ['test, this array does not exist, so an error is returned.

Next, save the current PATH_INFO (that is, "/a" to the session file) and output it. This is what you see in your browser.

Because the browser encounters an img tag, http: // localhost/test is requested based on the src attribute. php/notice (please refer to the relative path of src, I will not elaborate), this request is carried out by the browser in the background, it will not change the HTML source code of the current page, so, you cannot see any changes on the page.

Test. php is actually executed again. For the first var_dump (), It outputs "/a". Then, it puts the current PATH_INFO (I .e./notice) assign a value to $ _ SESSION ['test'], overwrite the original value of the session file (see the above), and then execute the second var_dump (). Where are the output of var_dump? In the Response of the http request "HTTP: // localhost/test. php/notice", the text output is obviously not a valid image, so your img label will fail to display.

Evidence of two HTTP requests:

We can see that the Reponse Body obtained by the two HTTP requests is not long (149 Bytes vs 61 Bytes), because the request http: // localhost/test. when php/notice does not have "Notice: Undefined index", an error is returned.

Two HTTP requests are returned when the page is refreshed for the second time:
The page you finally see is as follows:

The first HTTP Request (http: // localhost/test. php/a): Print "/notice" (this can be taken out in the session file, see) and overwrite it with the current PATH_INFO ("/, print "/".

Then the browser automatically initiates the second HTTP Request (http: // localhost/test. php/notice): First print "/a" in the background, then assign "/notice", and then print "/notice", because the display of illegal image content fails, you cannot see any changes on the page.

The process between 3rd and N is exactly the same as that of the second page refresh.

Finally:
We recommend that you add debugging skills to refresh the page and read the text of HTTP Communication and session files.

Thanks for the invitation. null and joyqi have explained the problem clearly.
By the way, the notice will be triggered when var_dump ($ _ SESSION ['test']) is used for the first time. It is recommended that you strictly consider the following:

if(isset($_SESSION['test'])){    var_dump($_SESSION['test']);}

In addition, when chrome is used to switch to the network function, an http: // 127.0.0.1/notice request will be found.
Nginx does not support $ _ SERVER ['path _ info'] by default. This value cannot be obtained.

Check whether the session takes effect and whether the local COOKIE containsPHPSESSIONID.

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.