Jump to content

Register/Login form and Sending Email


yrstruly

Recommended Posts

HiI have the following codes for registration and login. The registration doesnt seem to work, can anybody oint out where the problem is ?<?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.?>

Link to comment
Share on other sites

Im working throught tutorials from a book by Larry Ullman. I dont know if the page is going to log in only after i have developed and connected the database? From what i understand the page is suppose to register the user at this point and then send a link to your email adress and then you login that takes you to a welcome page. The welcome is already created, but dont display when i try to login. I dont think the registration is working, will it only worked after the datase base is connected?

Link to comment
Share on other sites

I'm not getting the registration email after i have registred, what am i doing wrong? It prints: You could not be registered due to a system error.ays:

Edited by yrstruly
Link to comment
Share on other sites

I cant see anything from the posted code which is returning that error message.

I dont know if the page is going to log in only after i have developed and connected the database?
You must have connect to database before you do anything with database.
I'm not getting the registration email after i have registred,
There could be many reasons for that (Assuming it register user to database but dont send email). To send email you need to have a email sever. from localhost you would neeed a sendmail like server and configure it properly to send email from your local server. Though some ISP may block email traffic. Third party sever usually have email server up and running. Some Email provider would block email too for spam. You can check spam folder of your email account too.
Link to comment
Share on other sites

You don't need a database if your code isn't trying to use a database. Obviously an actual registration and login system would need some sort of data store, but if you're not doing that yet then PHP isn't going to try to figure out that your code is registering users and refuse to work without a database or something like that.

It prints: You could not be registered due to a system error.ays:
That message isn't in the code anywhere, where is that coming from?
Link to comment
Share on other sites

At that point there is no data base connection.Only the codes given and then a page thats suppose to show after i have login.I have followed the steps in the book, so i dont know what i have been missing or doing wrong.Could it be that there need to be a text file to store the username n password when you register , which stores the data or what is missing?The sending of the email im also unlcear about as it dot not explain that you need a specific server ect?

Link to comment
Share on other sites

The registration code doesn't do anything except send an email, it doesn't save the user information in any sort of data store (database, text file, etc). So it's not registering anything, it's just sending an email. The login page checks against an email address and password that are hard-coded, it doesn't look that information up either: if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') ) { // Correct! You need a mail server to send email. If you installed your own test server for PHP then it probably does not have a mail server. The easiest way for sending email is just to get a free or low-cost PHP hosting account that you can use for testing things like this, rather than trying to install and configure your own mail server and firewall settings. Your ISP might not even allow mail to be sent from your account without their help.

Link to comment
Share on other sites

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

Edited by yrstruly
Link to comment
Share on other sites

It's becoming a trend with your questions that you are not actually running the PHP code. The browser should never display PHP code. If you are seeing PHP code, then it's not being executed. If the code was being executed then the browser wouldn't print the code, it would print the output of the code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...