Jump to content

User registration system


h20boynz

Recommended Posts

I'm trying to teach myself how to use functions and includes in my scripts.  As an exercise I am trying to create a registration system that I can re-use on other future projects.

I have my main register.php page as follows:

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Register</title>
</head>
<body>

<?php
ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

// Check to see if query string is set, if not pass empty array to function.
if((!isset($_GET['success'])) && (!isset($_GET['active'])) && (!isset($_GET['valid'])))
	{
		include ('registerform.php');
		$invalidcode = array();
		regform($invalidcode);		
	} 

//If success is set, use value to determine response
if(isset($_GET['success']))
	{
		if($_GET['success'] == "false")
		{
		echo "There has been an error with your registration. You will be redirected to the registration page in 5 SECONDS.";
		echo "<meta http-equiv=refresh content =\"5; URL=index.php\">";
		}
		if($_GET['success'] == "true")
		{
		echo "Thanks for registering. An activation email has been sent to your registered email address.<br />";
		echo "Please check you email and follow the activation link contained within it.";
		} 
	}

//if active is set, determine response (note: code to be added here in future)
if(isset($_GET['active']))
	{	
		if($_GET['active'] == "true")
		{
		echo "Thank you for activating this account.<br />";
		echo "You may now return to the home page and login!";
		}
	}

//if valid is set there was a problem with the form validation.
//get valid from query string, convert back to an array and pass array to the form display script
if(isset($_GET['valid']))
	{
		$invalidcode = $_GET['valid'];
		$invalidcode = unserialize(urldecode($invalidcode));
		include ('registerform.php');
		regform($invalidcode);
		}

?>
</body>
</html>

When the page loads it first checks for any variables passed in the url to determine action.

Heres the code for the function regform():

<?php
	function regform($errors)
	{
	//set variables to initial value to prevent undefined variable error
	$errors0 = 0;
	$errors1 = 0;
	$errors2 = 0;
	$errors3 = 0;
	$errors4 = 0;
	$errors5 = 0;
	$errors6 = 0;
	$errors7 = 0;		

	//check to make sure the array that was passed was not empty
	//from my validation script the errors are in matching order to the display on this page.
	//I have only prepared 2 errors for this inital testing, there will be one for each field on the form.
	$i = count($errors);		
	if($i > 0){
	$errors0 = $errors[0];
	$errors1 = $errors[1];

	//$errors2 = $errors[2];
	//$errors3 = $errors[3];
	//$errors4 = $errors[4];
	//$errors5 = $errors[5];
	//$errors6 = $errors[6];
	//$errors7 = $errors[7];

	}
	echo "<form id='register' name='register' method='post' action='doreg.php' >";
	echo "<table>";

	//if the variable = 1 there was an error validating the field in the validate script

	if($errors0 == 1){
		echo "<tr><td><font color='red'>First Name</font></td><td><input type='text' name='firstname' /></td><tr>";
	} else {
	echo "<tr><td>First Name</td><td><input type='text' name='firstname' /></td><tr>";
	}
	if($errors1 == 1){
		echo "<tr><td><font color='red'>Last Name</font></td><td><input type='text' name='lastname' /></td><tr>";
	} else {
		echo "<tr><td>Last Name</td><td><input type='text' name='lastname' /></td><tr>";
	}

	//additional errors to be added, as per abov,e for the code below

	echo "<tr><td>Email Address</td><td><input type='text' name='email' /></td><tr>";
	echo "<tr><td>Password</td><td><input type='password' name='password1' /></td><tr>";
	echo "<tr><td>Confirm Password</td><td><input type='password' name='password2' /></td><tr>";
	echo "<tr><td></td><td></td><tr>";
	echo "<tr><td>Accept Terms?</td><td><input type='checkbox' name='accept' /></td><tr>";
	echo "<tr><td>Security String</td><td><input type='text' name='secstring' /></td><tr>";
	echo "<tr><td></td><td><input type='submit' name='submit' value='submit' /></td><tr>";
	echo "<tr><td></td><td><input type='hidden' name='form_submit' value='1' /></td><tr>";
	echo "</table>";
	echo "</form>";
	}			
?>

So the form takes the array ?valid passed to the register page and determines which fields had a validation error and highlights them red. (I'll modify this highlighting to be what ever I need later...just testing my logic :) )

 

The form then calls doreg.php on submit.

This will firstly run the validate script, then perform DB actions if all ok, else it will return to register with the error array passed in a query string:

<?php
if($_POST['form_submit'] == 1)
{
include ('validate.php');

	//the validate script returns $errorarray. Need to figure out better way to test values to determine if any error flags are set

	if(($errorarray[0]!=0) or ($errorarray[1]!=0) or ($errorarray[2]!=0))
	{
	$errorarray = urlencode(serialize($errorarray));
	echo "<meta http-equiv=refresh content=\"0; URL=register.php?valid=$errorarray\">" ;
	} 

echo "<meta http-equiv=refresh content=\"0; URL=register.php?success=true\">"; 
}

?>
php]
And finally there is the actual validation script. I was hoping to be able to use this for all form validations across the site. I'll just add if(isset) for the various form fields and make sure I standardize my field names across my sites forms.
[code=php:0]
<?php
$errorarray = array();

if($_POST['firstname'] == "") {
$errorarray[0] = 1; 
} else {$errorarray[0] = 0; }

if($_POST['lastname'] == "") {
$errorarray[1] = 1; 
} else {$errorarray[1] = 0; }

if($_POST['email'] == "") {
$errorarray[2] = 1; 
} else {$errorarray[2] = 0; }



return $errorarray;
?>

For this example I have just done simple empty string checks.

 

 

I'd really appreciate any advice on the code I've written to date. Im quite sure there are more elegant ways to achieve what these scripts do and I would greatfully accept any feedback. (Be gentle...I'm new :) )

 

Thanks

Link to comment
Share on other sites

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.