Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way for PHP email verification link

I already have an advanced user login/register system on my website (colemansystems.psm2.co.uk). However, I would like to have a email sent to new users for verification of their email address. If they have not clicked the link they will not be able to access their account. I am semi-experienced with PHP and MySQL, so please explain in depth.

Edit: The code I'm using for the verify.php file (the link the user click on with a GET (for example, verify.php?d=51773199320))

$secret = $_GET['d'];
$result = mysql_query("SELECT valid FROM users WHERE secret=$secret");
while ($row = mysql_fetch_array($result))
{
    $valid = $row['valid'];
}
if ($valid == "") {
    echo"There seems to be a problem with the verification code.<br><br><br><br><br>";
}
elseif ($valid == "1")
{
    echo"Your account is already verified.<br><br><br><br><br>";
}
else
{
    mysql_query("UPDATE users SET valid = '1' WHERE secret=$secret");  
    echo "Thank you, your account is now verified and you are free to use the exclusive features!<br><br><br><br><br><br>";
}

Is this secure?

like image 604
ryryan Avatar asked Sep 25 '10 18:09

ryryan


People also ask

How do I create a link to verify my email?

My solution: Add string column "code" and boolean column "is_active" (with default value false) to user table. When user register, generate unique string key and save to database. Send to email link, for example host.com/user/email/{code}/confirm.

What is a verification link in email?

Email Verification is the process of verifying an email address is valid and improving the odds that it belongs to a real person. The whole purpose of email verification is to ensure that a real person with a valid email address will receive and interact with the sent email.


1 Answers

The easiest way is not to register unverified users at all.

Ask them for an email address and send email with a link that contains this address sealed with a hash. Upon receiving this link you can start the registration process.

Something like this

$secret = "35onoi2=-7#%g03kl";
$email = urlencode($_POST['email']);
$hash = MD5($_POST['email'].$secret);
$link = "http://example.com/register.php?email=$email&hash=$hash";

And in your register.php add 2 hidden fields to the registration form - email and hash, storing their received values from GET.

Finally, process registration and check,

if (md5($_POST['email'].$secret) == $_POST['hash']) {
    //Continue registration.
}
like image 136
Your Common Sense Avatar answered Nov 15 '22 14:11

Your Common Sense