Web Dev Course – Signup Form, Sessions and Cookies… Oh my!

Peter Tsiampas Development 0 Comments

Lecture 211 – MYSQL Create row, search.

The Section on mysql was pretty standard until Rob asked to create a user signup web form and do basic validation against the database.

The requirements were:

  • Ask for email and password
  • Check that the email and password have been entered
  • Check that the email is not already registered
  • Sign up the user.

As I went a little overboard with the coding when I compared it to what Rob did, but mine worked and his worked so all good.


Lecture 212 – Session Variables

Using Sessions seems like its been simplified over the years since I last used them, pretty much as long as I have session_start() (line 8) at the beginning of the PHP code I can access and set variables as required.

Rob also introduced the header() (line 15) function which allows for modifying the header information in a page, he demonstrated how easy it is to redirect to a different page.

<?php
/**
 * User: Peter Tsiampas
 * Date: 4/06/2016
 * Time: 12:29 PM
 */
session_start();

if($_SESSION['email']) {
    echo $_SESSION['email'];
    echo "You have been logged in successfully.";
} else {
    header("Location: index.php");
}

Lecture 214 – Cookies

Cookies are just as easy to work with, Rob gave several examples of how to set the cookie (Line 8), parameters are name, value, time in the future to live which is done by using the time() function and adding an appropriate amount of time; (time() + 60 sec * 60 min * xxHours * xx Days) pretty simple method which I like.

To remove a cookie to set the time in the past (line 10) and that delete’s the cookie.

Accessing the cookie is like any other named array pretty simple really (Line 13).

<?php 
/**
 * User: Peter Tsiampas
 * Date: 4/06/2016
 * Time: 12:43 PM
 * Subject: Cookies
 */ 
setcookie("customerId", "1234", time() + 60 * 60 * 24); 

setcookie("customerId", "", time() - 60 * 60); 

// update the value of the cookie
$_COOKIE['customerId'] = "test"; 
?>

So far so good, well the next lecture is going to be the big one, not sure what he has installed for my next challenge but I know its going to be fun.

Leave a Reply

Your email address will not be published. Required fields are marked *