PHP basic learning notes

Source: Internet
Author: User
1. Cookies used in PHP are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve cookies. Create cookie: You can use the setcookie () function to create a cookie, as shown in the following figure.

1. Cookies used in PHP are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve cookies. Create cookie: You can use the setcookie () function to create a cookie, as shown in the following figure.

1. Use of cookies in PHP

Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the cookie value.

Create cookie:

You can use the setcookie () function to create a cookie, as shown in the following code:

 
The cookie is saved in the form of a key-value pair. The above code saves a cookie named user with the corresponding value "hello" and the cookie expires in 10 seconds.
Use cookie:

Use the isset () function to determine whether a cookie is set, as shown in the following code:

 
If a cookie named user is set, the value corresponding to the user is output; otherwise, the value of welcome guest is output.
Delete cookie:

To delete a cookie, you must set the expiration time of the cookie to the past time point, as shown below:

 

2. Use of session in PHP

The PHP session variable is used to store information about a user session or change the settings of a user session. Session variables are stored by a single user and can be used on all pages of the application.

Create session:

Before using session, you must call the session_start () function, as shown in the following code:

 
The above code stores a count variable in the session and is initialized to 1.

Use session:

Before getting the value in the session, you can use the isset () function to determine whether the session exists. The following code is used:

 
In the above Code, use the isset () function to determine whether a session named count exists. If yes, add the value of this session to 1. If the value of this session reaches 20, the unset () function is used to delete the session. If there is no session named count, the new session is created, the count value is assigned to 1, and the count value is output in the browser, this page is named test. php: When we continuously access this page, we will find that the value of count is constantly increasing. When we reach 19, we will access this page again. Then, the browser will display the unset session count and access it again, the value of count starts from 1.

Delete session:

In addition to using the unset () function to delete a session, you can also use the session_destory () function to delete the session. The difference is that the unset () function deletes a session and session_destroy () the function deletes all sessions.

3. handle errors in PHP

Die () function:

Use the die () function to output custom error messages and terminate the script execution, as shown in the following code:

 
When the welcome.txt file does not exist, the file not found is output in the browser and the subsequent script is terminated.

Custom error handling functions:

 
The code above defines an error handling function customError (). This function has two parameters. The first parameter indicates the error level, and the second parameter indicates the error information. It uses set_error_handler () the function sets the error handler function, and then triggers the error with echo $ test. The result of executing this script in the browser is: error level: [8], error msg: Undefined variable: test

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.