<?php # Script from Oliver Bob Lagumen- register.php
// Set the page title and include the HTML header.
$page_title = 'Register';
include ('templates/header.inc'); // you will need to create your own header. I think you are familiar with this.
if (isset($_POST['submit'])) { // Handle the form.
// Register the user in the database.
require_once ('your_mysql_connect_page_which_is_a_separate_file.php'); // Connect to the db.
// Create a function for escaping the data.
function escape_data ($data) {
global $dbc; // Need the connection.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
} // End of function.
$message = NULL; // Create an empty new variable.
// Check for a first name.
if (empty($_POST['first_name'])) {
$fn = FALSE;
$message .= '<p>You forgot to enter your first name!</p>';
} else {
$fn = escape_data($_POST['first_name']);
}
// Check for a last name.
if (empty($_POST['last_name'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter your last name!</p>';
} else {
$ln = escape_data($_POST['last_name']);
}
// Check for an email address.
if (empty($_POST['email'])) {
$e = FALSE;
$message .= '<p>You forgot to enter your email address!</p>';
} else {
$e = escape_data($_POST['email']);
}
// Check for a username.
if (empty($_POST['username'])) {
$u = FALSE;
$message .= '<p>You forgot to enter your username!</p>';
} else {
$u = escape_data($_POST['username']);
}
// Check for a password and match against the confirmed password.
if (empty($_POST['password1'])) {
$p = FALSE;
$message .= '<p>You forgot to enter your password!</p>';
} else {
if ($_POST['password1'] == $_POST['password2']) {
$p = escape_data($_POST['password1']);
} else {
$p = FALSE;
$message .= '<p>Your password did not match the confirmed password!</p>';
}
}
if ($fn && $ln && $e && $u && $p) { // If everything's OK.
$query = "SELECT user_id FROM users WHERE username='$u'";
$result = @mysql_query ($query); // Run the query.
if (mysql_num_rows($result) == 0) {
// Make the query. This code is what you will use to prevent duplicate of usernames
$query = "INSERT INTO users (username, first_name, last_name, email, password, registration_date) VALUES ('$u', '$fn', '$ln', '$e', PASSWORD('$p'), NOW() )";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
// Send an email, if desired.
echo '<p><b>You have been registered!</b></p>';
include ('templates/footer.inc'); // Include the HTML footer.
exit(); // Quit the script.
} else { // If it did not run OK.
$message = '<p>You could not be registered due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>';
}
} else {
$message = '<p>That username is already taken.</p>';
}
mysql_close(); // Close the database connection.
} else {
$message .= '<p>Please try again.</p>';
}
} // End of the main Submit conditional.
// Print the error message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15" value="<?php if (isset($POST['first_name'])) echo $POST['first_name']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="30" maxlength="30" value="<?php if (isset($POST['last_name'])) echo $POST['last_name']; ?>" /></p>
<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="40" value="<?php if (isset($POST['email'])) echo $POST['email']; ?>" /> </p>
<p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if (isset($POST['username'])) echo $POST['username']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" /></p>
<p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Register" /></div>
</form><!-- End of Form -->
<?php // You will have to create your own footer file
include ('templates/footer.inc'); // Include the HTML footer.
?>
So you are excited ha.... May be, But you will need to code or to build the following in mysql or in phpmyadmin fields:
user_id, username, first_name, email and password and registration_date in order for this script to work for you. You can call your tabale "users" and your database "any_thing_you_like"
Now the login.php is here and I do not have to go to the details, but I wanted to help you to the codes you need:
<?php # - login.php
include ('nocache.php');
ob_start(); // Start output buffering.
$page_title = 'Login';
include ('templates/header.inc'); // Require the page header.
if (isset($_POST['submit'])) { // Check if the form has been submitted.
require_once ('your_mysql_connect_page_which_is_a_separate_file.php'); // Connect to the database.
function escape_data ($data) { // Function for escaping data.
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}
if (empty($_POST['username'])) { // Validate the username.
$u = FALSE;
echo '<p>You forgot to enter your username!</p>';
} else {
$u = escape_data($_POST['username']);
}
if (empty($_POST['password'])) { // Validate the password.
$p = FALSE;
echo '<p>You forgot to enter your password!</p>';
} else {
$p = escape_data($_POST['password']);
}
if ($u && $p) { // If everything's OK.
// Query the database.
$query = "SELECT user_id, first_name FROM users WHERE username='$u' AND password=PASSWORD('$p')";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) { // A match was made.
// Start the session, register the values & redirect.
session_start();
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
ob_end_clean(); // Delete the buffer.
header ("Location: [url]http://[/url]" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/loggedin.php"); //this part of the code brings you to loggedin.php before or to your index.php if it is successful which means that I will have to code that here for you.
exit();
} else { // No match was made.
echo '<p>The username and password entered do not match those on file.</p>';
}
mysql_close(); // Close the database connection.
} else { // If everything wasn't OK.
echo '<p>Please try again.</p>';
}
} // End of SUBMIT conditional.
?><center>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend><div align ="center">Enter your information in the form below:</div></legend>
<p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if (isset($POST['username'])) echo $_POST['username']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
</form><!-- End of Form --></center>
<?php include ('http://www.sharesiteph.com/index.htm');
include ('templates/footer.inc'); // Include the footer.
ob_end_flush(); // Send everything to the Web browser.
?>
Now be careful here (that code above), since you will need to be able to copy this without spaces so that your code will not report an error.
Now it is time for this last code:
<?php # - loggedin.php
ob_start(); // Start ouput buffering.
session_start(); // Start the session.
// Set the page title and include the HTML header.
$page_title = 'Logged In!';
include ('templates/include/header.inc');
// Check for a first name value.
if (isset($SESSION['first_name'])) {
echo "<center><p>You are now logged in, {$SESSION['first_name']}!</p></center>"; $g = "include_once ('myprofile.php')"; echo $g; echo 'this part is your assignment';
} else {
ob_end_clean(); // Delete the buffer.
header ("Location: [url]http://[/url]" . $SERVER['HTTP_HOST'] . dirname($SERVER['PHP_SELF']) . "/index.php");
exit(); // Quit the script.
}
include ('http://www.sharesiteph.com/newphp/');
include ('templates/include/footer.inc'); // Include the HTML footer.
ob_end_flush(); // Send everything to the Web browser.
?>
Hey remember that when you will use include(); or require functions(); you will need to really make sure to have those things exist in your directory where you want it. If you want that this will work properly for you or need to email me do so or, please visit here: www.sharesiteph.com/newphp/database
Note: Do not forget to notify me of your success.