Jump to content

Recommended Posts

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.