-2

How do I write a cron for verified act emails & lost passwords timer?

I'm using apache on Linux server with mysql database and php code. In the database, besides the usual fields for an account, I have verified (a boolean that is 1 for yes and 0 for no), act_key (varchar(50) for sending an email to users with link to special pages for verifying their email or if they forgot their password), time(datetime) and temp(boolean that is 1 for forgot password or 0 for not).

When a user creates an account, I set the verified in the new act to 0, time=current date & time, temp=0 and generate act_key that I send in email to them. If they try to login and there act is not verified then they can't get in. If they click the link and go to a certain page within an hour I set verified to one and they can login. If they clink on the link after the one hour then I tell them that the key is invalid and there account was deleted and to make a new one.

If a user forgets their password, they go to this page where they enter their email and if the email matches a record in the database I set temp=1 and time=current date & time. I tell user that I sent out email regardless. If they clink this new link it goes to a page and if its less then an hour I make them reset there password and temp is set back to zero. If they click on the link after the hour, I tell them the key is invalid. After one hour temp is set back to zero.

The verification process was working fine until I added the time variable into the equation. I never really got the forgot password part working.

I know I need to setup a cron job to do two things every hour:

  1. delete accounts were verified=0 and it's been more than an hour since created that act.
  2. update accounts where temp=1 and it's been more than an hour since sent out forgot password email

My main questions are:

  1. Am I using the correct datatype in my database for the time field? And if not which should I use?

  2. How do I set the time variable?$time = getdate(); maybe, the default is null? $time is the variable i'm using to insert into dB record for time field.

  3. How do I make the cron script? I'm using godaddy, do I just point the cron control to this php file and if so is this the code I need?

    //connect to cron
    
    /web/cgi-bin/php5 -f "$HOME/html/hourly.php";
    
    //connect to dB
    
    include 'db_connect.php'; 
    
    //delete any unverified email act records that are more than 1 hour old
    
    DELETE from members WHERE verified == 0 & time + 1 > NOW()
    
    //make temp=0 on any record where temp is 1 for more than an hour
    
    UPDATE members SET temp=0 WHERE temp == 1 & time + 1 > NOW()
    
    mysqli_close($mysqli);
    
  4. Does any of my overall method sound fundamentally flawed?

Please thank you in advance.

Bob Todd
  • 39
  • 1
  • 1
  • 6
  • 1
    that's a lot of requirements with very little effort on your part –  Aug 11 '13 at 22:12
  • Are you saying I'm being lazy lol. I just need to be pointed in the right direction, I'm not asking for anyone to write all my pages for me. I have created the verification page and forgot password page on my own. – Bob Todd Aug 11 '13 at 22:30

1 Answers1

0

As far as I can see, temp is useless. It means the same thing as unverified.

This SQL is broken:

DELETE from members WHERE verified == 0 & time + 1 > NOW();
  • & is a bitwise operator. You want AND
  • SQL use a single = not ==
  • You will delete accounts less than an hour old, not more than an hour old

Try:

DELETE FROM members WHERE verified = 0 AND time + 1 < NOW();

Datetime is the correct field type for time, but I'd name the field creation_time or something so you have a clue what it means. You can set it's value by passing NOW() to SQL, e.g. INSERT INTO members SET time=NOW(), ....

rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • verified is used to know if the email for the account is verified or not. temp(probably could've made better name) is used to tell me if a user for that account has forgotten there username &/or password and has been emailed that fact stating so. Only verified users can get this email and only once per hour. But, thanks for correcting the line in my Cron script. :) – Bob Todd Aug 11 '13 at 22:25
  • Thank you rjmunro, it works! And yes, I changed the time field to creation_time, good idea. :) – Bob Todd Aug 12 '13 at 16:00