Summary
Through the above three steps, we used PHP to quickly implement the form display and form processing functions on a single page.
Setting Cookies
PHP provides powerful functions for setting and reading Cookies. Here, we do not want to introduce too much about Cookies, but users should be aware of the important role that Cookies may play in the process of designing WEB applications.
Users can use the setcookie() function provided by PHP to create or modify cookies. The setcookie() function includes a total of 6 parameters, which can accurately control cookies.
The simplest way to set cookies using the setcookie() function is:
setcookie('name', 'PETER');
In this way, when the user visits the entire During the site page, PHP will automatically create a variable named $name and assign the value PETER to the variable. We call this kind of cookie a session cookie, that is, its scope is the user's entire session.
If we want the cookie value to remain valid after the visiting user leaves the site, we can use the corresponding parameters of the setcookie() function to set the validity period of the cookie. Here, we need to explain the time setting of PHP. PHP is a technology developed based on Unix. Users need to express the current time in seconds since January 1, 1970. It is really confusing for the majority of ordinary users who have no experience in Unix system programming. However, you don’t have to worry, because PHP provides us with a very simple solution, namely the mktime() function. Users can enter the time they want to represent in the order of hours, minutes, seconds, months, days, and years in the mktime() function, and the mktime() function will return the number of seconds from January 1, 1970. For example, if we want to set a cookie that is valid until the year 2000, we can use the following method:
< ? php
$y2k = mktime(0,0,0,1,1,2000);
setcookie('name', 'PETER', $y2k);
? >
If the user wants to update an existing cookie, he or she can simply overwrite the original value directly. For example, even if we have set the cookie according to the above code, we can still make the following changes to it:
< ?php
$y2k = mktime(0,0,0, 1,1,2000);
setcookie('name', 'JEFF', $y2k);
? >
One thing that users need to pay attention to here is , although we have modified the cookie value, the value of the $name variable in PHP will still not change before the modified page is loaded. If the user wants to change the value of the corresponding PHP variable while changing the cookie value, the following method can be used:
< ? php
$name = 'JEFF';
$y2k = mktime(0,0,0,1,1,2000);
setcookie('name', $name, $y2k);
? >
After the validity period parameter, the setcookie() function provides parameters for setting the path and domain of the page that can read the cookie value. For security reasons, by default, only pages in the same directory or subordinate subdirectories as the page that sets the cookie can read the corresponding cookie value. However, we can also modify this setting if necessary. For example:
< ? php
setcookie(‘name’, ‘jeff’, $y2k, ‘~/myhome’, ‘.domain.com’);
? >
Through the above code, we set that any page located in the ~/myhome directory and belonging to the .domain.com domain can read the cookie value.
The last parameter of the setcookie() function is rarely used. This parameter can specify that the cookie value is returned only to WEB servers running secure connection protocols, such as SSL. If the user wants to enable this parameter function, he only needs to set its value to 1.
Deleting cookies using PHP is also very easy. Users only need to enter the name of the cookie they want to delete in the setcookie() function, and PHP will automatically delete cookies.
< ?php setcookie('name'); ? >
Finally, there is one more thing to say about cookies. Considering the way cookies work in the HTTP protocol, users must be aware that all cookie settings are sent before any text is displayed. If the user first sets the displayed text when writing code and then sends the cookie, PHP will pop up an error message and cannot complete the cookie setting. For example:
< ?php
setcookie('name', 'jeff');
echo "Hello Everyone!";
? >
This way of setting cookies is correct. But if the following method is used:
< ?php
echo “Hello Everyone!”;
setcookie('name', 'jeff');
? >
, an error message will appear and the cookie setting cannot be completed.
Date and time
PHP provides a variety of simple functions to facilitate users to display and control date and time.
If the user wants to display a certain date or time in a certain form, they can use the date() function provided by PHP. The date(). function includes two parameters, which are used to set the display format of the date and the timestamp representing the displayed date. The timestamp must be expressed in seconds from January 1, 1970.Like the strftime() function in C language or the POSIX::strftime() function in Perl, the date() function in PHP has a lot of formatting options, which will not be explained here. For example, the date() function is used as follows:
< ?php
$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."
? >
The displayed result is: Peter was born on October 10, 1975--10:10 p.m.
Summary
PHP is a powerful tool for quickly creating dynamic WEB sites, with its familiar syntax style and open source code Features enable users to understand and master its functions in the shortest possible time and then unleash its great potential. I hope this article can inspire readers. I wish everyone will become a PHP master as soon as possible.