There are many applications that I’ve written that require some sort of membership system. The core section of any membership system is its registration page. I decided to share with you how I store membership information to be called later for any other section of the site. First of all, like all PHP applications, I would suggest you create a configuration file. Here is an example of my config file.

config.php

<?php
global $adminemail, $baseurl;
//Enter your SQL Server HostName
$hostname=’localhost’;

//Enter the Username to login to the SQL Server
$username=’username’;

//Enter the password to login to SQL Server - This is not seen in the code of a PHP page
$password=’password’;

//Enter the name of the db you are connecting to
$dbname=’databasename’;

//Enter the email account of the system administrator
$adminemail=’nathan@blazekwebdesign.com’;

//Enter the baseurl of the addressbook
$baseurl=’http://www.blazekwebdesign.com/’;

//Enter the title of your addressbook
$siteTitle=’Blazek Web Design’;

mysql_connect($hostname, $username, $password) or die(mysql_error()) ;
mysql_select_db($dbname) or die(mysql_error()) ;
?>

Now I have a database table set up called Member and it has a structure like this

memberid
active
username
password
email
datejoined
fullname
address1
address2
city
state
zipcode
contact

This registration page is called join.php and will accept a users application, then sends them an email asking them to confirm their membership. Once confirmed there account is set to active and they can login.

join.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title><?php echo $siteTitle; ?></title>
</head>

<body>
<?php

#################### VARIABLES ############################
$formdata = ‘<form name=”register” method=”post” action=”join.php”>
<label>Username</label><input type=”text” name=”Username” /><br />
<label>Password</label><input type=”password” name=”password” /><br />
<label>Verify Password</label><input type=”password” name=”password2″ /><br />
<label>Email</label><input type=”text” name=”Email” /><br />
<label>Full Name</label><input type=”text” name=”FullName” /><br />
<label>Address Line 1</label><input type=”text” name=”Address1″ /><br />
<label>Address Line 2</label><input type=”text” name=”Address2″ /><br />
<label>City</label><input type=”text” name=”City” /><br />
<label>State</label><input type=”text” name=”State” /><br />
<label>ZipCode</label><input type=”text” name=”ZipCode” /><br />
<label>Can we email you?</label><input type=”checkbox” value=”yes” checked=”checked” name=”contact” /><br />
<input type=”submit” name=”join” />
</form>’;

 

