关于 服务器脚本 跟 浏览器脚本
关于 服务器脚本 和 浏览器脚本
小弟新学 web 相关知识,我知道这个问题很2,请不吝赐教。
我认为,浏览器脚本,如javascript,就是浏览器解释执行的。服务器脚本,就是服务器执行的,如PHP。
在W3C的PHP教程里面也说了:
那么我的理解就是,php是拿来生成一个html页面的,在浏览器访问的时候,生成一个页面,然后这个页面给浏览器,这个php的任务就完成了。或者说php就在这就结束了。
但是PHP又可以操作cookie,cookie不是在浏览器的东西么,理解不能啊。
我分析了下可能的情况就是,php生成的页面里面,用了javascript或者类似的东西来操作这个。但是不确定,求指教。
------解决方案--------------------
理解基本正确。至于php能读写客户端的cookie,是通过http头实现的。
当你用浏览器访问一个网站时,实际上是发送了一次http request。请求头中包含客户机器上的cookie。你机器上的cookie就是这样发送到服务器那里的,你可以把服务器理解为一个php脚本。php脚本处理请求完毕,解释成html后,就会http response,response会把解释完毕的html和cookie返回给客户机器。这样php就实现了对你机器上cookie的读写。
一次http交互有很多东西。以下就是我打开百度的http交互过程。你可以看到request和response都交互了什么内容。
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=ldMY2JRSFVXR2Jva0FIb0lOemJIQ0s4anZab1ZGcEZ2aWJlN1hXSnFFTHNZc0ZSQVFBQUFBJCQAAAAAAAAAAAouSSDzIfYFMW5qZWN0MTBuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAALD275YqAAAA6p5DAAAAAAAxMC4zNi4xNOwU1FDsFNRQe; 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, 19 Jan 2013 21:55:43 GMT
Expires:Sat, 19 Jan 2013 21:55:43 GMT
Server:BWS/1.0
Set-Cookie:H_PS_PSSID=1792_1421_1760_1788; path=/; domain=.baidu.com
Vary:Accept-Encoding
------解决方案--------------------
LZ 理解得非常好,比起很多在本版问“怎样把javascript赋值给php变量”的人(并无BS的意思,我也是这样走过来的)迈进了一步,至少知道php运行结束了

当年我是做js,然后发觉有些事情要在服务器才能做,接触到server side javascript这个名称(期间就出现过上述的思维),发现支持很少,再发现php,这样才走上php的道路
楼上说的http头,我建议你可以从现在就开始了解,以前没什么东西辅助,光靠看书理解这个比较困难;但时代不同了,现在很多工具(而且是简单的工具)可以实现抓包,抓包不是什么难事,我早年还一直以为这是黑客才做的事,见笑了;在firefox装个firebug插件,或者chrome也有类似功能(或者说chrome可能更简单些)
一个http过程是这样的,连接那些就省略了,你可以学到socket或者抓网页再去了解tcp连接,只说说数据传输的部分——
1.客户端请求,例如访问一个网址:这时发送的数据并不仅仅是一个网址,而是一堆请求头信息,请求头信息包含方法(GET、POST……)+网址,网站host+端口、访问的软件和版本、传输方式(是否压缩等),语言、以及其他,再加上get或post的数据——详细请看楼上的例子和http协议的RFC2616,如果访问是第二次或以后,还可能加上cookie信息(这些基本都是浏览器自己完成)
2.服务器处理后返回数据:返回的也并不仅仅是网页(或文件),在之前还有一堆返回头信息,返回信息也很多,第一行是状态(只有一种状态),200表示成功返回,404表示找不到,500表示服务器问题等等,更多状态号也要看协议文档;然后就是其他例如返回的数据类型,text/html当然就是html了;过期时间、传输方式……上面这些基本都由web服务器(如apache)自动完成,但也可以通过php改变。cookie就是加载这个头里面的,php的header函数就是改变上述的内容,只不过把session和cookie另外单独抽出来方便编程而已。返回头结束后才是“正式的”传输内容,例如网页本身、图片本身等等
了解这些,对于你使用header函数和处理其他的事有帮助,例如知道为何php画图后要在输出前先发一个header函数,为何session要放在程序最前面,php怎样输出xml,怎样让浏览器缓存或不缓存某个页面……诸如此类
至于javascript也能发送cookie,那个是跟php发cookie无关的,但因为都是cookie,javascript发送的在下一次请求php也能读取,这就引申一个话题,任何其他软件或程序写的cookie,服务器端也能读,这个到讨论其他话题再研究吧

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
