Learn about the Session's session function through the case of the login and logout functions of the student login form

Table of contents

I. Introduction

2. The function of this article and the rendering of the student login form

3. Code part

1. bd01 student login form

2.bd02 judge whether the form value is correct

3.bd03 judge whether the student information is successfully logged in

4. bd04 log out and log in again

4. Web page publishing and running link

5. Running results

1. DW coding interface

2. Display the result when the student ID or password is empty 

3. When the student ID or password is wrong, the result will be displayed 

4. Successful login interface


I. Introduction

1. This article introduces PHP code exercises. I use the code written by Adobe Dreamweaver2021 software. In principle, other software such as HBX, VSCode or DW are compatible with lower and higher versions. If you need it and the writing software is not DW, etc. software, please paste the content in <?php?>;

2. This is the code I wrote during school and online learning. If it violates, please delete it!

3. In principle, the code is correct, but there may be logical errors in some places, please understand, and please give me feedback on the existing problems, I am learning lessons, improving the program, and continuing to improve;

4. The following codes are examples, if necessary, you can rewrite them yourself;

5. To use PHP code alone, please paste the content in <?php ?>;

6. This is a blog post that needs to be published and run. I use Google Chrome, and Google Chrome is recommended;

7. This code is divided into 4 programs, which are named after bd01/bd02/bd03/bd04 respectively, and the folder is named after the form case. If you paste it, please be optimistic about the file name you created. At the same time, the release link also needs your own consistent;

提示:以下是本篇文章正文内容,下面案例可供参考

2. The function of this article and the rendering of the student login form

This article is written in PHP code, and uses the session function of Session to complete a student login form. This form realizes the login of relevant information of students, as well as the restrictions on login conditions, and functions such as returning to the login interface after login, and logging out. The code is divided into 4 parts. It mainly implements the student login form, judging whether the value of the form is correct, judging whether the student information is successfully logged in, logging out, and logging in again. The following is the rendering of the student login form:

3. Code part

1. bd01 student login form

The code is as follows (example): 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>学生登录表单</title>
</head>

<body>
    <form action="bd02.php" method="POST">
            <fieldset>
                <legend>学生登录表单</legend>
                <p>
                    学号:<input type="text" name="xh"/>
                </p>
                <p>
                    密码:<input type="password" name="mm"/>
                </p>
                <p>
                 性别:<input type="radio" value="男" name="sex" checked>男
                     <input type="radio" value="女" name="sex">女<br>
                </p>
                <p>
                    学院:<select name="se1[]" size="3" multiple>
                    <option value="信息学院">信息学院</option>
                    <option value="机电学院">机电学院</option>
                    <option value="土木学院">土木学院</option>
                    <option value="财经学院">财经学院</option>
                    </select><br>
                </p>
                <p>
                    专业:<select name="se">
                     <option value="软件技术">软件技术</option>
                     <option value="网络技术">网络技术</option>
                     <option value="大数据技术">大数据技术</option>
                     <option value="虚拟现实技术">虚拟现实技术</option>
                     <option value="人工智能技术">人工智能技术</option>
                     </select><br>
                </p>
                <p>
                    班级:<input type="radio" value="202101" name="class">202101
                     <input type="radio" value="202102" name="class">202102
                     <input type="radio" value="202103" name="class">202103
                     <input type="radio" value="202104" name="class">202104
                     <input type="radio" value="202105" name="class">202105
                </p>
                <p>
                    <input type="submit" value="登录" name="btn"/>
                    <input type="reset" value="重置" name="btnr"/>
                </p>
            </fieldset>		
        </form>
</body>
</html>

2.bd02 judge whether the form value is correct

The code is as follows (example): 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>判断表单数值是否正确</title>
</head>
<body>
<?php
 	session_start();
 	if (isset($_POST['btn'])){
		$xh = trim($_POST['xh']);
		$mm = trim($_POST['mm']);
		if (($xh == '') || ($mm== ''))
        {
 			header('refresh: 3; url=bd01.php');
 			echo "您输入的学号或密码不能为空,请等待3秒,网页自动跳转到登录页面";
		}
        else if  (($xh != 'csdn') || ($mm != '1024'))
        {
 			header('refresh: 3; url=bd01.php');
 			echo  "您输入的学号或密码错误,请等待3秒,网页自动跳转到登录页面!";
 		}
        else if  (($xh == 'csdn') && ($mm== '1024'))
        {  
			$_SESSION['xh'] = $xh; 
			  header("location:bd03.php");
		} 
	}
?>
</body>
</html>

3.bd03 judge whether the student information is successfully logged in

The code is as follows (example): 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>判断学生信息是否登录成功</title>
</head>
<body>
<?php
 	session_start();
 	if (isset($_SESSION['xh']))
    {
		echo $_SESSION['xh'] . ",您好,登陆成功,欢迎访问本网站! <br/> ";
        echo '<a href="bd04.php">退出登录</a>';
 	} 
    else
    {
		header("location:bd02.php");
 	}
?>
</body>
</html>

4. bd04 log out and log in again

The code is as follows (example): 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>退出登录,重新登录</title>
</head>
<body>
<?php
session_start();
$xh = $_SESSION['xh'];
$_SESSION = array();
session_destroy();
echo "$xh,谢谢光顾本网站,欢迎下次再见!<br/>";
echo "<a href='bd01.php'>重新登录</a>";   
?>
</body>
</html>

4. Web page publishing and running link

This code is not easy to use directly in the software preview, you need to start Apache, publish and run it on the web page, it is recommended to use Google Chrome

(Note: Please change your name if it is different from mine)

http://localhost/phptest/%E8%A1%A8%E5%8D%95%E6%A1%88%E4%BE%8B/bd01.php

5. Running results

1. DW coding interface

The displayed results are as follows (example): 

2. Display the result when the student ID or password is empty 

The displayed results are as follows (example): 

3. When the student ID or password is wrong, the result will be displayed 

The displayed results are as follows (example):

 

4. Successful login interface

The displayed results are as follows (example):

5. Exit interface

The displayed results are as follows (example):

 

6. Re-login interface

The displayed results are as follows (example):

 

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129912009