Help PHP Registration Form.
Posted: Wed Jun 29, 2011 12:14 am
This is the code I have. I had the Username field working and I simply duplicated the same methods for the other fields. For some reason now I keep getting;
Error 500 - Internal server error
An internal server error has occured!
Please try again later.
Think this is an issue with my server?
Or is there something wrong with my code?
I want the errors to appear next to the input field and a main error or success message at the top. I want only one error to be displayed for each field at a time as well(elseif). I want it to send a confirmation email to validate account.
Heres the link to it, the Functions, and the php/html
Can someone tell me whats up? Thanks.
LINK
http://www.chmedia.net/loginscript/register-form.php
FUNCTIONS
<?php
function format_email($info, $format){
//set the root
$root = $_SERVER['DOCUMENT_ROOT'].'/loginscript';
//grab the template content
$template = file_get_contents($root.'/signup_template.'.$format);
//replace all the tags
$template = ereg_replace('{USERNAME}', $info['username'], $template);
$template = ereg_replace('{EMAIL}', $info['email'], $template);
$template = ereg_replace('{KEY}', $info['key'], $template);
$template = ereg_replace('{SITEPATH}','http://chmedia.net/loginscript', $template);
//return the html of the template
return $template;
}
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to CH Media.Net');
$message ->setFrom(array('noreply@chmedia.net' => 'CH Media.Net'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
?>
PHP
<?php
//Include database connection details
require_once('includes/config.php');
require('includes/functions.php');
//Variables and Arrays to sotre validation errors
$checkfail = array();
$checkfail = NULL;
$emailsuccess = array();
$emailsuccess = NULL;
$emailfail = array();
$emailfail = NULL;
$checkuser = array();
$checkuser = NULL;
$checkpass = array();
$checkpass = NULL;
$checkpass1 = array();
$checkpass1 = NULL;
$checkemail = array();
$checkemail = NULL;
$checkfname = array();
$checkfname = NULL;
$checklname = array();
$checklname = NULL;
$checkadd = array();
$checkadd = NULL;
$checkcity = array();
$checkcity = NULL;
$checkstate = array();
$checkstate = NULL;
$checkcountry = array();
$checkcountry = NULL;
$checkzip = array();
$checkzip = NULL;
$errflg = false;
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return clean($str);
}
//Sanitize the POST values
if(isset($_POST['register'])){
$username = clean($_POST['username']);
$password = clean($_POST['password']);
$password1 = clean($_POST['password1']);
$email = clean($_POST['email']);
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$add = clean($_POST['add']);
$alt = clean($_POST['alt']);
$city = clean($_POST['city']);
$state = clean($_POST['state']);
$country = clean($_POST['country']);
$zip = clean($_POST['zip']);
$phone = clean($_POST['phone']);
//Input Validations
//Check Username
if(empty($username)){ $errflg = TRUE; ($checkuser = 'You must create a Username.'); }
//Check Username Length
elseif(strlen($username) < 5) {$errflg = TRUE; ($checkuser = 'Your Username must be longer than 5 characters. '); }
elseif(strlen($username) > 12) {$errflg = TRUE; ($checkuser = 'Your Username must not be longer than 12 characters. '); }
//Check for Duplicate Username
elseif(!empty($username)) {
$qry = "SELECT * FROM users WHERE username='$username'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) { $errflg = TRUE; ($checkuser = 'That Username has already been taken');
}
@mysql_free_result($result);
}
}
//Check Password 1
if(empty($password)){ $errflg = TRUE; ($checkpass = 'You must create a Password'); }
//Check Password 2
elseif(empty($password1)){ $errflg = TRUE; ($checkpass1 = 'You must Re-type your Password'); }
//Check Password Match
elseif($password != $password1) { $errflg = TRUE; ($checkpass = 'Your password does not match.');}
//Check Email
if(empty($email)){ $errflg = TRUE; ($checkemail = 'You must type in your Email.');
//Check Email Valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $errflg = TRUE; ($checkemail = 'You must enter a valid email.'); }
//Check Name
if(empty($fname)){ $errflg = TRUE; ($checkfname = 'You must enter your First Name'); }
if(empty($lname)){ $errflg = TRUE; ($checklname = 'You must enter your Last Name'); }
//Check Address
if(empty($add)){ $errflg = TRUE; ($checkadd = 'You must enter your Street Address'); }
if(empty($city)){ $errflg = TRUE; ($checkcity = 'You must enter your City/Town'); }
if(empty($state)){ $errflg = TRUE; ($checkstate = 'You must select your State'); }
if(empty($country)){ $errflg = TRUE; ($checkcountry = 'You must select your Country'); }
if(empty($zip)){ $errflg = TRUE; ($checkzip = 'You must enter your Zip/Postal Code'); }
//Validation error flag
if ($errflg = TRUE); { $checkfail = 'Sorry, but we ran into problems while processing your information.<br />Please check that all required fields have been completed properly.';
}
//No errors continue signup
//Create INSERT query
$qry = "INSERT INTO users(NULL,'$username','".md5($_POST['password'])."','$email','$fname','$lname','$add','$alt','$city','$state','$country','$zip','$phone',0)";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
//get the new user id
$userid = mysql_insert_id();
//create a random key
$key = $username . $email . date('mY');
$key = md5($key);
//add confirm row
$confirm = mysql_query("INSERT INTO `confirm` VALUES(NULL,'$userid','$key','$email')");
if($confirm){
//include the swift class
include_once './includes/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){ $emailsuccess = 'Thank you for registering at CH Media.Net. Please check your email and follow the link to verify your account.';
//email sent
}elseif(!send_email($info)) { $emailfail = 'Sorry there has been an error! We could not confirm your email, please try again.' . mysql_error(); }
}
}
}
?>
HTML
<!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=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="loginForm" name="loginForm" method="post" action=""><fieldset>
<table width="650" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<th colspan="3"><?php print $checkfail; print $emailsuccess; print $emailfail; ?></th>
</tr>
<tr>
<th width="140"><label for="username">Username:</label></th>
<td width="190"><input name="username" type="text" class="textfield" id="username" value="<?php echo $username ; ?>" /></td>
<td><?php print $checkuser; ?></td>
</tr>
<tr>
<th><label for="password">Password:</label></th>
<td><input name="password" type="password" class="textfield" id="password" /></td>
<td><?php print $checkpass; ?></td>
</tr>
<tr>
<th><label for="password1">Re-type Password:</label></th>
<td><input name="password1" type="password" class="textfield" id="password1" /></td>
<td><?php print $checkpass1; ?></td>
</tr>
<tr>
<th><label for="email">Email:</label></th>
<td><input name="email" type="text" class="textfield" id="email" value="<?php echo $email ; ?>" /></td>
<td><?php print $checkemail; ?></td>
</tr>
<tr>
<th><label for="fname">First Name:</label></th>
<td><input name="fname" type="text" class="textfield" id="fname" value="<?php echo $fname ; ?>" /></td>
<td><?php print $checkfname; ?></td>
</tr>
<tr>
<th><label for="lname">Last Name:</label></th>
<td><input name="lname" type="text" class="textfield" id="lname" value="<?php echo $lname ; ?>" /></td>
<td><?php print $checklname; ?></td>
</tr>
<tr>
<th><label for="add">Address :</label></th>
<td><input name="add" type="text" class="textfield" id="add" value="<?php echo $add ; ?>" /></td>
<td><?php print $checkadd; ?></td>
</tr>
<tr>
<th><label for="alt">Address Line 2 :</label></th>
<td><input name="alt" type="text" class="textfield" id="alt" value="<?php echo $alt ; ?>" /></td>
<td> </td>
</tr>
<tr>
<th><label for="city">City or Town :</label></th>
<td><input name="city" type="text" class="textfield" id="city" value="<?php echo $city ; ?>" /></td>
<td><?php print $checkcity; ?></td>
</tr>
<tr>
<th><label for="state">State :</label></th>
<td><select name="state" class="textfield" id="state" size="1">
<option selected value="">State...</option>
<option value="None">None</option>
<option value="">-- UNITED STATES --</option>
MORE OPTIONS
</select></td>
<td><?php print $checkstate; ?></td>
</tr>
<tr>
<th><label for="country">Country :</label></th>
<td><select name="country" class="textfield" id="country">
<option value="">Country...</option>
MORE OPTION
</select></td>
<td><?php print $checkcountry; ?></td>
</tr>
<tr>
<th><label for="zip">Zip / Postal Code:</label></th>
<td><input name="zip" type="text" class="textfield" id="zip" value="<?php echo $zip ; ?>" /></td>
<td><?php print $checkzip; ?></td>
</tr>
<tr>
<th><label for="phone">Phone:</label></th>
<td><input name="phone" type="tel" class="textfield" id="phone" value="<?php echo $phone ; ?>" /></td>
<td> </td>
</tr>
<tr>
<th> </th>
<td><input type="submit" name="register" value="Register" /></td>
<td> </td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
Error 500 - Internal server error
An internal server error has occured!
Please try again later.
Think this is an issue with my server?
Or is there something wrong with my code?
I want the errors to appear next to the input field and a main error or success message at the top. I want only one error to be displayed for each field at a time as well(elseif). I want it to send a confirmation email to validate account.
Heres the link to it, the Functions, and the php/html
Can someone tell me whats up? Thanks.
LINK
http://www.chmedia.net/loginscript/register-form.php
FUNCTIONS
<?php
function format_email($info, $format){
//set the root
$root = $_SERVER['DOCUMENT_ROOT'].'/loginscript';
//grab the template content
$template = file_get_contents($root.'/signup_template.'.$format);
//replace all the tags
$template = ereg_replace('{USERNAME}', $info['username'], $template);
$template = ereg_replace('{EMAIL}', $info['email'], $template);
$template = ereg_replace('{KEY}', $info['key'], $template);
$template = ereg_replace('{SITEPATH}','http://chmedia.net/loginscript', $template);
//return the html of the template
return $template;
}
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to CH Media.Net');
$message ->setFrom(array('noreply@chmedia.net' => 'CH Media.Net'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
?>
PHP
<?php
//Include database connection details
require_once('includes/config.php');
require('includes/functions.php');
//Variables and Arrays to sotre validation errors
$checkfail = array();
$checkfail = NULL;
$emailsuccess = array();
$emailsuccess = NULL;
$emailfail = array();
$emailfail = NULL;
$checkuser = array();
$checkuser = NULL;
$checkpass = array();
$checkpass = NULL;
$checkpass1 = array();
$checkpass1 = NULL;
$checkemail = array();
$checkemail = NULL;
$checkfname = array();
$checkfname = NULL;
$checklname = array();
$checklname = NULL;
$checkadd = array();
$checkadd = NULL;
$checkcity = array();
$checkcity = NULL;
$checkstate = array();
$checkstate = NULL;
$checkcountry = array();
$checkcountry = NULL;
$checkzip = array();
$checkzip = NULL;
$errflg = false;
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return clean($str);
}
//Sanitize the POST values
if(isset($_POST['register'])){
$username = clean($_POST['username']);
$password = clean($_POST['password']);
$password1 = clean($_POST['password1']);
$email = clean($_POST['email']);
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$add = clean($_POST['add']);
$alt = clean($_POST['alt']);
$city = clean($_POST['city']);
$state = clean($_POST['state']);
$country = clean($_POST['country']);
$zip = clean($_POST['zip']);
$phone = clean($_POST['phone']);
//Input Validations
//Check Username
if(empty($username)){ $errflg = TRUE; ($checkuser = 'You must create a Username.'); }
//Check Username Length
elseif(strlen($username) < 5) {$errflg = TRUE; ($checkuser = 'Your Username must be longer than 5 characters. '); }
elseif(strlen($username) > 12) {$errflg = TRUE; ($checkuser = 'Your Username must not be longer than 12 characters. '); }
//Check for Duplicate Username
elseif(!empty($username)) {
$qry = "SELECT * FROM users WHERE username='$username'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) { $errflg = TRUE; ($checkuser = 'That Username has already been taken');
}
@mysql_free_result($result);
}
}
//Check Password 1
if(empty($password)){ $errflg = TRUE; ($checkpass = 'You must create a Password'); }
//Check Password 2
elseif(empty($password1)){ $errflg = TRUE; ($checkpass1 = 'You must Re-type your Password'); }
//Check Password Match
elseif($password != $password1) { $errflg = TRUE; ($checkpass = 'Your password does not match.');}
//Check Email
if(empty($email)){ $errflg = TRUE; ($checkemail = 'You must type in your Email.');
//Check Email Valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $errflg = TRUE; ($checkemail = 'You must enter a valid email.'); }
//Check Name
if(empty($fname)){ $errflg = TRUE; ($checkfname = 'You must enter your First Name'); }
if(empty($lname)){ $errflg = TRUE; ($checklname = 'You must enter your Last Name'); }
//Check Address
if(empty($add)){ $errflg = TRUE; ($checkadd = 'You must enter your Street Address'); }
if(empty($city)){ $errflg = TRUE; ($checkcity = 'You must enter your City/Town'); }
if(empty($state)){ $errflg = TRUE; ($checkstate = 'You must select your State'); }
if(empty($country)){ $errflg = TRUE; ($checkcountry = 'You must select your Country'); }
if(empty($zip)){ $errflg = TRUE; ($checkzip = 'You must enter your Zip/Postal Code'); }
//Validation error flag
if ($errflg = TRUE); { $checkfail = 'Sorry, but we ran into problems while processing your information.<br />Please check that all required fields have been completed properly.';
}
//No errors continue signup
//Create INSERT query
$qry = "INSERT INTO users(NULL,'$username','".md5($_POST['password'])."','$email','$fname','$lname','$add','$alt','$city','$state','$country','$zip','$phone',0)";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
//get the new user id
$userid = mysql_insert_id();
//create a random key
$key = $username . $email . date('mY');
$key = md5($key);
//add confirm row
$confirm = mysql_query("INSERT INTO `confirm` VALUES(NULL,'$userid','$key','$email')");
if($confirm){
//include the swift class
include_once './includes/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){ $emailsuccess = 'Thank you for registering at CH Media.Net. Please check your email and follow the link to verify your account.';
//email sent
}elseif(!send_email($info)) { $emailfail = 'Sorry there has been an error! We could not confirm your email, please try again.' . mysql_error(); }
}
}
}
?>
HTML
<!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=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="loginForm" name="loginForm" method="post" action=""><fieldset>
<table width="650" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<th colspan="3"><?php print $checkfail; print $emailsuccess; print $emailfail; ?></th>
</tr>
<tr>
<th width="140"><label for="username">Username:</label></th>
<td width="190"><input name="username" type="text" class="textfield" id="username" value="<?php echo $username ; ?>" /></td>
<td><?php print $checkuser; ?></td>
</tr>
<tr>
<th><label for="password">Password:</label></th>
<td><input name="password" type="password" class="textfield" id="password" /></td>
<td><?php print $checkpass; ?></td>
</tr>
<tr>
<th><label for="password1">Re-type Password:</label></th>
<td><input name="password1" type="password" class="textfield" id="password1" /></td>
<td><?php print $checkpass1; ?></td>
</tr>
<tr>
<th><label for="email">Email:</label></th>
<td><input name="email" type="text" class="textfield" id="email" value="<?php echo $email ; ?>" /></td>
<td><?php print $checkemail; ?></td>
</tr>
<tr>
<th><label for="fname">First Name:</label></th>
<td><input name="fname" type="text" class="textfield" id="fname" value="<?php echo $fname ; ?>" /></td>
<td><?php print $checkfname; ?></td>
</tr>
<tr>
<th><label for="lname">Last Name:</label></th>
<td><input name="lname" type="text" class="textfield" id="lname" value="<?php echo $lname ; ?>" /></td>
<td><?php print $checklname; ?></td>
</tr>
<tr>
<th><label for="add">Address :</label></th>
<td><input name="add" type="text" class="textfield" id="add" value="<?php echo $add ; ?>" /></td>
<td><?php print $checkadd; ?></td>
</tr>
<tr>
<th><label for="alt">Address Line 2 :</label></th>
<td><input name="alt" type="text" class="textfield" id="alt" value="<?php echo $alt ; ?>" /></td>
<td> </td>
</tr>
<tr>
<th><label for="city">City or Town :</label></th>
<td><input name="city" type="text" class="textfield" id="city" value="<?php echo $city ; ?>" /></td>
<td><?php print $checkcity; ?></td>
</tr>
<tr>
<th><label for="state">State :</label></th>
<td><select name="state" class="textfield" id="state" size="1">
<option selected value="">State...</option>
<option value="None">None</option>
<option value="">-- UNITED STATES --</option>
MORE OPTIONS
</select></td>
<td><?php print $checkstate; ?></td>
</tr>
<tr>
<th><label for="country">Country :</label></th>
<td><select name="country" class="textfield" id="country">
<option value="">Country...</option>
MORE OPTION
</select></td>
<td><?php print $checkcountry; ?></td>
</tr>
<tr>
<th><label for="zip">Zip / Postal Code:</label></th>
<td><input name="zip" type="text" class="textfield" id="zip" value="<?php echo $zip ; ?>" /></td>
<td><?php print $checkzip; ?></td>
</tr>
<tr>
<th><label for="phone">Phone:</label></th>
<td><input name="phone" type="tel" class="textfield" id="phone" value="<?php echo $phone ; ?>" /></td>
<td> </td>
</tr>
<tr>
<th> </th>
<td><input type="submit" name="register" value="Register" /></td>
<td> </td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>