Analysis on the Method of Unique Login of Member Account List by PHP

  • 2021-11-29 06:28:11
  • OfStack

In this paper, an example is given to describe the method of PHP to realize the login of member account. Share it for your reference, as follows:

Scene reappearance

The account number of the same member is limited to single sign-on on the same device (computer, mobile phone, Ipad, etc.). After repeated login, when logging in to access the page, it will jump directly to the login page and need to log in again to access normally.

Principle analysis

After the A account is logged in on the A computer, the A account is logged in again on the B computer. When the A computer requests the page, it will prompt the message of "login again" and jump to the login page

Thinking analysis:

After the A account is logged in on the A computer, write SESSION ID into the TXT file, and check once every time you visit the page (compare whether the value of the txt file is the same as that of the currently logged-in session_id)

Instances

1. Entry and check (index. php)


<?php
session_start();
$uid = 10;
if(is_login($uid)){
 header('location:loginSuccess.php');
}else{
 $getLogSessionId = file_get_contents('session_log/'.$uid.'.txt');
 if($getLogSessionId){
  echo " You have logged in elsewhere, please log in again ";
  echo "<br>";
  echo "5 Jump to the login page in seconds !";
  header("refresh:5;url=login.php");
 }else{
  echo ' No. 1 1 Secondary login ...';
  echo "<br>";
  echo "3 Jump to the login page in seconds !";
  header("refresh:3;url=login.php");
 }
}
/**
*  By writing to a file at login session_id And the current login device session_id Compare, the same is the same 1 Log in to a device, but log in somewhere else if it is different 
*/
function is_login($uid){
 $getLogSessionId = file_get_contents('session_log/'.$uid.'.txt');
 if($getLogSessionId == session_id()){
  return true;
 }else{
  return false;
 }
}

2. Login (login. php)


<?php
session_start();
echo " In login ....";
echo '<br>';
$uid = 10;
file_put_contents('session_log/'.$uid.'.txt',session_id());
echo " Login complete ";

3. Successful login (loginSuccess. php)


<?php
echo " I successfully logged in !";

4. Save the sessionId folder (session_log) and test the code. You need to build one manually or give the permission to automatically generate files

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Summary of PHP Error and Exception Handling Methods", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: