Hi,

I have the following PHP code.

<?

session_start();

if($_POST['submit'] == "submit")
{
        $_SESSION["KB_username"] = $username;
        echo "Your Session username=" . $_SESSION["KB_username"];
        echo "<HR>";
        echo session_id();
}

if($_POST['logout'] == "logout")
{
        session_unset();
//        session_destroy();
}


?>

<html>
<head></head>
<body>

<form name="login" action="<?php $_SERVER["PHP_SELF"] ?>"  method="post">
<input type="text" name="username" size="20">
<br>
<input type="submit" name="submit" value="submit">
</form>
<hr>
<?php echo $_SESSION["KB_username"]; ?>
<hr>
<form name="logout" action="<?php $_SERVER["PHP_SELF"] ?>"  method="post">
<input type="submit" name="logout" value="logout">
</form>

</body>
</html>

If I try to login to the application on the same PC using 2 instances of a browser, the session variables clash.
That is if I logout user-1, user-2's session also gets logged out... Pls help how do I solve this.

I have tried using & hashing session_destroy();

End result:
I would like a user on a computer to be able to login to the application using 2 different username and have individual sessions. If the user decides to logout as user-1, it should not logout user-2
I hope this is not very confusing. I am searching google since a long time and no solution.....

Pls help!

Thx
Vai

    You can log in multiple users(I dont know why you want to do it). You just have to put logged users to array and when you logout some user, its deleted from the array.

    Session is the same if you have 2 firefoxes(or IE's or Operas or Google Chrome or ...) and all opened browsers would have the same session_id. To have separated session_id, you would have to open different browsers (for example 1 FF, 1 Opera).

    Anyway.. Heres a quickly done example of multiple logins:

    <?php
    
    session_start();
    
    if(isset($_POST['login']))
    {
    	if (!isset($_SESSION['logged_in_users']))
    	{
    		$_SESSION['logged_in_users'] = array();
    	}
    
    if (!empty($_POST['username']) && !in_array($_POST['username'], $_SESSION['logged_in_users']))
    {
    	$_SESSION['logged_in_users'][] = $_POST['username'];
    }
    }
    
    if(isset($_POST['logout']) && isset($_POST['user']))
    {
    	$delkey = array_search($_POST['user'],$_SESSION['logged_in_users']);
    	unset($_SESSION['logged_in_users'] [$delkey]);
    }
    
    
    ?>
    
    <html>
    <head></head>
    <body>
    <?php if (isset($_SESSION['logged_in_users'])): ?>
    
    <h3>Logged in users</h3>
    <pre><?php print_r($_SESSION['logged_in_users']); ?></pre>
    
    <?php endif; ?>
    <h3>Login</h3>
    <form name="login" action=""  method="post">
    <input type="hidden" name="login" value="1">
    <input type="text" name="username" size="20">
    <br>
    <input type="submit" name="submit" value="submit">
    </form>
    <hr>
    <?php
    if (isset($_SESSION['logged_in_users'])):
    	foreach ($_SESSION['logged_in_users'] as $user): ?>
    		<form name="logout" action=""  method="post">
    		<input type="hidden" name="user" value="<?php echo $user ?>">
    		<input type="submit" name="logout" value="logout <?php echo $user ?>">
    		</form>
    	<?php
    	endforeach; 
     endif; ?>
    </body>
    </html>
    

      @
      Thx. This seems to be working the way I want... I will test it with my scripts.
      Can you guide me on how to store additional session variables for each user ?
      example: $SESSION['aaa'], $SESSION['bbb'], $_SESSION['ccc'],
      Thx again for the help.

      Somewhere I had read of using session_id in the url will help me achieve what I need.
      But I am not sure how to use it.... A learner at all this. If you can direct me to some tutorial specifically explaining how to use session_id in url to help maintain multiple browser instances, it will be great. I did google around, but all the php session tutorials dealt with regular create, destroy, etc functions.

      cheers,
      Vai

        Write a Reply...