Getting Started with PHP crash (3) _php tutorial

Source: Internet
Author: User
Tags getting started with php setcookie
Summary

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


Set cookies

PHP provides powerful settings and the ability to read cookies. We do not want to introduce too much cookies here, 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 allow precise control of the cookie.

The simplest way to set a cookie using the Setcookie () function is:

Setcookie (' name ', ' PETER ');

Thus, during a user's visit to the entire Site page, PHP automatically creates a variable named $name and assigns the value Peter to the variable. We call this cookie a session cookie, which is scoped to the user's entire session process.

If we want the cookie value to be valid after the Access user leaves the site, you can use the corresponding parameter of the Setcookie () function to set the expiration date of the cookie. Here, we need to explain the time setting for PHP. PHP is a UNIX-based technology that users need to represent the current time in seconds since January 1, 1970. It's a bit confusing for a general user who doesn't have any programming experience with UNIX systems. However, there is no need to worry, because PHP provides us with a very simple solution, namely the Mktime () function. The user can enter the time that the user wants 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 method:

<? Php

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

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

? >

If a user wishes to update a cookie that already exists, a simple method of overwriting the original value can be used. For example, even if we have set cookies in accordance with the above code, we can still make the following changes to them:

< PHP

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

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

? >

One thing to note to the user here is that although we have modified the cookie value, the value of the $name variable in PHP will remain unchanged until the modified page has been loaded. If the user wants to change the value of the corresponding PHP variable while changing the value of the cookie, you can use the following method:

<? Php

$name = ' JEFF ';

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

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

? > After the expiration date parameter, the Setcookie () function provides the path and domain parameters of the page that sets the value of the cookie that can be read. For security reasons, by default only pages that are in the same directory or subordinate subdirectory as the page where the cookie is set 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 ');

? >

With the above code, we set all pages in the ~/myhome directory that belong to the. domain.com domain to be able to read the cookie value.
The last parameter of the Setcookie () function is less used. This parameter specifies that the cookie value is returned only to the Web server that is running the secure connection protocol, such as SSL. If the user wants to start the parameter feature, simply set its value to 1.
Using PHP to delete cookies is also very simple, users only need to enter the name of the cookie that they wish to delete in the Setcookie () function, PHP will be illustrations from the assignment.

< php setcookie (' name ');? >

Finally, there is one more thing to note about cookies. Given the way cookies work in the HTTP protocol, it is important to note that all cookie settings should be sent before any text is displayed. If the user first sets the displayed text and then sends the cookie when writing the code, PHP will pop up an error message and cannot complete the cookie setting. For example:

< PHP

Setcookie (' name ', ' Jeff ');

echo "Hello everyone!";

? >

This cookie is set up in the correct manner. But if you use the following method:

< PHP

echo "Hello everyone!";

Setcookie (' name ', ' Jeff ');

? >

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

PHP offers a number of easy-to-use features that allow users to display and control dates and times.

You can use the date () function provided by PHP if you want to display a date or time in some way. Date (). The function consists of two parameters for setting the display format of the date and a timestamp representing the date of the display, where the timestamp must be expressed as the number of seconds from January 1, 1970. Like 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 way:

< 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 was born on October, 1975--10:10 p.m.


Summarize

PHP is a powerful tool for quickly creating dynamic Web sites, with familiar syntax styles and open-source features that enable users to understand and master their capabilities in the shortest time possible and thereby unleash their enormous potential. I hope this article can inspire the vast number of readers. I wish you soon become a master of PHP.

http://www.bkjia.com/PHPjc/314025.html www.bkjia.com true http://www.bkjia.com/PHPjc/314025.html techarticle summary through the above three steps, we use PHP to quickly implement the form display and form processing function on a single page. Set cookies PHP provides powerful settings and read the work of Cookies ...

  • Related Article

    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.