This is an old revision of the document!
In this activity you'll create a session and recover the session on each page.
login.php
<?php
include ('database.php');
error_log("MURRAY: " . print_r($_POST,1));
if ($_POST['formSubmit'] == 'register') {
$name = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
// check data in all fields
$message = "";
if (($name == "") || ($email == "") || ($password == "") || ($confirm == "")) {
$message .= "Please complete all required fields. ";
}
// check email format
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message .= "Please enter a valid email address. ";
}
// check if email exists
$query = "SELECT * FROM users WHERE email=?";
$args = array($email);
$rslt = $pdo->prepare($query);
$rslt->execute($args);
if ($row = $rslt->fetch()) {
$message .= "You already have an account. ";
}
// check passwords match
if ($password != $confirm) {
$message .= "Please ensure passwords match. ";
}
// process registration
if ($message == "") {
// hash password to make it secure
$hash = password_hash ($password, PASSWORD_BCRYPT);
// add user
$query = "INSERT INTO users (username, email, password) VALUES (?,?,?)";
$args = array($name, $email, $hash);
$rslt = $pdo->prepare($query);
if ($rslt->execute($args)) {
$message = "Your account has been created. Please log in.";
} else {
$message = "There was a problem adding your account.";
}
}
} else if ($_POST['formSubmit'] == 'login') {
$name = $_POST['username'];
$password = $_POST['password'];
// check data in all fields
$message = "";
if (($name == "") || ($password == "")) {
$message .= "Please complete all required fields. ";
}
// process login
if ($message == "") {
// check for user in database
$query = "SELECT id, password FROM users WHERE username=?";
$args = array($name);
$rslt = $pdo->prepare($query);
$rslt->execute($args);
if ($row = $rslt->fetch()) {
// user exists
$hash = $row['password'];
if (password_verify($password, $hash)) { // password matches
// create session
error_log('logged in');
} else {
$message = "That password is incorrect. Please try again.";
}
} else {
$message = "That user does not exist. Please try again.";
}
}
}
$page = "login";
include('header.php');
?>
<main>
<div id="status">
<p><?php echo $message; ?></p>
</div>
<form name="loginForm" action="login.php" method="post">
<fieldset>
<ul>
<li>
<label for="username">User Name:<span class="required">*</span></label>
<input type="text" name="username">
</li>
<li class="register">
<label for="email">Email:<span class="required">*</span></label>
<input type="email" name="email">
</li>
<li>
<label for="password">Password:<span class="required">*</span></label>
<input type="password" name="password">
</li>
<li class="register">
<label for="confirm">Confirm Password:<span class="required">*</span></label>
<input type="password" name="confirm">
</li>
</ul>
<input type="hidden" name="formSubmit" value="">
<input type="button" name="login" value="Log In" onclick="loginUser()">
<input class="register" type="button" name="register" value="Register" onclick="registerUser()">
</fieldset>
</form>
</main>
<?php
include('footer.php');
?>
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tech School</title>
<link rel="stylesheet" href="style.css">
<script src="<?php echo $page; ?>.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<header>
<h1>Tech School Web Development Course</h1>
</header>
include('header.php').//create session in 'login.php' session_start();
unset($_SESSION['profile']);
session_set_cookie_params(array('SameSite' => 'strict'));
$_SESSION['profile'] = array('id' => $row['id'], 'name' => $name);
error_log("MURRAY: " . print_r($_SESSION['profile'], 1));
header("Location: form.php");
header("Location: http://google.com"); but for pages on the same site, the relative address is usually simpler.<?php
session_start();
?>
<!DOCTYPE html>
error_log("MURRAY 'form.php': " . print_r($_SESSION['profile'], 1));