Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi
I have the following codes for registration and login. The registration doesnt seem to work, can anybody point out where the problem is or what am i not doing, should there be a text file included?

?<?php // Script 8.10 - register.php #2
/* This page lets people register for the site (in theory). */

// Set the page title and include the header file:
define('TITLE', 'Register');
include('header.html');

// Print some introductory text:
print '<h2>Registration Form</h2>
<p>Register so that you can take advantage of certain features like this, that, and the other thing.</p>';

// Add the CSS:
print '<style type="text/css" media="screen">
.error { color: red; }
</style>';

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$problem = FALSE; // No problems so far.

// Check for each value...
if (empty($_POST['first_name'])) {
$problem = TRUE;
print '<p class="error">Please enter your first name!</p>';
}

if (empty($_POST['last_name'])) {
$problem = TRUE;
print '<p class="error">Please enter your last name!</p>';
}

if (empty($_POST['email']) || (substr_count($_POST['email'], '@') != 1) ) {
$problem = TRUE;
print '<p class="error">Please enter your email address!</p>';
}

if (empty($_POST['password1'])) {
$problem = TRUE;
print '<p class="error">Please enter a password!</p>';
}

if ($_POST['password1'] != $_POST['password2']) {
$problem = TRUE;
print '<p class="error">Your password did not match your confirmed password!</p>';
}

if (!$problem) { // If there weren't any problems...

// Print a message:
print '<p>You are now registered!<br />Okay, you are not really registered but...</p>';

// Send the email:
$body = "Thank you for registering with the J.D. Salinger fan club! Your password is '{$_POST['password1']}'.";
mail($_POST['email'], 'Registration Confirmation', $body, 'From: admin@example.com');

// Clear the posted values:
$_POST = array();

} else { // Forgot a field.

print '<p class="error">Please try again!</p>';

}

} // End of handle form IF.

// Create the form:
?>
<form action="register.php" method="post">

<p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($_POST['first_name'])) { print htmlspecialchars($_POST['first_name']); } ?>" /></p>

<p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($_POST['last_name'])) { print htmlspecialchars($_POST['last_name']); } ?>" /></p>

<p>Email Address: <input type="text" name="email" size="20" value="<?php if (isset($_POST['email'])) { print htmlspecialchars($_POST['email']); } ?>" /></p>

<p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($_POST['password1'])) { print htmlspecialchars($_POST['password1']); } ?>" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($_POST['password2'])) { print htmlspecialchars($_POST['password2']); } ?>" /></p>

<p><input type="submit" name="submit" value="Register!" /></p>

</form>

<?php include('footer.html'); // Need the footer. ?>



<?php // Script 8.13 - login.php #2
/* This page lets people log into the site (in theory). */

// Set the page title and include the header file:
define('TITLE', 'Login');
include('header.html');

// Print some introductory text:
print '<h2>Login Form</h2>
<p>Users who are logged in can take advantage of certain features like this, that, and the other thing.</p>';

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// Handle the form:
if ( (!empty($_POST['email'])) && (!empty($_POST['password'])) ) {

if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') ) { // Correct!

// Redirect the user to the welcome page!
ob_end_clean(); // Destroy the buffer!
header ('Location: welcome.php');
exit();

} else { // Incorrect!

print '<p>The submitted email address and password do not match those on file!<br />Go back and try again.</p>';

}

} else { // Forgot a field.

print '<p>Please make sure you enter both an email address and a password!<br />Go back and try again.</p>';

}

} else { // Display the form.

print '<form action="login.php" method="post">
<p>Email Address: <input type="text" name="email" size="20" /></p>
<p>Password: <input type="password" name="password" size="20" /></p>
<p><input type="submit" name="submit" value="Log In!" /></p>
</form>';

}

include('footer.html'); // Need the footer.
?>

 

 

 

 

Im getting these erros with the register and login codes:<!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>Register</title>
<style type="text/css" media="screen">
.error { color: red; }
</style>
</head>
<body>
<h1>Register</h1>
<?php // Script 11.6 - register.php
/* This script registers a user by storing their information in a text file and creating a directory for them. */

// Identify the directory and file to use:
$dir = '../users/';
$file = $dir . 'users.txt';

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

$problem = FALSE; // No problems so far.

// Check for each value...
if (empty($_POST['username'])) {
$problem = TRUE;
print '<p class="error">Please enter a username!</p>';
}

if (empty($_POST['password1'])) {
$problem = TRUE;
print '<p class="error">Please enter a password!</p>';
}

if ($_POST['password1'] != $_POST['password2']) {
$problem = TRUE;
print '<p class="error">Your password did not match your confirmed password!</p>';
}

