Week 8 : Advanced PHP

Introduction
Following up on the previous week's post, here is some more advanced php stuff to learn from. In this post we will cover forms, session, cookies, useful variables & more.

Forms
User input is a vital part of the web, without this function, most of the scripts on the internet wouldn't work because most of them require some form of user input. Forms make it possible for a web developer to gather input from a user. Here is a sample HTML form that requests input from a user & its equivalent PHP script

<!-- HTML FORM -->
<form action = "request_form.php" method = "POST">
<p>
<label for = "username">Username</label>
<input name = "username" id = "username" type = "text" />
</p>
<p>
<label for = "password">Password</label>
<input name = "password" id = "password" type = "password" />
</p>
<p>
<button>Login</button>
</p>



</form>

<?php 
//PHP script

//Var that specifies if the user is logged in
$is_user = false;


//if there was a post request
if(isset($username = $_POST['username']) && isset($password = $_POST['password'])

$is_user = check_user($username,$password); // set the user as logged in

}

/*
  Function that checks if a user is valid
  @param1 username - string username to check
  @param2 password - string password to check
*/
function check_user($username,$password)
{
//Var that specifies if the user is logged in
$is_user = false;

//Array that defines a username and password
$creds = array("username"=>"demo"
               "password"=>"password");

}

if($username == $creds['username'] && $password == $creds['password'])
{
$is_user = true;
}

return $is_user;

?>


Forms can use two methods of sending information to the web server which are the Post & Get methods and the difference between them is that when using the GET the data posted will be posted through the url using a Query String but the Post method will send the data through headers so these will be invisible to the user. In PHP to gather this input you can use $_POST['var_name'] for posts & $_GET['var_name'] for get. $_REQUEST['var_name']  gives you the option to get variables posted through both GET,Post & also contains the data accessible via the $_COOKIE method.

Useful Variables 

There are various in built methods and variables in php's core. Here are some of the useful variables a web developer may come in use:
$_GLOBALS //Refers to all the variables in the global scope
$_SERVER // Contains Server & environment information
        $_SERVER['PHP_SELF']// Current executing file name
        $_SERVER['SERVER_ADDR']// The ip address of the server
        $_SERVER['SERVER_NAME']// The name of the server host
        $_SERVER['DOCUMENT_ROOT']// The document root directory
        $_SERVER['HTTP_REFERRER']// The previous page the user was on before visiting this page
        $_SERVER['HTTP_USER_AGENT']// The user agent of the visitor
        $_SERVER['REMOTE_ADDR']// The ip address of the user
$_GET // Associative array containing variables posted via a get form
$_POST // Associative array containing variables posted via a post form
$_SESSION // Associative array containing variables stored in the Session
$_COOKIE // An Associative array containing variables stored in the Cookies



Mail
Sending emails through web application has grown over the past few years and php has taken care of that. In its core, php has an in built function that send email, here's an example code to send an email.

mail($to,$subject,$message) //Refers to all the variables in the global scope

Cookies
Sometimes a web developer would need to store persistent data into a client's pc for example user credentials so the user won't have to login every time he visits a new page. Here is some example code one can use to store, access and delete a cookie.

setcookie('username', 'demo', time() + 4800); /sets a cookie



$_COOKIE['username']; //Read a cookie


setcookie ('userName', '', time() - 4800); // Deletes the cookie


SESSIONS
In today's advanced websites, information needs to be consistently stored. Variables is a way to store data but after the page refreshes, that variable is lost forever unless it is stored in a file, cookie or a database. In order to keep variables between different pages there's a special variable which is called a Session cookie which enables someone to store data throughout the pages and is cleared out when the session expires. This type of variable is generally used for login information including purposes such as storing of personal data & user type.
session_start(); //starts a session

$_SESSION['username']; //Reads a Session

unset($_SESSION['username']);//Deletes only the username variable from the session

session_destroy()// Deletes the Session



Lab Session
1. Create a login for the user
<!-- HTML FORM -->
<form action = "request_form.php" method = "POST">
<p>
<label for = "username">Username</label>
<input name = "username" id = "username" type = "text" />
</p>
<p>
<label for = "password">Password</label>
<input name = "password" id = "password" type = "password" />
</p>
<p>
<button>Login</button>
</p>



</form>

<?php 
//PHP script

//Var that specifies if the user is logged in
$is_user = false;


//if there was a post request
if(isset($username = $_POST['username']) && isset($password = $_POST['password'])

$is_user = check_user($username,$password); // set the user as logged in

}

/*
  Function that checks if a user is valid
  @param1 username - string username to check
  @param2 password - string password to check
*/
function check_user($username,$password)
{
//Var that specifies if the user is logged in
$is_user = false;

//Array that defines a username and password
$creds = array("username"=>"demo"
               "password"=>"password");

}

if($username == $creds['username'] && $password == $creds['password'])
{
$is_user = true;
}

return $is_user;

?>



2. Store a cookie with a remember me option

//added in the html part
<p>
<label for = "remember_me">Remember me</label>
<input type = "checkbox" name = "remember_me" id = "remember_me" />
</p>

<?php 
//PHP script

//Added to the php script
function set_cookie($key,$username){
setcookie($key, $username', time() + 4800); /sets a cookie
}
function get_cookie($var){
return $_COOKIE[$var]; //Read a cookie
}


?>

3. Replace the cookie with a Session
<?php 
//PHP script
session_start(); //Added this to the beginning of the page

//Changed cookie function to this
function set_session($key,$username){
$_SESSION[$key] = $username
}
function get_cookie($key){
return $_SESSION[$key]; //Read a Session variable
}


?>

0 comments:

Post a Comment