-
- //Check whether the user is logged in
- function checklogin(){
- if(emptyempty($_SESSION['user_info'])){ //Check whether the session is empty
- if(emptyempty ($_COOKIE['username']) || emptyempty($_COOKIE['password'])){ //If the session is empty and the user does not choose to record the login status
- header("location:login.php?req_url=" .$_SERVER['REQUEST_URI']); //Go to the login page, record the requested url, jump to it after logging in, and have a good user experience.
- }else{ //The user has chosen to remember the login status
- $user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //Get the user's personal information
- if(emptyempty($ user)){ //The username and password are incorrect and the information is not available. Go to the login page
- header(”location:login.php?req_url=”.$_SERVER['REQUEST_URI']);
- }else{
- $_SESSION ['user_info'] = $user; //If the username and password are correct, put the user's personal information into the session
- }
- }
- }
- }
Copy code Instructions:
When accessing every page in the background, you must first perform the above checks
2. User submits login information
When the user fills in the username and password, it is submitted here.
/** - Detection of login information submitted by users
- link: http://bbs.it-home.org
- */
- $username = trim($_POST['username']);
- $password = md5(trim($_POST[ 'password']));
- $validatecode = $_POST['validateCode'];
- $ref_url = $_GET['req_url'];
- $remember = $_POST['remember'];
- < ;p>$err_msg = ”;
- if($validatecode!=$_SESSION['checksum']){
- $err_msg = “Verification code is incorrect”;
- }elseif($username==” || $password== ”){
- $err_msg = “Neither username nor password can be empty”;
- }else{
- $row = getUserInfo($username,$password);
if(emptyempty($row )){
- $err_msg = "Both the username and password are incorrect";
- }else{
- $_SESSION['user_info'] = $row;
- if(!emptyempty($remember)){ //If the user chooses, To record the login status, put the username and encrypted password in the cookie
- setcookie("username", $username, time()+3600*24*365);
- setcookie("password", $password, time() +3600*24*365);
- }
- if(strpos($ref_url,"login.php") === false){
- header("location:".$ref_url);
- }else{
- header(" location:main_user.php”);
- }
- }
- }
- $username = trim($_POST['username']);
- $password = md5(trim($_POST['password']));
- $validatecode = $_POST['validateCode'];
- $ref_url = $_GET['req_url'];
- $remember = $_POST['remember'];
$err_msg = ”;
- if ($validatecode!=$_SESSION['checksum']){
- $err_msg = "Verification code is incorrect";
- }elseif($username==" || $password=="){
- $err_msg = "Username and The password cannot be empty";
- }else{
- $row = getUserInfo($username,$password);
if(empty($row)){
- $err_msg = "Username and The passwords are not correct";
- }else{
- $_SESSION['user_info'] = $row;
- if(!empty($remember)){ //If the user chooses, the username and password will be recorded and the login status will be recorded Put the password in the cookie
- setcookie("username", $username, time()+3600*24*365);
- setcookie("password", $password, time()+3600*24*365);
- }
- if(strpos($ref_url,"login.php") === false){
- header("location:".$ref_url);
- }else{
- header("location:main_user.php");
- }
- }
- }
-
Copy code
Explanation about $ref_url:
Suppose: User A accesses b.php, but User A is not logged in, jumps to the login page login.php, fills in the user and password on the login page, and then jumps to the page b.php after confirmation, instead of jumping A default page main_user.php.
Because b.php is the page that user A wants to access, the user experience will be better.
3. When the user logs out, clear the record login status
When the user clicks to log out, be sure to clear the current login status to prevent malicious people from using your login information to cause damage.
-
- //Log out
- function logout(){
- unset($_SESSION['user_info']);
- if(!emptyempty($_COOKIE['username']) || emptyempty ($_COOKIE['password'])){
- setcookie("username", null, time()-3600*24*365);
- setcookie("password", null, time()-3600*24*365) ;
- }
- }
- ?>
Copy code
Okay, I’ve introduced the method of remembering passwords and automatically logging in using php. I hope you gain something.
Scripting School is dedicated to you.
|