0

I have a table with users. I want the "status" value of this user to change after some period of time (In months (from 1 to 12)).

For example: A new row is added with the Name = Jack, Age = 20, Status = enabled, Expiration = 6.

All of this values are sent from a PHP script, when the user registers. A user selects for how many months he wants to be enabled on the website. From 1 to 12. This value is passed into the database (as in example). This user's status should be changed to disabled after 6 months (Or what ever he has chosen ("Expiration" value)) automatically.

I have read about different techniques that can handle this:

1) Cron (But I guess it can't be handled on the local server for testing and it requires cPanel. Also I am not sure if I can easily send the expiration value to cron (Please tell me if I am wrong))

2) Set up the event scheduler (I am not sure if it is efficient (Please tell me if I am wrong))

3) Calculation in the PHP script (I guess in this case, if some one with the incorrect time will visit my application, the system will automatically change the status of all of the rows as it looks at the visitors time on their computer (Please tell me if I am wrong))

So basically I am asking "What is the best way to handle this operation in this particular case?" Can you please tell your thoughts about this. I am looking for the most efficient way (As I will probably have a lot of users). Please correct me where I was wrong and I am open to your suggestions. Thank you.

3
  • I think the third option is the easiest one... Commented Jul 30, 2015 at 7:15
  • Easiest, but the most efficient? Wouldn't everything be screwed up by some person with the incorrect time on their computer? Commented Jul 30, 2015 at 7:18
  • 2
    @Jay no, because you always should use the server-time not the client-time. Commented Jul 30, 2015 at 7:19

5 Answers 5

4

I think PHP script option suites here(option 3).

you can check for validation at the time of user log in, if that pass the time change the status in database and show user an error.

Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't everything be screwed up by some person with the incorrect time on their computer?
@Jay no, because you always should use the server-time not the client-time.
@php-coder well, if he only needs the status update when the specific user logs in by himself, then this is the best solution. but this doesn't update the user-status as long as s/he doesn't log in.
@ jay, as low_rents say every thing must done from your server itself as database is in your system. nothing should be from client side except log in..
1

I would attack this problem like this:

add expiration date column un user table
add there timestamp to what datetime account should be valid.

When user is logging in, PHP is checking the 'epxiration' 
column and check if datetime value is still valid.

You can do like rescan data function that will check the 'expiration' column values.

  For example if the date expired more that 6 month ago, 
row will be deleted.

You can add this like autofeature whenever app starts or admin is loggin in.

   For example make table 'Cleaning time' and
 there 'Last Cleaning Date' 'Next Cleaning Date' 
'Cleaning step';

Make the app to check the table each times script runs if the Cleaning date is === today, just make it to rescan users and delete other ones.

Then just change the 'Cleaning time date' by adding 'Step value' to the 'Last cleaning date'

btw. always use serwer time to manage data

Comments

1

You can calculate the period the user was enabled by using date and strtotime functions in php. After that you can trigger a query if the current date matches with your calculated date.

//---for calculating future date  
$newtimestamp = strtotime( "october, 26, 2015" );
$expmonth = date( 'm', $newtimestamp );

//---for calculating current date  
$currenttimestamp = date();
$currentmonth = date( 'm', $currenttimestamp );

//--if both date match then execute query
if ($expmonth == $currentmonth) {
  //write your update query here.
}

Comments

0

The correct way is to run cronjob daily on server which checks user expiration date and change it's status. If you want to test on local server you do not need any scheduler just hit the url which checks the user expiration and changes its status. if you have linux hosting you need only to set cron job by executing a command like wget -O /dev/null http://www.example.com/cronit.php

Comments

0

I think cron job will be good for you . You will just need to make a script and run that in a your specific time . That's It.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.