Php learning officially started (5)

Source: Internet
Author: User
Tags php session php form
Now, php and html can be embedded into php code, but html files cannot be embedded into php code. because html is a static file, it is now the way to mix php and html.
Php files can be embedded with html code, but html files cannot be embedded with php code, because html is a static file.

PHP form and user input

PHP $ _ GET and $ _ POST are used to obtain the value of form submission.

Create a new php file index. php

 

Now, the html code form is submitted to itself. the submission method is post.

Add php code now

  
 


Php can be added to any part of the file without being added to the html tag.
The final result is the result of your text box input.

If the action is empty, it is submitted to the file by default.

$ _ POST ['name']; double quotation marks can be used for single quotation marks, without quotation marks (but attention will be reminded)
The post submission method cannot be obtained using $ _ GET.

Use $ _ GET to obtain the value of the form.

  
 

Why use $ _ GET?
When $ _ GET is used, all variable names and values are displayed in the URL. Therefore, this method should not be used when sending passwords or other sensitive information. However, because the variables are displayed in the URL, you can add the page to favorites. In some cases, this is useful.
The http get method is not suitable for large variable values. The value cannot exceed 100 characters.
Usually use pagination, detailed information display, etc.
POST is generally dedicated for data submission.

There is also a $ _ REQUEST, indicating the client REQUEST

The $ _ REQUEST variable in PHP contains $ _ GET, $ _ POST, and $ _ COOKIE content.
The $ _ REQUEST variable of PHP can be used to obtain the results of form data sent through the GET and POST methods.

  
 

This can be obtained in both get and post submission methods, but it should be used as little as possible. Because sometimes you have to confirm the method submitted by the other party, it is better to clearly distinguish

Let's talk about PHP Session variables.
When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained. By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database. The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.

Before using session, you must start the session, which is different from other languages.

 
 

The session_start () function must be located inBefore tag

Index. php page

    test.php 
 

Test. php page

 

The seesion value stored on the index page is displayed on the test page.
Session_start () is required for every page, whether stored or output.
$ _ SESSION ['name']; it can also be a single double quotation mark or not added. it seems that this is the case in php and will not be repeated in the future.


If you want to delete some session data, you can use the unset () or session_destroy () function.

 Test. php 
 

Then the output will not be available in test. php.

 

The isset function checks whether a value is set (assigned value) and determines whether a value is null.

Next
PHP Cookies

What is Cookie? 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.

How to create a cookie? The setcookie () function is used to set the cookie.
The setcookie () function must be locatedBefore the tag.

 

We will create a cookie named "user" and assign it "Hello world ". This cookie expires in one hour:

Where are cookies stored on your computer?
In the IE browser
C: \ Documents Ents and Settings \ Administrator \ Local Settings \ Temporary Internet Files

Temporary Internet Files folder
You will find a txt file named after your php project, open it
You can see the content, but some content is encrypted, but the first half
User
Hello + world
Localhost/MyPHP/can still be seen

Get cookie value

  
 "; else echo "Welcome New!
"; ?>

$ _ COOKIE: the cookie value is obtained.


In other languages, get, post, and cookie are all objects. php is much simpler, although it is process-oriented.



The following describes how php processes files.

First, create a file 1.txt in the project root directory with the content hello world

The fopen () function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file

 

$ File is a resource variable, indicating the open state of the file.
Resource variables will be exposed later

The file may be opened in the following mode: mode description
R read-only. Start at the beginning of the file.
R + read/write. Start at the beginning of the file.
W write only. Open and clear the file content. if the file does not exist, create a new file.
W + read/write. Open and clear the file content. if the file does not exist, create a new file.
A append. Open and write to the end of the file. if the file does not exist, create a new file.
A + read/append. Write content to the end of the file to keep the file content.
X write only. Create a new file. If the file already exists, FALSE is returned.
X + read/write. Create a new file. If the file already exists, FALSE and an error are returned.

If fopen () cannot open the specified file, 0 (false) is returned ).


It's not enough to open the file and print it.

 

The feof () function checks whether it has reached the end of the file (EOF ). The feof () function is useful when data with unknown length is traversed cyclically.
The fgets () function is used to read objects row by row from objects.
After this function is called, the file pointer is moved to the next row.
Fclose close file

The fread function can also read files.

 


Fread () reads a maximum of length bytes from the file pointer handle. This function can be read by a maximum of length bytes, or when it reaches the EOF, or (for network streams) when a package is available, or (after opening the user space stream) when 8192 bytes have been read, the system stops reading files.

Difference between fread and fgets
Fread: calculates the length in bytes and reads data according to the specified length and number of times. it stops reading data at the end or after the specified length is completed.
Fgets: Read the entire line. if a carriage return returns a line break or the end is stopped, use it in text mode.


In fact, there are also file writing, file upload and download these
Here is a brief introduction for the time being. I will discuss it later.

The above is the official start of php learning (5). For more information, please follow the PHP Chinese website (www.php1.cn )!

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.