Php effectively prevents multiple logins by the same user

Source: Internet
Author: User
This article mainly introduces how php effectively prevents multiple logins of the same account at the same time. if you are interested, refer [Problem description ]:It is dangerous for a single user to log on multiple times at the same time. Because, you cannot know if other users are logging on to your account. How Can I disable multiple logins from the same user?
[Solution]
(1) A new session_id is generated after the identity authentication is successful upon each login.

session_regenerate_id(); session_register ("username") ; 

(2) open a sessionid field in the user database. after the session_id is re-generated, the field is updated.

$sessionid = session_id(); $db = new PDO('sqlite:softToken.db'); $sql = "update userinfo set sessionid ='$sessionid' where username='$username' and passwd='$passwd';"; $query = $db->prepare($sql); $query->execute(); 

(3) Create a session to save the user name

$_SESSION["username"] = $username; 

(4) use url rewriting to pass session_id

$ Url = "main. php? Sid = ". session_id (); unset ($ db); echo" login successful, jump! "; Header (" Location: $ url ");

(5) add
Main. php

<? Php header ('content-type: text/html; charset = utf-8 '); $ sessionid = $ _ GET ['Sid']; session_id ($ sessionid); session_start (); $ username = $ _ SESSION ["username"]; $ db = new PDO ('sqlite: softToken. db'); $ SQL = "select * from userinfo where username = '$ username' and sessionid =' $ sessionid ';"; $ query = $ db-> prepare ($ SQL); $ query-> execute (); $ user = $ query-> fetch (PDO: FETCH_OBJ ); if ($ user-> username = "") {session_destroy (); echo"

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.