PHP effectively prevents the same user from logging in _php tips

Source: Internet
Author: User

"Problem description": the same user logged on multiple times at the same time if it is not detected, it is dangerous. Because, you can't know if any other users are logged in to your account. How do I prevent the same user from logging on multiple times?
"Solution"
(1) Each login, the success of identity authentication, the session_id of a new generation.

SESSION_REGENERATE_ID (); 
Session_register ("username"); 

(2) A SessionID field is opened in the user database, and the field is updated after the session_id is generated.

$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) Establish a session to save the user name

$_session["username"] = $username; 

(4) Use URL rewrite, pass session_id

$url = "main.php?sid=". session_id (); 
Unset ($db); 
echo "<font color=blue> login success, is jumping!</font>"; 
Header ("Location: $url"); 

(5) in the need to jump to the page, at the beginning of the join
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 "<script language= ' javascript ' type= ' text/javascript ' >"; 
echo "window.location.href = ' index.html ';"; 
echo "</script>"; 
Exit (); 
> 
 
 
 

The above is a valid PHP to prevent the same account number of times the same time to log on the solution, I hope to solve the same account at the same time multiple login problems help.

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.