Jump to content

Recommended Posts

I need a simple php script that goes in my MYSQL database every 2 hours and changes data in a row. Like if i have a row called "logged_in" and every time a user logs in it puts a 1 in that row. If the account is attempted to be logged in from another place and the 1 is still there, it denies access. So essentially it prevents two people being on at the same time. But the user closes the browser and forgets to log out. The 1 is still in the database. So i need a script that every 2 hours goes through and changes all the 1s to 0s in the row "logged_in"

 

any help please?

Well it sounds like you have a solid battle plan.  Break it down and do it step by step.  Basic database handling, logging time last logged in, run a cron job on the script to execute every 2 hours.  So hop to it sir.  If you get stuck on some coding let us know, we'll try to help you out.  If you are wanting someone to write it for you, make a post in the freelance forum making an offer.

From what you say, I thought about it this way.

 

  • Make a SQL statement altering the "logged_in" column when the user logs in. Set the default as 1.
  • "If the account is attempted to be logged in from another place and the 1 is still there" - From that i would log their IP or something like that (I don't know how to log someone's IP dont ask, lol) then if their not from that IP, then deny their access.
  • "every 2 hours goes through and changes all the 1s to 0s" - This is just me saying it. Set a timer at certin times to alter that field at specific times to go to a 0.

If you want to run a script every two hours on a *nix platform (I'm not familiar with IIS) you need to use the crontab.  If your hosting provider doesn't allow you access to your crontab then you may want to consider switching.

 

However, I'm not sure your approach to session handling is on the money.  If you run a script every two hours to change the 'logged_in' field to 0... and I logged in five minutes before the cron script ran then I'll be logged out for no apparent reason.  I would think about a different approach to session handling.

Why don't you just put a timestamp in the database for when the user logs in?  And when someone logs in see if its over 2 hours?  If it is, reset your login bit, if not, dont.  I dont really see the need for a cron job for this.

http://www.webcron.org/ ..

 

Some language i don't know ... but runs cron jobs for you ... maybe further searching could find an english version ... but just shy of hitting a page yourself every 2 hours or some demented javascript page that you have to keep open all the time this is the only option i can see for you dilema of not being able to set up a cron job

actually i have a time stamp go in when they log out. so to make one when they log in, would i put the following code in this line?

 

time stamp code:

$time = time();

timeout = NOW()

 

that is in my logout.php

now to add it to my login

does it go in here?

 

$sql="SELECT logged_in FROM $tbl_name WHERE username='$myusername' and password='$mypassword' AND activated='1'";

$result=mysql_query($sql);

 

Yeah, you're on the right track now, I'd say.  You're authentication algorithm could be something like this:

 

On all password-protected pages:

- check to see if they're logged in

  - if they are, check the last time they logged in

    - if that was more than two hours ago, log them out and send them to the login page

  - if they aren't, send them to the login page

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.