This is all the code. The is that someone logs in and then goes the index page which displays their unique ID number (I will change this profile name)
Login Page
Code:
// Reset errors and success messages
$errors = array();
$success = array();
// Login attempt
if(isset($_POST['loginSubmit']) && $_POST['loginSubmit'] == 'true'){
$loginEmail = trim($_POST['email']);
$loginPassword = trim($_POST['password']);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if (!$email)
{
$error = 'Please enter your email address in a valid format. Example: bobsmith@companyname.com';
}
if(strlen($loginPassword) < 6 || strlen($loginPassword) > 12)
$errors['loginPassword'] = 'Your password must be between 6-12 characters.';
if(!$errors){
$query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($loginEmail) . '" AND password = MD5("' . $loginPassword . '") LIMIT 1';
$result = mysql_query($query);
if(mysql_num_rows($result) == 1){
$user = mysql_fetch_assoc($result);
$query = 'UPDATE users SET session_id = "' . session_id() . '" WHERE id = ' . $user['id'] . ' LIMIT 1';
mysql_query($query);
header('Location: index.php');
exit;
}else{
$errors['login'] = 'No user was found with the details provided.';
}
}
}
// Register attempt
if(isset($_POST['registerSubmit']) && $_POST['registerSubmit'] == 'true'){
$registerEmail = trim($_POST['email']);
$registerPassword = trim($_POST['password']);
$registerConfirmPassword = trim($_POST['confirmPassword']);
if(strlen($registerPassword) < 6 || strlen($registerPassword) > 12)
$errors['registerPassword'] = 'Your password must be between 6-12 characters.';
if($password != $confirmPassword && !$error) {
$error = "The passwords you entered did not match.";
}
$emailAddress = filter_var($_POST['emailaddress'], FILTER_VALIDATE_EMAIL);
if (!$emailAddress)
{
$error = 'Please enter your email address in a valid format. Example: bobsmith@companyname.com';
}
if($registerPassword != $registerConfirmPassword)
$errors['registerConfirmPassword'] = 'Your passwords did not match.';
// Check to see if we have a user registered with this email address already
$query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($registerEmail) . '" LIMIT 1';
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
$errors['registerEmail'] = 'This email address already exists.';
if(!$errors){
$query = 'INSERT INTO users SET email = "' . mysql_real_escape_string($registerEmail) . '",
password = MD5("' . mysql_real_escape_string($registerPassword) . '"),
date_registered = "' . date('Y-m-d H:i:s') . '"';
if(mysql_query($query)){
$success['register'] = 'Thank you for registering. You can now log in on the left.';
}else{
$errors['register'] = 'There was a problem registering you. Please check your details and try again.';
}
}
}
$query = mysql_query("SELECT id FROM users WHERE email = '".$email."' LIMIT 1");
if(mysql_num_rows($query) > 0 && !$error) {
$error = "Sorry, that email is already in use!";
}
print_r($website);
if(!$error) {
$query = mysql_query("INSERT INTO users (email) VALUES ('".$password."', '".$password."', '".mysql_real_escape_string(md5($password))."', '".$email."')");
if($query) {
$message = "Hello ".$_POST['email'].",\r\n\r\nThanks for registering with EventVital.com! We hope you enjoy your stay.\r\n\r\n Many Thanks,\r\n.com";
$headers = "From: ".$website['name']." <".$website['email'].">\r\n";
mail($_POST['email'], "Welcome", $message, $headers);
setcookie("user", mysql_insert_id(), $time);
setcookie("pass", mysql_real_escape_string(md5($password)), $time);
header("Location: users.php");
} else {
$error = "There was a problem with the registration. Please try again.";
}
}
echo $_POST['email'];
echo $message;
echo $headers;
?>
Index
Code:
<?php
$query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($email) . '" LIMIT 1';
if ($query && mysql_num_rows($query) === 1) // query was successful and returned 1 row
{
$row = mysql_fetch_array($query);
echo '<li>You are sucessfully logged in as ' . $row['id'] . '</li>';
} else {
echo '<a href="/blog/blog.php" >Blog</a>';
}
?>
Bookmarks