Sei sulla pagina 1di 12

PHP Day 4

http://www.php.net

Geshan Manandhar
Developer,
Young Innovations Private Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
HTML Forms
► <form name=“form1” method=“post”
action=“process_form.php”> …. </form>
► <input type=“text” name=“user_name”>
► <input type="password" name="pass_word“>
► <input type="submit" name="submit_form"
value="Submit" />
► For display formatted forms use tables.
► For more see
http://www.w3schools.com/html/html_forms.asp
GeshanManandhar.com 2
Require and Include
► require(‘path/to/file_name.php’)
► include(‘path/to/file_name.php’)
 Difference is require will throw a Fatal error is
file required is not found include will just throw
a warning.
► require_once(‘path/to/file_name.php’);
► include_once(‘path/to/file_name.php’);
 To require or include a file just once.

GeshanManandhar.com 3
Session Management
► session_start
— Initialize session data
► $_SESSION[‘index’] = “value”;
 $_SESSION[‘user_name’] = $user_name;
► session_destroy — Destroys all data
registered to a session

GeshanManandhar.com 4
A simple login interface

GeshanManandhar.com 5
Login Check function
► Getting Variables and starting session.
<?php
session_start();
$user_name = $_POST[‘user_name’];
$pass_word = $_POST[‘pass_word’];

GeshanManandhar.com 6
Login Check function
► The Function definition
function check_login($user_name, $pass_word){
if(($user_name=="admin") && ($pass_word === "test"))
{
//set session
$_SESSION['user_name'] = $user_name;
$_SESSION['user_logged_in'] = true;
return 1;

}
else{
return 0;
}
}

GeshanManandhar.com 7
Login check function
► Function call and process
$user_ok = check_login($user_name, $pass_word); //called from
check_login_function.php

if($user_ok){
header("Location: logged_in.php"); /* Redirect browser */

}
else {
header("Location: not_logged_in.php");

}
?>

GeshanManandhar.com 8
Either you login or are not logged in

GeshanManandhar.com 9
Session to authenticate users
► Address security issues.
► Put content that should be seen by logged
in users or even by user type.
► User module and session management is
essential to almost every system.

GeshanManandhar.com 10
Questions

GeshanManandhar.com 11
Let’s start practice
► Build your own login system with upto 3
user name and password combination with
user of array, refer to code of Day04.
► Among the 3 one should be of type admin
and remaining two of type user.
► The logged in page should not be accessible
directly after typing full URL/address in the
address bar.
GeshanManandhar.com 12

Potrebbero piacerti anche