PHP入门速成(3)
小结
通过以上三步,我们使用PHP快速地在单一页面上实现了表单显示和表单处理的功能。
设定Cookies
PHP提供了强大的设定和读取Cookies的功能。在此,我们不想对Cookies进行过多的介绍,但是用户应当认识到在设计WEB应用的过程中Cookies可能起到的重要作用。
用户可以使用PHP提供的setcookie()函数创建或修改Cookie。setcookie()函数总共包括6个参数,能够对cookie进行精确的控制。
使用setcookie()函数设定cookie的最简单的方式为:
setcookie(‘name', ‘PETER');
这样,在用户访问整个站点页面的期间,PHP将会自动创建一个名为$name的变量,并将值PETER赋予该变量。我们把这种Cookie称做会话Cookie,即其作用域为用户的整个会话过程。
如果我们希望Cookie值在访问用户离开站点后仍然有效的话,可以使用setcookie()函数的相应参数,设定Cookie的有效期。这里,我们需要对PHP的时间设定进行一下说明。PHP是一种基于Unix开发出的技术,用户需要用自1970年1月1日之后的秒数表示当前时间。对于没有任何Unix系统编程经验的广大普通用户来说真是有点摸不着头脑。不过,大家大可不必担心,因为PHP为我们提供了一个非常简便的解决方式,即mktime()函数。用户可以在mktime()函数中按照小时、分、秒、月、日、年的顺序输入用户希望表示的时间,mktime()函数将会返回该时间距离1970年1月1日的秒数。举例来说,如果我们希望设定一个有效期持续到2000年的Cookie的话,可以使用如下方式:
$y2k = mktime(0,0,0,1,1,2000);
setcookie(‘name', ‘PETER', $y2k);
? >
如果用户希望更新已经存在的cookie,可以采用简单的直接覆盖原值的方法。例如,即使我们已经按照上述代码对cookie进行了设定,仍可以对其做以下改动:
$y2k = mktime(0,0,0,1,1,2000);
setcookie(‘name', ‘JEFF', $y2k);
? >
这里需要提醒用户注意的一点是,虽然我们对cookie值进行了修改,但是在没有加载经过修改的页面之前,PHP中$name变量的值仍然不会改变。如果用户希望在更改cookie值的同时更改 相应PHP变量的值,可以采用以下方式:
$name = ‘JEFF';
$y2k = mktime(0,0,0,1,1,2000);
setcookie(‘name', $name, $y2k);
? >
在有效期参数之后,setcookie()函数提供了设置可以读取cookie值的页面的路径和域的参数。出于安全因素的考虑,在默认情况下,只有与设定cookie的页面处于同一目录或下属子目录的页面才能够读取相应cookie值。但是,根据需要,我们也可以对这一设定进行修改。例如:
setcookie(‘name', ‘jeff', $y2k, ‘~/myhome', ‘.domain.com');
? >
通过上述代码,我们设定凡是位于~/myhome目录下,属于.domain.com域的页面都能够读取该cookie值。
setcookie()函数的最后一个参数较少被使用。该参数可以指定只向运行安全连接协议,如SSL,的WEB服务器返回cookie值。如果用户希望启动该参数功能,只需要将其值设为1即可。
使用PHP删除cookie也非常简便,用户只需要在setcookie()函数中输入希望删除的cookie的名称,PHP就会自瓿伤械纳境ぷ鳌@纾?
最后,关于cookie还要说明一点。考虑到HTTP协议中cookie的运作方式,用户一定要注意应当在显示任何文字之前发送所有的cookie设置。如果用户在编写代码时先设定显示的文字然后再发送cookie的话,PHP就会弹出报错信息,无法完成cookie的设置。举例来说:
setcookie(‘name', ‘jeff');
echo “Hello Everyone!”;
? >
这种cookie设置的方式是正确的。但是如果采用如下方式:
echo “Hello Everyone!”;
setcookie(‘name', ‘jeff');
? >
则会出现报错信息,无法完成cookie的设置。
日期和时
PHP提供了多种简便的功能以方便用户显示和控制日期和时间。
如果用户希望以某种形式显示某一日期或时间的话,可以使用PHP提供的date()函数。date().函数包括两个参数,分别用于设定日期的显示格式以及代表显示日期的时间戳,其中,时间戳必须采用距离1970年1月1日的秒数方式表示。如同C语言中的strftime()函数或Perl中的POSIX::strftime()函数,PHP中的date()函数具有非常多的格式设置选项,这里就不一一说明了。举例来说,date()函数的使用方式如下:
$birthday_stamp = mktime(10,10,0,10,20,1975);
$birthday_formatted = date(‘F d, Y - g:i a',$birthday_stamp);
echo “Peter was born on $birthday_formatted.”
? >
显示结果为:Peter was born on October 10, 1975--10:10 p.m.
总结
PHP是一种快速创建动态WEB站点的强大工具,其熟悉的语法风格和源代码开放的特性能够使使用者在最短的时间内了解和掌握其功能并进而发挥出其巨大潜力。希望本文能够对广大读者有所启发。祝大家早日成为PHP高手。

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 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 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 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 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 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.
