PHP learning officially sets sail (5)

黄舟
Release: 2023-03-04 11:36:01
Original
1010 people have browsed it

Now let’s start the road of mixing php and html
php files can embed html code, but html files cannot embed php code, because html is a static file

Let’s talk about PHP forms and user input

PHP's $_GET and $_POST are used to obtain the value submitted by the form

Create a new php file index.php

<html>
<body>
<form action="index.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Copy after login

Now it is still a pure html code form Submit to yourself, the submission method is post

Now add the php code

<html>
<body> <form action="index.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
<?php
echo "Name:".$_POST[&#39;name&#39;];
echo "Age:".$_POST[&#39;age&#39;];
?>
Copy after login


php can be added anywhere in the file, no need to be in the html tag
Finally What is printed is the result of your text box input

If the action is empty, it will be submitted to the file itself by default

$_POST['name']; You can use double quotes or single quotes, no Quotation marks can also be used (but it will remind you)
Post submission method cannot be obtained with $_GET

Use $_GET below to obtain the value of the form

<html>
<body> <form action="" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
Copy after login
<?php
echo "Name:".$_GET["name"];
echo "Age:".$_GET["age"];
?>
Copy after login

Why use $_GET?
When using $_GET variables, all variable names and values ​​will be displayed in the URL. So this method should not be used when sending passwords or other sensitive information. However, because the variables appear in the URL, you can bookmark the page. In some cases this is useful.
HTTP GET method is not suitable for large variable values; the value cannot exceed 100 characters.
Generally used for paging, detailed information display, etc.
POST is generally used for submitting data

There is also a $_REQUEST request, which represents the client's request

PHP's $ The _REQUEST variable contains the contents of $_GET, $_POST and $_COOKIE.
PHP’s $_REQUEST variable can be used to obtain the results of form data sent through the GET and POST methods.

<html>
<body> <form action="" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
Copy after login
<?php
echo "Name:".$_REQUEST["name"];
echo "Age:".$_REQUEST["age"];
?>
Copy after login

This can be obtained by either get or post submission method, but use it as little as possible. Because sometimes you have to confirm how the other party submitted it, it is better to distinguish it clearly

Let’s talk about PHP Session variables
When running an application, you will open it, make some changes, and then Close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state. PHP sessions solve this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database. The working mechanism of Session is to create a unique ID (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or passed through the URL.

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

<?php session_start(); ?>
Copy after login
<html>
<body>
</body>
</html>
Copy after login

session_start() function must be located before the tag

index.php page

<?php session_start(); ?> 

 
Name:
test.php
Copy after login

test.php page

<?php
session_start();
echo $_SESSION[&#39;name&#39;];
?>
Copy after login

The seesion value stored in the index page can be displayed on the test page
Whether it is stored or For output, each page needs session_start();
$_SESSION['name']; It can also be single or double quotation marks or not. It seems that this is the case in php. I won’t repeat it in the future.


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

<?php session_start(); ?>


Name:
test.php
Copy after login

Then when you go to test.php, you cannot output it

<?php
session_start();
if(isset( $_SESSION[&#39;name&#39;]))
echo $_SESSION[&#39;name&#39;];
else
echo "null";
?>
Copy after login

isset function checks whether a value is set (assigned), which is to determine whether a value is Empty

Say below
PHP Cookies

What are cookies? Cookies are often used to identify users. Cookies are small files that a server leaves on a user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.

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

<?php 
setcookie("user", "Hello world", time()+3600);
?>
Copy after login

We will create a cookie named "user" and assign it the value "Hello world". Specifies that this cookie will expire after one hour:

Where are cookies generally stored on the computer?
For IE browser, save it in
C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files

Temporary Internet Files folder
You will find that you txt file named php project, open it
and you can see the content. However, some of the content is encrypted, but the first half of
user
Hello+world
localhost/MyPHP/ can still be viewed. The

gets the cookie value

<html> 
<body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; 
else echo "Welcome New!<br />"; 
?> 
</body> 
</html>
Copy after login

$_COOKIE is the one that gets the cookie value


Looking at other languages, getting get, post, and cookie are all Using objects, PHP is obviously much simpler, although it is process-oriented



Let’s talk about PHP’s processing of files

First create it in the project root directory A file 1.txt content hello world

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

<?php
$file=fopen("1.txt","r");
?>
Copy after login

$file这个变量是个资源变量,表示文件打开的状态
关于资源变量以后还会接触

文件可能通过下列模式来打开: 模式描述
r 只读。在文件的开头开始。
r+ 读/写。在文件的开头开始。
w 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。
w+ 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。
a 追加。打开并向文件文件的末端进行写操作,如果文件不存在,则创建新文件。
a+ 读/追加。通过向文件末端写内容,来保持文件内容。
x 只写。创建新文件。如果文件已存在,则返回 FALSE。
x+ 读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。

如果 fopen() 无法打开指定文件,则返回 0 (false)。


打开文件还不够,接着打印

<?php
$file=fopen("1.txt","r");
$data="";
while(!feof($file)) 
{ 
$data.=fgets($file); 
} fclose($file); echo $data; 
?>
Copy after login

feof() 函数检测是否已达到文件的末端 (EOF)。在循环遍历未知长度的数据时,feof() 函数很有用。
fgets() 函数用于从文件中逐行读取文件。
在调用该函数之后,文件指针会移动到下一行。
fclose 关闭文件

另外fread函数也可以读取文件

<?php
$file=fopen("1.txt","r");
$data="";
while(!feof($file)) 
{ 
$data.=fread($file,4096); 
} fclose($file); echo $data; 
?>
Copy after login


fread() 从文件指针 handle 读取最多 length 个字节。该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况。 

fread与fgets的区别 
fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止. 
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用. 


其实还有文件写入,文件上传下载这些
暂时先简要介绍在这里,以后我接触的时候再说

以上就是php学习正式起航(5)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template