Creating one session for any user

Discussion in 'PHP' started by Om ji Kesharwani, Aug 28, 2010.

  1. #1
    If a user is already login then from that username he should be not able to login on another system from any where on the internet.
    I mean only one session should be created for users on my site.
     
    Om ji Kesharwani, Aug 28, 2010 IP
  2. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #2
    You could do it two ways, block the user from logging in a second time, or log off the first user if second user logs in.

    With the first method you would store the users timestamp of their last_action, to keep track of their online status. And not allow the user to login if they are already online. The problem with this method is if the user forgets to logout they will have to wait until their last_action reaches the timeout period (you set) before they could log in again.

    With the other method, you would store the session_id in the database and when the user logs in you would destroy all the previous session variables and set the new session.
     
    Last edited: Aug 28, 2010
    Narrator, Aug 28, 2010 IP
  3. HungryMindz

    HungryMindz Member

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    Hi!

    Try This Code: For Any Mistake Or BUG, I Apologize.

    $UserName = $_REQUEST["UserName"];
    $Password = $_REQUEST["Password"];

    $query = "select * from Users Where UserName = '$UserName' and Password = '$Password'";
    $result = mysql_query($query, $conn);
    $Found=mysql_num_rows($result);
    $Row=mysql_fetch_array($result);

    if($Found==1)
    {

    if($Row["OnlineStatus"] == "Offline")
    {
    $_SESSION["UserName"] = $Row["UserName"];
    mysql_query("UPDATE Users SET OnlineStatus = 'Online' WHERE UserName = '".$_SESSION["UserName"]."'");
    print "Logged In";
    } else {
    print "Already Logged In";
    }

    } else {
    print "Username Or Password Not Found";
    }

    Query When Logout:

    mysql_query("UPDATE Users SET OnlineStatus = 'Offline' WHERE UserName = '".$_SESSION["UserName"]."'");
     
    HungryMindz, Aug 28, 2010 IP
  4. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #4
    That would only work if everyone logs out, if someone forgets to logout they wont be able to log back in.
     
    Narrator, Aug 28, 2010 IP
  5. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I agree . Please describe second method(session id).
    Can u provide me some code help?
    Thank you
     
    Om ji Kesharwani, Sep 1, 2010 IP