include ‘config.php’;
if(isset($_POST["join"])) {
  if (empty($_POST["password"]) || empty($_POST["password2"])) {
   echo “Please enter a password”;
  }
  elseif ($_POST["password"]==$_POST["password2"]) {
   ################### STORE MEMBERSHIP INFO ######################
   $origpass = mysql_real_escape_string($_POST["password"]);
   $password = md5($origpass);
   $datejoined = date(”F j, Y, g:i a”);
   $fullname = mysql_real_escape_string($_POST["FullName"]);
   $username = mysql_real_escape_string($_POST["Username"]);
   $email = mysql_real_escape_string($_POST["Email"]);
   $address1 = mysql_real_escape_string($_POST["Address1"]);
   $address2 = mysql_real_escape_string($_POST["Address2"]);
   $city = mysql_real_escape_string($_POST["City"]);
   $state = mysql_real_escape_string($_POST["State"]);
   $zipcode = mysql_real_escape_string($_POST["ZipCode"]);
   $contact = mysql_real_escape_string($_POST["contact"]);
   
   ################ VERIFY DATA INTEGRITY #################
   
   if (usernameexists($username)) {
    echo “This username already exists!”;
    echo $formdata;
   }
   elseif (emailexists($email)) {
    echo “This email already exists!”;
    echo $formdata;
   }
   else {
   ################ ADD MEMBER TO member DATABASE #################
    $SQLnewmember = “INSERT INTO `member` VALUES (”, ‘0′, ‘$username’, ‘$password’, ‘$email’, ‘$datejoined’, ‘$fullname’, ‘$address1′, ‘$address2′, ‘$city’, ‘$state’, ‘$zipcode’, ‘$contact’)”;
    $results = mysql_query($SQLnewmember);
    $memberid = mysql_insert_id();
    emailUser($memberid, $fullname, $username, $origpass, $password, $email, $adminemail, $baseurl);
   }
  }
}
elseif ($_GET['memberID']>0) {
   $var = @$_GET['memberID'] ;
   $memberid = trim($var);
   $var = @$_GET['Hash'] ;
   $password = trim($var);
   $sqlactivate = “UPDATE `member` SET Active=’1′ WHERE Password=’$password’ AND memberID=$memberid;”;  //activate the member account
   mysql_query($sqlactivate);
   echo “Your account was activated! To login, <a href=\”index.php\”>click here</a>.”;
}
else {
################################################
############# DISPLAY FORM OUTPUT ##############
################################################
echo $formdata;
}
?>
</body>
</html>
<?php
function usernameexists($user) {
 ########### ATTEMPT TO FIND OUT IF THE USERNAME EXISTS #############
 $SQLfindusername = “SELECT * FROM `member` WHERE username=’$user’”;
 $results = mysql_query($SQLfindusername);
 $numrows = mysql_num_rows($results);
 if ($numrows == 0) {
  return false;
 }
 else {
  return true;
 }
}
function emailexists($email) {
 ########### ATTEMPT TO FIND OUT IF THE USERNAME EXISTS #############
 $SQLfindemail = “SELECT * FROM `member` WHERE email=’$email’”;
 $results = mysql_query($SQLfindemail);
 $numrows = mysql_num_rows($results);
 if ($numrows == 0) {
  return false;
 }
 else {
  return true;
 }
}
function emailUser($memberid, $fullname, $username, $origpass, $password, $email, $adminemail, $baseurl) {
      $subject = “Thanks for joining $siteTitle.”;
      $headers = “From: $adminemail\r\n” .
   ”X-Mailer: php”;
      $updateurl = “$baseurl/join.php?memberID=$memberid&Hash=$password”;
      $body = “Hi,\n\n$fullname,\n Thanks for joining $siteTitle! Your login information is as follows\n\nUsername:$username\nPassword:$origpass\n\nTo activate your account, please click this address:\n\n<a href=\”$updateurl\”>$updateurl</a>\n\nIf that link doesn’t work, copy and paste this link into your browser:\n\n$updateurl\n\nThanks for registering. I hope you enjoy ‘The Public Pen’!”;
      
      
      if (mail($email, $subject, $body, $headers)) {
      echo “You were sent an email to confirm your account. You should receive this within 10 minutes. To continue, please click the link in your email.<br> If you did not receive an email, please let the administrator know at <a href=\”mailto:$adminemail\”>$adminemail</a>”;
      }
      else {
        $sqlactivate = “UPDATE `member` SET Active=’1′ WHERE Password=’$password’ AND memberID=$memberid;”;
        //activate the member account
        mysql_query($sqlactivate);
        echo “Your account was activated! To login, <a href=\”index.php\”>click here</a>.”;
      }

}
?>

It is very straightforward and nothing is styled since my CSS will vary from site to site. I hope you find this bit of PHP useful! Check back soon for part 2, which will demonstrate a login page that sets a cookie and remembers if the user is logged in. It also will include a password reset script to email a user a new password if theirs is forgotten. Part 3 will password protect any page you choose with a small script.

Part 1: How to create a PHP membership database

Part 2: Membership login and password reset in PHP

Part 3: Password Protect your pages in PHP

  1. [...] the original: How to create a PHP membership database Related ArticlesBookmarksTags PHP PHP is a computer scripting language. Originally [...]

    How to create a PHP membership database | PHP-Blog.com on November 10th, 2008 at 7:57 pm
  2. [...] is part 2 of a 3 part part post. The first post is here: How to Create a PHP membership database. This post will take the previous a little further and will demonstrate how to create a login [...]

    Membership login and password reset in PHP | Blazek Web Design on November 11th, 2008 at 10:34 pm
  3. [...] Part 1: How to create a PHP membership database [...]

    Password Protect your pages in PHP | Blazek Web Design on November 12th, 2008 at 10:20 am
  4. [...] on all my script I work on, I create a link to my configuration file, which can be referenced on this post, if [...]

    AJAX - Save textarea as a draft to SQL database | Blazek Web Design on November 20th, 2008 at 11:00 pm
Would you like to login?