PHP Introduction Crash (3) _php Basics

Source: Internet
Author: User
Tags current time php introduction setcookie

Summary

With the above three steps, we use PHP to quickly implement form display and form processing on a single page.


Set cookies

PHP provides a powerful set and read cookies function. Here, we do not want to introduce too much cookies, but users should be aware of the important role cookies may play in designing Web applications.

Users can create or modify cookies using the Setcookie () function provided by PHP. The Setcookie () function includes a total of 6 parameters that enable precise control of the cookie.

The simplest way to use the Setcookie () function to set cookies is:

Setcookie (' name ', ' PETER ');

This way, when the user accesses the entire Site page, PHP automatically creates a variable named $name and assigns the value Peter to the variable. We refer to this cookie as the session cookie, which is the scope of the user's entire session process.

If we want the cookie value to remain in effect after the user leaves the site, you can use the appropriate parameters of the Setcookie () function to set the cookie's expiration date. Here, we need to give a description of the PHP time setting. PHP is a technology developed on the basis of UNIX, users need to use the number of seconds since January 1, 1970 to represent the current time. It's a bit confusing for the average user who doesn't have any experience with UNIX system programming. But there's no need to worry, because PHP provides us with a very simple solution, the mktime () function. Users can enter the time that the user wishes to represent in the order of hours, minutes, seconds, months, days, and years in the Mktime () function, and the Mktime () function returns the number of seconds from January 1, 1970. For example, if we want to set a cookie that lasts until 2000, you can use the following methods:

<? Php

$y 2k = mktime (0,0,0,1,1,2000);

Setcookie (' name ', ' PETER ', $y 2k);

? >

If a user wishes to update an existing cookie, a simple method of overwriting the original value is possible. For example, even though we have set the cookie in the code above, we can make the following changes to it:

< PHP

$y 2k = mktime (0,0,0,1,1,2000);

Setcookie (' name ', ' JEFF ', $y 2k);

? >

The point to note here is that although we have modified the cookie value, the value of the $name variable in PHP will not change until 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 methods can be used:

<? Php

$name = ' JEFF ';

$y 2k = mktime (0,0,0,1,1,2000);

Setcookie (' name ', $name, $y 2k);

? > After the expiration argument, the Setcookie () function provides the path to the page where the cookie value can be read and the parameters of the field. For security reasons, by default, only pages that have the same directory or subordinate subdirectory as the cookie-setting page can read the corresponding cookie value. However, as needed, we can also modify this setting. For example:

<? Php

Setcookie (' name ', ' Jeff ', $y 2k, ' ~/myhome ', '. domain.com ');

? >

Through the above code, we set the ~/myhome directory, the page that belongs to the. domain.com domain can read the cookie value.
The last parameter of the Setcookie () function is less used. This parameter can specify that a cookie value be returned only to a Web server running a secure connection agreement, such as SSL. If the user wants to start the parameter feature, simply set the value to 1.
Using PHP to delete cookies is also very simple, users need only in the Setcookie () function to enter the name of the cookie you want to delete, PHP will be from  finish injury  weapons na ぷ @ the rescue?

< php setcookie (' name ');? >

Finally, there is one more thing to say about cookies. Given the way cookies work in the HTTP protocol, users must be aware that all cookie settings should be sent before any text is displayed. If a user sets the text to be displayed and then sends a cookie before writing the code, PHP will eject the error message and it cannot complete the cookie setting. For example:

< PHP

Setcookie (' name ', ' Jeff ');

echo "Hello everyone!";

? >

This cookie is set in the right way. But if you use the following method:

< PHP

echo "Hello everyone!";

Setcookie (' name ', ' Jeff ');

? >

The error message will appear and the cookie settings cannot be completed. Date and time

PHP offers a variety of simple features to make it easier for users to display and control dates and times.

If a user wants to display a date or time in some form, you can use the date () function provided by PHP. The date (). function includes two parameters, which are used to format the display of the date and the time stamp that represents the date, in which the timestamp must be expressed in seconds from January 1, 1970. As with the strftime () function in C, or the Posix::strftime () function in Perl, the date () function in PHP has a lot of formatting options, which is not explained here. For example, the date () function is used in the following ways:

< PHP
$birthday _stamp = mktime (10,10,0,10,20,1975);
$birthday _formatted = Date (' F D, y-g:i a ', $birthday _stamp);
Echo "Peter is born on $birthday _formatted."
? >

The result is: Peter is born on October, 1975--10:10 p.m.


Summarize

PHP is a powerful tool for quickly creating dynamic Web sites, and its familiar syntax style and open source features enable users to understand and master their functions in the shortest possible time and thereby develop their great potential. I hope this article can enlighten the readers. I wish you all an early success as a PHP master.

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.