About server scripts and browser scripts

Source: Internet
Author: User
Tags send cookies
About server scripts and browser scripts
I know this question is 2, please enlighten me.

I think that browser scripting, like JavaScript, is what the browser interprets to do. Server scripts, which are performed by the server, such as PHP.
In the Book of the PHP tutorial also said:

Reference
You cannot view the source code in the browser by looking at the original document-you can only see the output of the PHP file, which is pure HTML. This is because the script has been executed on the server before the result is returned to the browser.


Then my understanding is that PHP is to take the next generation into an HTML page, when the browser access, generate a page, and then this page to the browser, this PHP task is completed. Or that's where PHP ends.


But PHP can also operate Cookie,cookie is not in the browser of things, understand not AH.

I analyzed the possible situation where PHP generated the page, using JavaScript or something like that to manipulate this. But not sure, ask for advice.


------Solution--------------------
Understanding is basically correct. As for PHP's ability to read and write client-side cookies, it is implemented via HTTP headers.
When you visit a website in a browser, you actually send an HTTP request. The request header contains the cookie on the client machine. The cookie on your machine is sent to the server, and you can interpret the server as a PHP script. After the PHP script processing request has been interpreted into HTML, HTTP Response,response will return the finished HTML and cookie to the client machine. This will enable PHP to read and write cookies on your machine.

One HTTP interaction has a lot of things. Here is my HTTP interactive process to open Baidu. You can see what the request and response are interacting with.

Request headers
---------------------------------
Request url:http://www.baidu.com/
Request Method:get
Status code:200 OK
Request Headersview Source
accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-charset:gbk,utf-8;q=0.7,*;q=0.3
Accept-encoding:gzip,deflate,sdch
accept-language:zh-cn,zh;q=0.8
Cache-control:max-age=0
Connection:keep-alive
cookie:baiduid=9222a2a4bf6d80d713705cd25fe0390e:fg=1; bdshare_firstime=1357125911300; bduss= Ldmy2jrsfvxr2jva0fib0loemjiq0s4anzab1zgcez2awjln1hxsnffthnzc0zsqvfbqufbjcqaaaaaaaaaaaoussdzifyfmw5qzwn0mtbuaaaaaaaaaaaaaa AAAAAAAAAAAAAAAACAYIARMAAAALD275YQAAAA6P5DAAAAAAAXMC4ZNI4XNOWU1FDSFNRQE; bdut=4dim9222a2a4bf6d80d713705cd25fe0390e13b748d0bb81; pms_cache=1358090765568; bdrcvfr[fewj1vr5u3d]=i67x6tjhwwyf0;


Response header
------------------------
Cache-control:private
Connection:keep-alive
Content-encoding:gzip
content-length:8379
Content-type:text/html;charset=utf-8
Date:sat, 21:55:43 GMT
Expires:sat, 21:55:43 GMT
server:bws/1.0
set-cookie:h_ps_pssid=1792_1421_1760_1788; path=/; Domain=.baidu.com
Vary:accept-encoding
------Solution--------------------
LZ understand very well, compared to many in this version of the "How to assign JavaScript to PHP variable" person (and no BS meaning, I also came this way) took a step forward, at least know that the end of the PHP run, there is no end to the situation, it is relatively advanced topic, there is a need to study

When I was doing JS, and then found that some things to do in the server to do, touch the name of the server side JavaScript (during the above-mentioned thinking), found that support very little, and then found PHP, so just embark on the path of PHP

Upstairs said the HTTP head, I suggest you can start from now on to understand, before nothing auxiliary, light by reading to understand this more difficult; but the times are different, now a lot of tools (and simple tools) can be realized grab bag, grab bag is not difficult, I still think this is a hacker to do, laughed at , install a Firebug plugin in Firefox, or Chrome has a similar function (or chrome may be simpler)

An HTTP process is such that the connection is omitted, you can learn the socket or Web page to understand the TCP connection, only the part of the data transmission--
1. Client requests, such as access to a URL: At this time the data sent is not just a URL, but a bunch of request header information, the request header information contains methods (GET, POST ...) + URL, website host+ port, access to the software and version, transmission mode (whether compressed, etc.), language, and other, plus get or post data--details see upstairs example and HTTP protocol RFC2616, if access is second or later, may also add cookie information (these are basically the browser's own completion)
2. The server returns data: Return is not just a Web page (or file), before there is a lot of return header information, return information is also many, the first line is the state (only one state), 200 means successful return, 404 means not Found, 500 indicates a server problem and so on, more status number also depends on the protocol document And then there are other data types such as return, text/html of course, HTML; expiration time, transmission mode ... The above basic is done automatically by the Web server (such as Apache), but it can also be changed by PHP. The cookie is loaded in this header, PHP's header function is to change the above content, but the session and the cookie separately extracted to facilitate programming. Return to the end of the header is the "official" transfer of content, such as the web itself, the picture itself and so on

Knowing this is helpful for you to use the header function and handle other things, such as knowing why PHP has to send a header function before the output, why the session should be placed at the top of the program, how PHP outputs XML, how to let the browser cache or not cache a page ... Such

As for JavaScript, it can also send cookies, which are not related to PHP cookies, but because they are sent by Cookie,javascript in the next request PHP can also read, this extension of a topic, any other software or programs to write cookies, Server-side can also read, this to discuss other topics to study it again
  • 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.