PHP to implement a member account can only login code instances

Source: Internet
Author: User
This article brings the content is about the PHP implementation member account can only login code example, there is a certain reference value, the need for friends can refer to, I hope to help you.

Scene reproduction

The same member account limit on the same device (computer, mobile phone, ipad, etc.) on a single sign-on, repeated login, the original login to access the page directly to the login page and need to re-login to normal access.

Principle Analysis

A account on a computer login, a account at this time again with B computer login again, a computer request page, prompt "re-login" information, and jump to the login page

Thinking Analysis:

A account is logged on a computer, the session ID is written to the TXT file, each time you visit the page, do a check (compare the value of the TXT file is the same as the value of the current login session_id)

Instance

1. Entrance and calibration (index.php)

<?phpsession_start (); $uid = 10;if (Is_login ($uid)) {header (' location:loginSuccess.php ');} else{  $getLogSessionId = file_get_contents (' session_log/'. $uid. TXT ');  if ($getLogSessionId) {    echo "has been logged in elsewhere, please login again";    echo "<br>";    echo "Jump to login page after 5 seconds!";    Header ("refresh:5;url=login.php");  } else{      echo ' first login ... ';      echo "<br>";      echo "Jump to the login page after 3 seconds!";    Header ("refresh:3;url=login.php");}  } /*** is compared to the session_id generated by writing a file at login session_id and the current login device, the same device is logged in, and the other one is logged in */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)

<?phpsession_start (); echo "Login ...."; Echo ' <br> '; $uid = 10;file_put_contents (' session_log/'. $uid. TXT ', session_id ()); echo "Login Complete";

3. Successful Login (loginsuccess.php)

<?phpecho "I am logged in successfully!";

4, Save SessionID folder (session_log), test code, need to manually build one or give automatically generated file permissions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.