开发者

login/logout problem in PHP

开发者 https://www.devze.com 2023-01-01 16:43 出处:网络
i have a question. I have a website in which i am giving security like login id and password( as usual). No what i want is that,

i have a question. I have a website in which i am giving security like login id and password( as usual). No what i want is that, 1) I don't want to allow a single user to login in different machine at the same time. 2) For this i am using a column in database which is keeping the current status of user(i.e. loging/logout). I am allowing user to login only when has session has not closed and status is login. 3) So my problem is that when i am logging out manually. it is closing the session as well as updating the database with status "logout". 4) but when i am closing the window from Cross buttonat top right corner. it is closing the ssion but table data is still "login". so later on i can't be able to login into the same user. 5) So how could 开发者_运维问答i solve this problem. Please help me!


Do it another way.
Don't forbid access. Just manually log the previous one out.
Like ICQ or other smart enough messengers do.

To do so, keep the session id in the user's table. Once it got changed - prompt for login.


1) I don't want to allow a single user to login in different machine at the same time.

Every time a user tries to login, check if a session has already been set. You can do this by saving a hash of a unix time stamp to the database that is associated with the user as well as set the hash to the session every time a user logins. Once the user logins again, you'll create a new hash which will overwrite the old hash in the database.

Then in all your protected files, you can compare the session hash variable to the one in the database. If it doesn't match, log the user out- but the new login instance will still stay active.

2 + 3 + 4) For this i am using a column in database which is keeping the current status of user(i.e. loging/logout). I am allowing user to login only when has session has not closed and status is login. So my problem is that when i am logging out manually. it is closing the session as well as updating the database with status "logout".but when i am closing the window from Cross buttonat top right corner. it is closing the ssion but table data is still "login". so later on i can't be able to login into the same user.

For this, you have to save a last active unix time stamp. You will also have to come up with amount of time before you "logout" the user- this is usually an hour or 3600 seconds. Either using a cron job or an include file on certain pages, you can check the times for the users and compare them doing if(time - last_active_time > 3600){your_logout_func();}

5) So how could i solve this problem. Please help me!

Doing that above should fix your problems, but of course there are many ways to solve it :)


you can try something like this:

<html>
<head>
<script type="text/javascript">
function logout(){
window.open("http://www.yoursite.com/logout.php","_blank");
}
</script>
</head>

<body onunload="logout()">
</body>

</body>

</html> 


You could also have a cron job running that will periodically check your user's status. For instance, if the last time the user "did something" is too far away, then you could have your script log the user out.

This would involve tracking the user's status as well...maybe a simple table that checks page views.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号