Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Logging In with Cookies

You know what cookies are. You know the flow of the sign-in form. Now it’s time to code. Create signin.php and start with the basic outline:

<?php

require_once '../scripts/database_connection.php';
require_once '../scripts/view.php';

// If the user is logged in, the user_id cookie will be set
if (!isset($_COOKIE['user_id'])) {

  // See if a login form was submitted with a username for login
  if (isset($_REQUEST['username'])) {
    // Try and log the user in
    $username = mysql_real_escape_string(trim($_REQUEST['username']));
    $password = mysql_real_escape_string(trim($_REQUEST['password']));

    // Look up the user

    // If user not found, issue an error
  }

  // Still in the "not signed in" part of the if
  // Start the page, and we know there's no success or error message
  //   since they're just logging in
  page_start("Sign In");
?>

<html>
  <div id="content"....

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial