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