views:

84

answers:

2

I already have an advance 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 & MySQL, so please explain in depth. Any help would be much appreciated.

Edit: The code i'm using for the verify.php (the link the user click on with a GET (eg: 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?

+7  A: 

Easiest way is not to register unverified users at all.
Ask them for email address and send email with link contains this address sealed with hash. Upon receiving this link you can start 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&amp;hash=$hash";

And in your register.php add to registration form 2 hidden fields - email and hash, storing there received values from GET.
Finally, process registration and check

if (md5($_POST['email'].$secret) == $_POST['hash']) {
  //continue registration
}
Col. Shrapnel
+1 That's the way especially the first sentence :)
Sarfraz
this is a smart solution, you dont have to store the verificaton code in your database.
coolkid
I have taken your information on borad but made php generate a random code, this is then inserted into the database on the same row as the user + the valid field is set to 0 (to not allow them in). When they click on the link it then checks where the code is set to in the database and makes sure it isn't already valid and then sets valid to 1. This then allows them in. I'm not sure if it's secure though.
hart1994
This is the code i am using on the verify.php (the one that the user click with the secret at the end as a GET) at the top.
hart1994
+1  A: 

Easiest for whom - user, coder, computer? What are you optimizing - the quantity of keypresses, the size of the code, the user experience?

The easiest to code is probably unsafe. You should check the email address for correctness before sending a letter to it.

Grigori Kochanov
Easiest for me, the coder. I want it to be easy and but very secure to stop hackers.
hart1994