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>
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='text/javascript'>window.location='index.php'; </script>"; } else { echo"<script type='text/javascript'> window.location='error.php'; </script>"; } ?>
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>
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)!