Page 1 of 1

Email activation help

Posted: Fri Nov 05, 2010 12:57 pm
by fatfacedharry
hi everyone, i've got some code that does email activation, its does a few username and password checks before carrying onto the email activation code. The problem i'm having is i can either make it do the checks or the email activation, not both. This is to do with an "} else {" positioning. I've looked through the code and i can't see what the problem is, it should do the checks as well as the email activation, but it just doesn't. My code is below;

<?php // Connects to your Database
mysql_connect("myserver", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

if ($_POST['form_submitted'] == '1') {

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['password'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}

$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users1 WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
$user = $_POST['username'];
header("Location: preguser.php?user=$user");
}
// this makes sure both passwords entered match
if ($_POST['password'] != $_POST['pass2']) {
$user = $_POST['username'];
header("Location: pregpass.php?user=$user");
}

// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}



$activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$username = mysql_real_escape_string($_POST[username]);
$password = mysql_real_escape_string($_POST[password]);

$email = mysql_real_escape_string($_POST[email]);

$sql="INSERT INTO users1 (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";

if (!mysql_query($sql))

{

die('Error: ' . mysql_error());

}



##Send activation Email

$to = $_POST[email];

$subject = " myname.co.uk Registration";

$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at designdog.co.uk. You can complete registration by clicking http://www.myname.co.uk/verify.php?$activationKey If this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ myname.co.uk Team";

$headers = 'From: noreply@myname.co.uk' . "\r\n" .

'Reply-To: noreply@myname.co.uk' . "\r\n" .

'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
header("Location: sent.php");

} else { //<<<If i move this up to just below the checks, then the checks work, but the email activation doesn't. i need it to be in this position and for the checks to work?


$queryString = $_SERVER['QUERY_STRING'];

$query = "SELECT * FROM users1";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

if ($queryString == $row["activationkey"]){

$sql="UPDATE users1 SET activationkey = '', status='activated' WHERE (id = $row[id])";
$thanks = "Thank you, your account has now been activated! Please log in!";

header("Location: p1.php?thanks=$thanks");

if (!mysql_query($sql))

{

die('Error: ' . mysql_error());

}

}

}

}

?>


Thanks for your time. I really hope someone can help. Fast ;)