My PHP codeigniter site sends 3 types of emails to users, they are html formatted and I wish to have a ‘view this email in browser’ link option.
How can I create a reasonably secure link? 2 of the emails are sensitive in nature:
-
New Registration: email shows their details and an activation link
-
Forgot Pass: email shows the link to reset pass
Is it enough to just do something like this?:
$code = sha1(mt_rand(10000,99999).time().$user_email);
$link = 'email/view/' . $code . '/' . $user_id . '/';
=> http://mysite.com/email/view/c0acc09c6d00b706e1e511e52f286b1859067047/213/
So authentication would be done based on a random hash and their user_id. Is it worth hashing the user id also?
I wouldn’t include the user_id as a URL parameter, I’d just stick with the hash.
As soon as somebody navigates to the link, the hash should be cross-checked with those stored in your database, and if they match, pull the information associated with that e-mail (i.e. username, e-mail). When they go to fill out the form you can validate to see if their entered data is compatible.
For example, if a user has forgotten their password, as soon as they enter their e-mail address I reset their password to a random value (stored as a hash) and use this stored hash as the URL parameter in their reset e-mail.