carlg Posted August 8, 2007 Share Posted August 8, 2007 I am building a php site that requires the user's to register. Each user has his own name and password. I already have it coded where the user can register and his info gets stored in appropriate DB tables. I also have the login coded so my users could log in. I want to alter the site so when a user registers, it sends him an email, and then he clicks on the link in the mail before proceeding. Anyone have a code sample of this? or how it should be done? Thanks for the info Carl Link to comment https://forums.phpfreaks.com/topic/63926-user-registration-email/ Share on other sites More sharing options...
php_tom Posted August 8, 2007 Share Posted August 8, 2007 Try: <?php // in 'register.php' // Load POSTed data $username = striptags($_POST['username']); $pass1 = striptags($_POST['password1']); $pass2 = striptags($_POST['password2']); $email = striptags($_POST['email']); // Validate data if($pass1 != $pass2) die("Passwords didn't match!"); if(!strstr($email, '@') || !strstr($email, '.') || strlen($email)<6) die("Bad email!"); // Insert into a temporary database table, say, 'pending' and // put a UNIQUE user id number in as well, and store it in $user_id $msg = "Welcome to mydomain.com, $username! \n\n Please finish your registration by visiting http://www.mydomain.com/verify.php?id=$user_id \n\n\ Sincerely, \n\n The mydomain.com team \n\n"; // Send the email if(!mail($email, "mydomain.com registration verification", $msg)) die("Registration failed! Maybe your email was not real..."); ?> Then in 'verify.php': <?php // Fetch the user_id $id = $_GET['id']; // Connect to DB: mysql_connect('domain', 'username', 'password'); mysql_select_db('db'); $res = mysql_query("SELECT * FROM pending WHERE id=".$id." LIMIT 1"); if(mysql_num_rows($res) < 1) die("Registration verification failed! Please try registering again."); else { $row = mysql_fetch_assoc($res); $quer = mysql_query("INSERT INTO users VALUES (".$row['username'].", ".$row['password'].", ".$row['email'].", ".$row['id'].")"); if(!$quer) die("Registration verification failed! Please try again."); else { mysql_query("DELETE * FROM pending WHERE id=".$id." LIMIT 1"); echo "Your registration has been verified. Click <a href='somewhere.php'>here</a> to log in."; } ?> I didn't actually test the code, but it should work (barring any small syntax errors). Hope that helps. Link to comment https://forums.phpfreaks.com/topic/63926-user-registration-email/#findComment-318653 Share on other sites More sharing options...
carlg Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks, This is very helpful Carl Link to comment https://forums.phpfreaks.com/topic/63926-user-registration-email/#findComment-318687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.