if (!$problem) { // If there weren't any problems...

if (is_writable($file)) { // Open the file.

// Create the data to be written:
$subdir = time() . rand(0, 4596);
$data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL;

// Write the data:
file_put_contents($file, $data, FILE_APPEND | LOCK_EX);

// Create the directory:
mkdir ($dir . $subdir);

// Print a message:
print '<p>You are now registered!</p>';

} else { // Couldn't write to the file.
print '<p class="error">You could not be registered due to a system error.</p>';
}

} else { // Forgot a field.
print '<p class="error">Please go back and try again!</p>';
}

} else { // Display the form.

// Leave PHP and display the form:
?>

<form action="register.php" method="post">
<p>Username: <input type="text" name="username" size="20" /></p>
<p>Password: <input type="password" name="password1" size="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" /></p>
<input type="submit" name="submit" value="Register" />
</form>

<?php } // End of submission IF. ?>
</body>
</html>

Register

Please enter a username!'; } if (empty($_POST['password1'])) { $problem = TRUE; print '
Please enter a password!
'; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print '
Your password did not match your confirmed password!
'; } if (!$problem) { // If there weren't any problems... if (is_writable($file)) { // Open the file. // Create the data to be written: $subdir = time() . rand(0, 4596); $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); // Create the directory: mkdir ($dir . $subdir); // Print a message: print '
You are now registered!
'; } else { // Couldn't write to the file. print '
You could not be registered due to a system error.
'; } } else { // Forgot a field. print '
Please go back and try again!
'; } } else { // Display the form. // Leave PHP and display the form: ?>
Username:
Password:
Confirm Password:


<!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>Login</title>
</head>
<body>
<h1>Login</h1>
<?php // Script 11.8 - login.php
/* This script logs a user in by check the stored values in text file. */

// Identify the file to use:
$file = 'users/users.txt';

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

$loggedin = FALSE; // Not currently logged in.

// Enable auto_detect_line_settings:
ini_set('auto_detect_line_endings', 1);

// Open the file:
$fp = fopen($file, 'rb');

// Loop through the file:
while ( $line = fgetcsv($fp, 200, "\t") ) {

// Check the file data against the submitted data:
if ( ($line[0] == $_POST['username']) AND ($line[1] == md5(trim($_POST['password']))) ) {

$loggedin = TRUE; // Correct username/password combination.

// Stop looping through the file:
break;

} // End of IF.

} // End of WHILE.

fclose($fp); // Close the file.

// Print a message:
if ($loggedin) {
print '<p>You are now logged in.</p>';
} else {
print '<p style="color: red;">The username and password you entered do not match those on file.</p>';
}

} else { // Display the form.

// Leave PHP and display the form:
?>

<form action="login.php" method="post">
<p>Username: <input type="text" name="username" size="20" /></p>
<p>Password: <input type="password" name="password" size="20" /></p>
<input type="submit" name="submit" value="Login" />
</form>

<?php } // End of submission IF. ?>

</body>
</html>


Login

You are now logged in.'; } else { print '
The username and password you entered do not match those on file.

'; } } else { // Display the form. // Leave PHP and display the form: ?>
Username:
Password:

 

 

This is the live one:http://project2013.net78.net/login.php

Share this post


Link to post
Share on other sites

You've highlighted parts in red but you don't say HOW it doesn't work. What isn't happening but should? What is happening but shouldn't? Also, what version of PHP are you using?

Share this post


Link to post
Share on other sites

Greetings,

 

I am having an issue with script 8.9 and 8.10 PHP for the web 4th edition.

Script_8_9.jpg

 

The form is not completing the registration, The information is in the form and I can print it out however I still get the error message and the form does not process. I have comared the code with the example code for the book and I am at a loss. I am thinking I am going to go do something else for a while nd come back to it. I am certain it must be me but I'll be hanged if I can figure it out.

 

This is where I think I have an error that I can't seem to determine:

 

// qualify print statement and print

   // if there were not any problems

   if (!problem) {

   // opening no problem curly

   // print no problem statement

   echo '<p> Well if looks like you can fill out a form without too many errors not bad</p>'."\n";

  

   // clear posted values

   $POST = array();

  

   // closing no problem curly

   } else {

   // opening problem curly

   echo '<p class="error">What a simpleton you can not even complete a simple form go back and try again.</p>'."\n";

  }

   // closing problem curly

   // end qualify print

 

I notice that when I submit the information remains available. Isn't it supposed to clear as it is passed? I been staring at it for to long and need to take a break. Hopefully you can point me in the right direction.

Share this post


Link to post
Share on other sites

This is syntactically invalid:

 

 

 

if (!problem) {
 
Notice the lack of a dollar sign there. I suspect PHP is treating this like a constant, then. If you were running this with error messages on the highest level, you should have seen something about this.

Share this post


Link to post
Share on other sites

DOAH!!!! That was it. Too long reading and studying to see the nose on my face. Larry I want to thank you again for having a writing style that is both educational and easy to comprehend. You would not believe what you have enabled me to do already (well maybe you would it is your goal after all). I look forward to owning all of your books as related to php SQL and Java. The quality of my services has increased one hundred fold in the last few days my thanks to you for pulling together the pieces!

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...