Jump to content
Larry Ullman's Book Forums

Recommended Posts

<?php # Script 16.6 - register.php

 

// This is the registration page for the site.

 

 

require_once ('includes/config.inc.php');

$page_title = 'Register';

 

include ('includes/header.html');

 

 

 

if (isset($_POST['submitted'])) { // Handle the form.

 

 

require_once ('mysqli_connect.php');

 

 

 

// Trim all the incoming data:

$trimmed = array_map('trim', $_POST);

 

// Assume invalid values:

$fn = $ln = $e = $p =$ad =$ph = FALSE;

 

 

 

// Check for a first name:

 

if (preg_match ('/^[A-Z \'.-]{2,20}$/i',

$trimmed['first_name'])) {

 

$fn = mysqli_real_escape_string ($dbc,

 

$trimmed['first_name']);

 

} else {

echo '<p class="error">Please enter

your first name!</p>';

 

 

}

 

 

 

 

// Check for a last name:

 

if (preg_match ('/^[A-Z \'.-]{2,40}$/i',

$trimmed['last_name'])) {

 

$ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);

 

} else {

echo '<p class="error">Please enter

your last name!</p>';

 

 

 

 

}

 

 

 

// Check for an email address:

 

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {

 

$e = mysqli_real_escape_string ($dbc,

$trimmed['email']);

 

}else {

echo '<p class="error">Please enter a

valid email address!</p>';

 

}

// Check for a password and match against the confirmed password:

if (preg_match ('/^\w{4,20}$/',

$trimmed['password1']) ) {

 

if ($trimmed['password1'] ==

$trimmed['password2']) {

 

$p = mysqli_real_escape_string

($dbc, $trimmed['password1']);

 

} else {

 

echo '<p class="error">Your password did not match the confirmed

password!</p>';

 

}

 

} else {

 

echo '<p class="error">Please enter a

valid password!</p>';

 

}

if ($fn && $ln && $e && $p) { // If everything's OK...

// Make sure the email address is available:

$q = "SELECT customer_id FROM customers WHERE email='$e'";

 

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL

Error: " . mysqli_error($dbc));

 

 

if (mysqli_num_rows($r) == 0) { // Available.

// Create the activation code:

 

$a = md5(uniqid(rand(), true));

 

 

 

// Add the user to the database:

 

$q = "INSERT INTO customers (email,pass, first_name, last_name, registration_date) VALUES ('$e',SHA1('$p'), '$fn', '$ln', NOW() )";

 

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL

Error: " . mysqli_error($dbc));

 

 

 

if (mysqli_affected_rows($dbc) == 1)

{ // If it ran OK.

 

 

 

// Send the email:

 

$body = "Thank you for registering

at YourDesigner.com. To activate your account, please click on this

link:www.yourdesigner.com\n\n";

$body .= BASE_URL . 'activate.php? x=' . urlencode($e) . "&y=$a";

 

mail($trimmed['email'],'Registration Confirmation', $body, 'From: lalarukh@yourdesigner.com');

// Finish the page:

 

echo '<h3>Thank you for

registering! A confirmation email

has been sent to your address.

Please click on the link in that

 

email in order to activate your

 

account.</h3>';

 

 

include ('includes/footer.html');

 

// Include the HTML footer.

exit(); // Stop the page.

} else { // If it did not run OK.

 

echo '<p class="error">You could

not be registered due to a system

error. We apologize for any inconvenience.</p>';

 

}

 

 

} else { // The email address is not available.

 

echo '<p class="error">That email address has already been registered.If you have forgotten your password, use the link at right to have your password sent to you.</p>';

 

}

 

} else { // If one of the data tests failed.

echo '<p class="error">Please re-enter your passwords and try again.</p>';

 

}

 

mysqli_close($dbc);

 

} // End of the main Submit conditional.

 

?>

<style type="text/css">

label { display:block;

 

float: left;

width: 10em;

margin-right: 1em;

text-align: left;

}

fieldset.submit {

float: none;

width: auto;

border: 0 none #FFF;

padding-left: 12em;

}

 

</style>

 

 

 

<p> First fill the registeration form and register yourself before you can order. A confirmation email will be sent to your email address upon registering. </p>

<h1>Register</h1>

<form action="register.php" method="post" style="border:medium" >

 

<div class="fieldset" align="center">

 

 

<fieldset>

<legend><b>Register<b></legend>

 

 

<p><label>First name:</label><input type="text"

name="first_name" size="20" maxlength=

"20" value="<?php if (isset($trimmed ['first_name'])) echo $trimmed

['first_name']; ?>" />

</p>

<p><label>Last Name:</label> <input type="text"

name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed ['last_name'])) echo $trimmed ['last_name']; ?>" /></p>

 

 

 

 

<p><label>Email:</label> <input type="text" name="email" size="20"

maxlength="80" value="<?php if (isset($trimmed['email'])) echo

$trimmed['email']; ?>" /> </p>

 

<p><label>Password:</label> <input type= "password" name="password1" size="20" maxlength="20" /></p>

<p><small><i>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></i></p>

<p><label>Confirm Password:</label> <input type="password" name="password2"

size="20" maxlength="20" /></p>

</fieldset>

<fieldset>

<div align="center"><input type="submit"

name="submit" value="Register" /></div>

 

<input type="hidden" name="submitted"

value="TRUE" />

</fieldset>

 

 

</form>

 

<?php // Include the HTML footer.

 

include ('includes/footer.html');

 

?>

 

 

 

 

this is my code. when i click submit after entering the values it always returns the first page of my site instead of any error messages in case of leaving something or submitted message. what probably is wrong ?

Link to comment
Share on other sites

Your form is calling register.php. Is that what you want?

Instead of posting a ton of code and asking us to fix everything, you should do a little investigating yourself and try to at least narrow down the problem area, and then come back with less code and more of an explanation.

  • Upvote 1
Link to comment
Share on other sites

My best advice is to follow the error-debugging chapter in Larry's book to help you debug your problems.

Related to that, if you want a simpler solution, start echoing lots of values to the screen at various points in your code to figure out where things are hanging up and/or where you're not getting the values you expect. More than likely, all your problems are centered around one central error.

  • Upvote 1
Link to comment
Share on other sites

Actually there was another form in my header file and it was calling another page and this header page is included in this code register.php and the form it this code was also calling the same page as was in header file. when i removed the form form from header file , register.php started calling register.php. so resolved :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...