Home  >  Article  >  Backend Development  >  A simple way to implement user login in php

A simple way to implement user login in php

墨辰丷
墨辰丷Original
2018-05-24 10:36:111865browse

This article mainly introduces relevant information on simple examples of user login in php. Friends who need it can refer to

Simple example of user login in php

Foreword:

The last part to be completed recently is to manage the data submitted by users. As for management, it must be the administrator's business, which must involve logging in and verifying account permissions. , whether the account has expired, etc.

Required knowledge

session is indeed a very important thing. And I encountered that the session could not cross pages, so I modified the session.use_trans_sid = 0 value of PHP.ini to 1.

Detailed implementation

My background design is relatively simple. You only need to enter a password. This password is of course saved on the server and can be changed. So you only need to add two variables to the session, flag and time.

First, use flag to determine whether the administrator has successfully logged in, and use time to determine whether the login has timed out. After submitting the password, if it is correct, the flag will be assigned a value of 1, and the time will be assigned the current time. Each time you enter a new page or perform an operation, these two variables will be judged. First, judge whether the flag value is 1. If it is not unique, it will directly prompt that you are not logged in and destroy the session. If it is 1, then judge the current time -$_SESSION(' time') is less than 600 (10 minutes). If it is greater, the login timeout will be prompted and the session will be destroyed; if it is less, the operation will be allowed and the time variable value will be updated to the current value.

Part of the code

check_pw.php

<?php
session_start();
$_SESSION[&#39;flag&#39;] = 0;
$myfile = fopen("passwd","r") or die("Unable to open file!");
$passwd = fgets($myfile);
if(empty($_POST[&#39;pass&#39;])){
  echo "不能为空,重新输入";
  $page = "login.html";
}else{
  $pass = $_POST[&#39;pass&#39;];
  $passwd = test_input($passwd);
  $pass = test_input($pass);
  if($pass == $passwd){
    echo "口令正确,允许访问";
    $page = "list_all.php";
    $_SESSION[&#39;flag&#39;] = 1;
    $_SESSION[&#39;time&#39;] = time(); //当前秒数
  }else{
    echo "口令错误,重新输入";
    $page = "login.html";
  }
}

function test_input($date){
  $date = trim($date);
  $date = stripcslashes($date);
  $date = htmlspecialchars($date);
  return $date;
}
?>
  <a href="<?php echo $page;?>" rel="external nofollow" >点此跳转</a>

list_all.php(Part )

<?php
session_start();
if($_SESSION[&#39;flag&#39;] == 1){
  if(time() - $_SESSION[&#39;time&#39;] > 600){
    echo "登陆超时";
    echo &#39;<a href="login.html" rel="external nofollow" rel="external nofollow" >点此登陆</a>&#39;;
    session_destroy();
    exit();
  }else{
  $_SESSION[&#39;time&#39;] = time();
  }
}else{
  echo "未登陆,无权访问!";
  echo &#39;<a href="login.html" rel="external nofollow" rel="external nofollow" >点此登陆</a>&#39;;
  session_destroy();
  exit();
}
?>

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php mysql develops the simplest online question bank and online question making system

PHPDifferentiate enterprise WeChat browser | Ordinary WeChat browser | Other browsers

##PHPtwo Dimensional array deduplication based on a certain element

The above is the detailed content of A simple way to implement user login in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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