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>
menu.php
<nav> <ul> <li><a href="lists.php">Lists</a></li> <li><a href="tables.php">Tables</a></li> <li><a href="layout.php">Layout</a></li> <li><a href="media.php">Media</a></li> <li><a href="mysql.php">Databases</a></li> <li><a href="form.php">Forms</a></li> <li><a href="login.php">Login</a></li> <li><a href="logout.php">Logout</a></li> </ul> </nav>
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));
<h2><?php echo $_SESSION['profile']['name']; ?></h2>
h2 { padding-left: 40px; }
<?php session_start(); unset($_SESSION['profile']); header("Location: login.php"); ?>
session_start()
starts the session so we have access to the session data.unset
deletes the session data so it won't be there when other pages try to access it.<nav> <h2><?php echo $_SESSION['profile']['name']; ?></h2> <ul> <li><a href="lists.php">Lists</a></li> <li><a href="tables.php">Tables</a></li> <li><a href="layout.php">Layout</a></li> <li><a href="media.php">Media</a></li> <li><a href="mysql.php">Databases</a></li> <li><a href="form.php">Forms</a></li> <li><a href="login.php">Login</a></li> <li><a href="logout.php">Logout</a></li> </ul> </nav>
if (!isset($_SESSION['profile'])) { header("Location:login.php"); }
session_start();
in 'header.php'.$filename = basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']); error_log("MURRAY: " . $filename);
$_SERVER['REQUEST_URI']
is the full string entered into the address bar of the browser.$_SERVER['QUERY_STRING']
is everything in the address bar after '?'.basename
takes a URL and returns everything from the filename on. By adding '?' . $_SERVER['QUERY_STRING']
as a parameter, we're telling it to exclude the '?' and everything afterwards.error_log
.if (($filename != 'login.php') && !isset($_SESSION['profile'])) { header("Location:login.php"); }