User sign-up form with advanced validation, subscription plan selection, feedback emails (ESP Edition)
From Octeth Wiki / Documentation
In this tutorial, we will show you how to develop a simple PHP script which will be used to accept user sign-ups and perform additional processes. The functionality of this custom user sign-up PHP script will be;
- Selecting a plan (user group)
- Verifying the email address
- Password confirmation
- Email address confirmation
Contents |
Introduction
In this example, we will use the following Oempro4 API calls:
Step 1: Create user_signup.php
In this tutorial, we will develop our PHP code in a file name called "user_signup.php". The URL of the web site is considered as "http://yourdomain.com" in this tutorial.
Create a PHP file on the root folder of your http://yourdomain.com website and name it as "user_signup.php"
Step 2:Setting Up User Sign-Up Plans (User Groups)
In this example, we will provide different types of subscription plans to the customer. In this way, he/she can select the best plan based on his/her budget during the sign-up process.
- Login to your administrator area
- Click "User Groups" link on the top menu
- Click "Create new user group" link
- Name your new user group such as "Plan A: 10,000 emails/month, $29.95"
- Set user group permissions, limits and other settings based on your choice
- Click "Create new user group" link again
- Name your new user group such as "Plan B: 20,000 emails/month, $59.95"
- Set user group permissions, limits and other settings based on your choice
- Click "Create new user group" link again
- Name your new user group such as "Plan C: 20,000 emails/month, $79.95"
- Set user group permissions, limits and other settings based on your choice
- If you wish to provide a free plan, repeat the steps again and set a very limited email delivery amount such as 50 emails per month
Now, you will see three user groups addition to your default user group in the list:
Step 3:Creating The General Outline
In this step, we will generate the user sign-up form. We will ask the following information from the user:
- First name
- Last name
- Email address
- Username
- Password
- Company name
- Subscription plan (user group)
Here's the generated HTML/PHP code for the sign-up page:
<?php /* Oempro4 API Example Personalized User Sign-Up Form */ error_reporting(0); ?> <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>User Sign-Up</title> </head> <body> <form action="./user_signup.php" method="post" accept-charset="utf-8"> <h1>Sign-Up Now</h1> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top" style="font-weight:bold;width:150px;">Select Plan*</td> <td> <input type="radio" name="InputPlan" value="5" id="InputPlan5" checked="checked" /> 50 emails per month, Free<br /> <input type="radio" name="InputPlan" value="2" id="InputPlan2" /> 10,000 emails per month, $29.95/month<br /> <input type="radio" name="InputPlan" value="3" id="InputPlan3" /> 20,000 emails per month, $59.95/month<br /> <input type="radio" name="InputPlan" value="4" id="InputPlan4" /> 30,000 emails per month, $79.95/month </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">First name*</td> <td> <input type="text" name="InputFirstName" value="<?php print($_POST['InputFirstName']); ?>" id="InputFirstName"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Last name*</td> <td> <input type="text" name="InputLastName" value="<?php print($_POST['InputLastName']); ?>" id="InputLastName"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Company name</td> <td> <input type="text" name="InputCompanyName" value="<?php print($_POST['InputCompanyName']); ?>" id="InputCompanyName"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Email address*</td> <td> <input type="text" name="InputEmailAddress" value="<?php print($_POST['InputEmailAddress']); ?>" id="InputEmailAddress"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Username*</td> <td> <input type="text" name="InputUsername" value="<?php print($_POST['InputUsername']); ?>" id="InputUsername"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Password*</td> <td> <input type="password" name="InputPassword" value="<?php print($_POST['InputPassword']); ?>" id="InputPassword"> </td> </tr> <tr> <td valign="top" style="font-weight:bold;width:150px;">Confirm Password</td> <td> <input type="password" name="InputConfirmPassword" value="<?php print($_POST['InputConfirmPassword']); ?>" id="InputConfirmPassword"> </td> </tr> <tr> <td colspan="2" style="text-align:center;padding-top:18px;"> <input type="submit" name="ButtonSignUp" value="Sign-Up" id="ButtonSignUp"> </td> </tr> </table> </form> </body> </html>
Step 4:Form Validations
In this step, we will perform some form validations such as required field check, field value format check, etc.:
Required Fields
- First name
- Last name
- Email address
- Username
- Password
Field Confirmations
- Password
Here's the updated user_signup.php file:
<?php
/*
Oempro4 API Example
Personalized User Sign-Up Form
*/
error_reporting(0);
// Sign-up event - Start {
if ($_POST['ButtonSignUp'] != '')
{
// Field validations - Start {
if (($_POST['InputPlan'] == '') || ($_POST['InputFirstName'] == '') || ($_POST['InputLastName'] == '') || ($_POST['InputEmailAddress'] == '') || ($_POST['InputUsername'] == '') || ($_POST['InputPassword'] == '') || ($_POST['InputConfirmPassword'] == ''))
{
print "Missing information. Please return back and fill in all required fields.";
exit;
}
if ($_POST['InputPassword'] != $_POST['InputConfirmPassword'])
{
print "Passwords do not match. Please return back and re-enter your password.";
exit;
}
// Field validations - End }
}
// Sign-up event - End }
?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>User Sign-Up</title>
</head>
<body>
<form action="./user_signup.php" method="post" accept-charset="utf-8">
<h1>Sign-Up Now</h1>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Select Plan*</td>
<td>
<input type="radio" name="InputPlan" value="5" id="InputPlan5" checked="checked" /> 50 emails per month, Free<br />
<input type="radio" name="InputPlan" value="2" id="InputPlan2" /> 10,000 emails per month, $29.95/month<br />
<input type="radio" name="InputPlan" value="3" id="InputPlan3" /> 20,000 emails per month, $59.95/month<br />
<input type="radio" name="InputPlan" value="4" id="InputPlan4" /> 30,000 emails per month, $79.95/month
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">First name*</td>
<td>
<input type="text" name="InputFirstName" value="<?php print($_POST['InputFirstName']); ?>" id="InputFirstName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Last name*</td>
<td>
<input type="text" name="InputLastName" value="<?php print($_POST['InputLastName']); ?>" id="InputLastName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Company name</td>
<td>
<input type="text" name="InputCompanyName" value="<?php print($_POST['InputCompanyName']); ?>" id="InputCompanyName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Email address*</td>
<td>
<input type="text" name="InputEmailAddress" value="<?php print($_POST['InputEmailAddress']); ?>" id="InputEmailAddress">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Username*</td>
<td>
<input type="text" name="InputUsername" value="<?php print($_POST['InputUsername']); ?>" id="InputUsername">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Password*</td>
<td>
<input type="password" name="InputPassword" value="<?php print($_POST['InputPassword']); ?>" id="InputPassword">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Confirm Password*</td>
<td>
<input type="password" name="InputConfirmPassword" value="<?php print($_POST['InputConfirmPassword']); ?>" id="InputConfirmPassword">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;padding-top:18px;">
<input type="submit" name="ButtonSignUp" value="Sign-Up" id="ButtonSignUp">
</td>
</tr>
</table>
</form>
</body>
</html>
Step 5: Creating User Account
Okay, now it's time to communicate with Oempro4 API and send the submitted information to User.Create API call to create the new user account. For this, we will need the following information defined in the PHP file:
- Oempro4 API URL
- Username/password of the administrator account
Here's the updated PHP script:
<?php
/*
Oempro4 API Example
Personalized User Sign-Up Form
*/
error_reporting(0);
// Settings - Start {
define('OEMPRO4_API_URL', 'http://yourdomain.com/oempro4/api.php');
define('OEMPRO4_ADMIN_USERNAME', 'admin');
define('OEMPRO4_ADMIN_PASSWORD', 'admin');
define('CONFIRMATION_LINK', 'http://yourdomain.com/user_confirm.php');
// Settings - End }
// Sign-up event - Start {
if ($_POST['ButtonSignUp'] != '')
{
// Field validations - Start {
if (($_POST['InputPlan'] == '') || ($_POST['InputFirstName'] == '') || ($_POST['InputLastName'] == '') || ($_POST['InputEmailAddress'] == '') || ($_POST['InputUsername'] == '') || ($_POST['InputPassword'] == '') || ($_POST['InputConfirmPassword'] == ''))
{
print("Missing information. Please return back and fill in all required fields.");
exit;
}
if ($_POST['InputPassword'] != $_POST['InputConfirmPassword'])
{
print("Passwords do not match. Please return back and re-enter your password.");
exit;
}
// Field validations - End }
// Communicate with Oempro4 API - Start {
// Login as Oempro4 admin (Admin.Login)
$ArrayPostParameters = array(
'Command=Admin.Login',
'ResponseFormat=XML',
'Username='.OEMPRO4_ADMIN_USERNAME,
'Password='.OEMPRO4_ADMIN_PASSWORD,
);
$ArrayReturn = DataPostToRemoteURL(OEMPRO4_API_URL, $ArrayPostParameters, 'POST', false, '', '', $ConnectTimeOutSeconds = 5, false);
if ($ArrayReturn[0] == false)
{
print("API connection error occurred.");
exit;
}
else
{
$XML = $ArrayReturn[1];
$ObjectXML = simplexml_load_string($XML);
if ($ObjectXML->Success == false)
{
print("Incorrect Oempro4 Admin Credentials");
exit;
}
else
{
define('OEMPRO4_API_SESSION', $ObjectXML->SessionID);
}
}
// Create the new user account (User.Create)
$ArrayPostParameters = array(
'Command=User.Create',
'ResponseFormat=XML',
'SessionID='.OEMPRO4_API_SESSION,
'RelUserGroupID='.$_POST['InputPlan'],
'EmailAddress='.$_POST['InputEmailAddress'],
'Username='.$_POST['InputUsername'],
'Password='.$_POST['InputPassword'],
'FirstName='.$_POST['InputFirstName'],
'LastName='.$_POST['InputLastName'],
'Language=en',
'TimeZone=GMT',
'ReputationLevel=Trusted',
'CompanyName='.$_POST['InputCompanyName'],
'AccountStatus=Disabled',
);
$ArrayReturn = DataPostToRemoteURL(OEMPRO4_API_URL, $ArrayPostParameters, 'POST', false, '', '', $ConnectTimeOutSeconds = 5, false);
if ($ArrayReturn[0] == false)
{
print("API connection error occurred.");
exit;
}
else
{
$XML = $ArrayReturn[1];
$ObjectXML = simplexml_load_string($XML);
if ($ObjectXML->Success == false)
{
print("API Error #".$ObjectXML->ErrorCode.' occurred.');
exit;
}
else
{
mail($_POST['InputEmailAddress'], 'Please confirm your account', 'Thanks for signing up. Please confirm your account by clicking the following link: '.CONFIRMATION_LINK.'?UserID='.$ObjectXML->UserID, 'From: sender@yourdomain.com');
print("Thank you for signing up. We just sent you a confirmation email. Please click the link inside the email you will receive and your account will be activated.");
exit;
}
}
// Communicate with Oempro4 API - End }
}
// Sign-up event - End }
// Functions - Start {
function DataPostToRemoteURL($URL, $ArrayPostParameters, $HTTPRequestType = 'POST', $HTTPAuth = false, $HTTPAuthUsername = '', $HTTPAuthPassword = '', $ConnectTimeOutSeconds = 1, $ReturnHeaders = false)
{
$PostParameters = implode('&', $ArrayPostParameters);
$CurlHandler = curl_init();
curl_setopt($CurlHandler, CURLOPT_URL, $URL);
if ($HTTPRequestType == 'GET')
{
curl_setopt($CurlHandler, CURLOPT_HTTPGET, true);
}
elseif ($HTTPRequestType == 'PUT')
{
curl_setopt($CurlHandler, CURLOPT_PUT, true);
}
elseif ($HTTPRequestType == 'DELETE')
{
curl_setopt($CurlHandler, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
else
{
curl_setopt($CurlHandler, CURLOPT_POST, true);
curl_setopt($CurlHandler, CURLOPT_POSTFIELDS, $PostParameters);
}
curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 5);
curl_setopt($CurlHandler, CURLOPT_CONNECTTIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_TIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
// The option doesn't work with safe mode or when open_basedir is set.
if ((ini_get('safe_mode') != false) && (ini_get('open_basedir') != false))
{
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, true);
}
if ($ReturnHeaders == true)
{
curl_setopt($CurlHandler, CURLOPT_HEADER, true);
}
else
{
curl_setopt($CurlHandler, CURLOPT_HEADER, false);
}
if ($HTTPAuth == true)
{
curl_setopt($CurlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($CurlHandler, CURLOPT_USERPWD, $HTTPAuthUsername.':'.$HTTPAuthPassword);
}
$RemoteContent = curl_exec($CurlHandler);
if (curl_error($CurlHandler) != '')
{
return array(false, curl_error($CurlHandler));
}
curl_close($CurlHandler);
return array(true, $RemoteContent);
}
// Functions - End }
?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>User Sign-Up</title>
</head>
<body>
<form action="./user_signup.php" method="post" accept-charset="utf-8">
<h1>Sign-Up Now</h1>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Select Plan*</td>
<td>
<input type="radio" name="InputPlan" value="5" id="InputPlan5" checked="checked" /> 50 emails per month, Free<br />
<input type="radio" name="InputPlan" value="2" id="InputPlan2" /> 10,000 emails per month, $29.95/month<br />
<input type="radio" name="InputPlan" value="3" id="InputPlan3" /> 20,000 emails per month, $59.95/month<br />
<input type="radio" name="InputPlan" value="4" id="InputPlan4" /> 30,000 emails per month, $79.95/month
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">First name*</td>
<td>
<input type="text" name="InputFirstName" value="<?php print($_POST['InputFirstName']); ?>" id="InputFirstName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Last name*</td>
<td>
<input type="text" name="InputLastName" value="<?php print($_POST['InputLastName']); ?>" id="InputLastName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Company name</td>
<td>
<input type="text" name="InputCompanyName" value="<?php print($_POST['InputCompanyName']); ?>" id="InputCompanyName">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Email address*</td>
<td>
<input type="text" name="InputEmailAddress" value="<?php print($_POST['InputEmailAddress']); ?>" id="InputEmailAddress">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Username*</td>
<td>
<input type="text" name="InputUsername" value="<?php print($_POST['InputUsername']); ?>" id="InputUsername">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Password*</td>
<td>
<input type="password" name="InputPassword" value="<?php print($_POST['InputPassword']); ?>" id="InputPassword">
</td>
</tr>
<tr>
<td valign="top" style="font-weight:bold;width:150px;">Confirm Password*</td>
<td>
<input type="password" name="InputConfirmPassword" value="<?php print($_POST['InputConfirmPassword']); ?>" id="InputConfirmPassword">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;padding-top:18px;">
<input type="submit" name="ButtonSignUp" value="Sign-Up" id="ButtonSignUp">
</td>
</tr>
</table>
</form>
</body>
</html>
Step 6: Confirming User Accounts
In step 5, we sent a confirmation email to the new user email address. Now, it's time to make the confirmation PHP script which will be activated once user clicks the link (user_confirm.php) inside the email.
Here's the source code of user_confirm.php file:
<?php
/*
Oempro4 API Example
Personalized User Sign-Up Form
*/
error_reporting(0);
// Settings - Start {
define('OEMPRO4_API_URL', 'http://oempro4.local/api.php');
define('OEMPRO4_ADMIN_USERNAME', 'admin');
define('OEMPRO4_ADMIN_PASSWORD', 'admin');
// Settings - End }
// Confirm the user account - Start {
if ($_GET['UserID'] != '')
{
// Login as Oempro4 admin (Admin.Login)
$ArrayPostParameters = array(
'Command=Admin.Login',
'ResponseFormat=XML',
'Username='.OEMPRO4_ADMIN_USERNAME,
'Password='.OEMPRO4_ADMIN_PASSWORD,
);
$ArrayReturn = DataPostToRemoteURL(OEMPRO4_API_URL, $ArrayPostParameters, 'POST', false, '', '', $ConnectTimeOutSeconds = 5, false);
if ($ArrayReturn[0] == false)
{
print("API connection error occurred.");
exit;
}
else
{
$XML = $ArrayReturn[1];
$ObjectXML = simplexml_load_string($XML);
if ($ObjectXML->Success == false)
{
print("Incorrect Oempro4 Admin Credentials");
exit;
}
else
{
define('OEMPRO4_API_SESSION', $ObjectXML->SessionID);
}
}
// Update the user account (User.Update)
$ArrayPostParameters = array(
'Command=User.Update',
'ResponseFormat=XML',
'SessionID='.OEMPRO4_API_SESSION,
'UserID='.$_GET['UserID'],
'AccountStatus=Enabled',
);
$ArrayReturn = DataPostToRemoteURL(OEMPRO4_API_URL, $ArrayPostParameters, 'POST', false, '', '', $ConnectTimeOutSeconds = 5, false);
if ($ArrayReturn[0] == false)
{
print("API connection error occurred.");
exit;
}
else
{
$XML = $ArrayReturn[1];
$ObjectXML = simplexml_load_string($XML);
if ($ObjectXML->Success == false)
{
print("API Error #".$ObjectXML->ErrorCode.' occurred.');
exit;
}
else
{
print("Your account confirmed. Thank you.");
exit;
}
}
}
// Confirm the user account - End }
// Functions - Start {
function DataPostToRemoteURL($URL, $ArrayPostParameters, $HTTPRequestType = 'POST', $HTTPAuth = false, $HTTPAuthUsername = '', $HTTPAuthPassword = '', $ConnectTimeOutSeconds = 1, $ReturnHeaders = false)
{
$PostParameters = implode('&', $ArrayPostParameters);
$CurlHandler = curl_init();
curl_setopt($CurlHandler, CURLOPT_URL, $URL);
if ($HTTPRequestType == 'GET')
{
curl_setopt($CurlHandler, CURLOPT_HTTPGET, true);
}
elseif ($HTTPRequestType == 'PUT')
{
curl_setopt($CurlHandler, CURLOPT_PUT, true);
}
elseif ($HTTPRequestType == 'DELETE')
{
curl_setopt($CurlHandler, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
else
{
curl_setopt($CurlHandler, CURLOPT_POST, true);
curl_setopt($CurlHandler, CURLOPT_POSTFIELDS, $PostParameters);
}
curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 5);
curl_setopt($CurlHandler, CURLOPT_CONNECTTIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_TIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
// The option doesn't work with safe mode or when open_basedir is set.
if ((ini_get('safe_mode') != false) && (ini_get('open_basedir') != false))
{
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, true);
}
if ($ReturnHeaders == true)
{
curl_setopt($CurlHandler, CURLOPT_HEADER, true);
}
else
{
curl_setopt($CurlHandler, CURLOPT_HEADER, false);
}
if ($HTTPAuth == true)
{
curl_setopt($CurlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($CurlHandler, CURLOPT_USERPWD, $HTTPAuthUsername.':'.$HTTPAuthPassword);
}
$RemoteContent = curl_exec($CurlHandler);
if (curl_error($CurlHandler) != '')
{
return array(false, curl_error($CurlHandler));
}
curl_close($CurlHandler);
return array(true, $RemoteContent);
}
// Functions - End }
?>
That's all. We just built a custom sign-up form with different user plans and email confirmation functionality. This PHP script can be extended with additional functionalities such as adding CAPTCHA verification, sending "thank you" email after X days, sending "notification" email to not-confirmed users after X days, etc. You are limited with your coding capabilities and imagination.

