Php login module function example, php implementation module example
This example describes how to implement the login module function of php. We will share this with you for your reference. The details are as follows:
I have been studying php recently. I learned something about login and wrote it down.
Create four pages named respectively:
Login. php
Check. php
Index. php
Error. php
The login page uses a form to create a login page. In the code, use a js script to determine whether the user name and password cannot be empty. If it is empty, reset the focus. 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 ("Enter the user name"); userName. focus (); return false;} if (userPwd. value = "") {alert ("Enter the user name"); userPwd. focus (); return false ;}}</script>
Check is to check the page. If the password and user name are correct, redirect to index. php; otherwise, 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 php built-in function used to record user login information. It is similar to a cookie, but it is different.
We can define and use sessions on the verification page, and then define and use sessions on the home page to welcome Mo. The code in the above check is already available. The code on the home page is as follows:
<? Session_start ();?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Verify that the user name and password are entered on the login page. If the user name and password are correct, they will jump to the home page to display a welcome user. If the user name and password are incorrect, the error page will be displayed.