Detailed introduction to the example of php implementing the login module function

黄舟
Release: 2023-03-06 07:38:02
Original
1507 people have browsed it

The example in this article describes how to implement the login module function in PHP. Share it with everyone for your reference, the details are as follows:
I am learning PHP recently. I learned a little bit about logging in and wrote it down as a memo.
Create four new pages, named respectively:
login.php
check.php
index.php
error.php

login page Use a form to create a landing page, not much to say. Use js script in the code to determine that the username and password cannot be empty. If they are empty, the focus will be reset. The code is as follows:

<script type="text/JavaScript">
function jc()
{
 var userName=document.getElementById("userName");
 var userPwd=document.getElementById("userPwd");
 if(userName.value=="")
 {
 alert("请输入用户名");
 userName.focus();
 return false;
 }
 if(userPwd.value=="")
 {
 alert("请输入用户名");
 userPwd.focus();
 return false;
 }
}
</script>
Copy after login


check is a check page. If the password and username are correct, it will redirect to index.php, otherwise it will redirect to the error page. The code is as follows:

<? session_start();
 $userName=$_POST["userName"];
 $userPwd=$_POST["userPwd"];
 if($userName=="admin"&&$userPwd=="123456")
 {
 $_SESSION["userName"]=$userName;
 echo "<script type=&#39;text/javascript&#39;>window.location=&#39;index.php&#39;;
</script>";
 }
 else
 {
 echo"<script type=&#39;text/javascript&#39;>
window.location=&#39;error.php&#39;;
</script>";
 }
?>
Copy after login

Finally, let’s talk about session verification. The session function is a function that comes with PHP and is used to record the user's login information. It is similar to cookies, but different.

We can define and use session on the verification page, and then define and use it again on the homepage to achieve the effect of welcoming Mo. The code in the above check is already there. Here is the code in the homepage:

<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
欢迎<? echo $_SESSION["userName" ]; ?>来到这里
</body>
</html>
Copy after login



Verify it. Enter the user name and password on the login page. If correct, it will Jump to the home page and display welcome so-and-so. If there is an error, it will jump to the error page and display the error.

The above is the detailed introduction of the example of the login module function in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!