1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.

Slides:



Advertisements
Similar presentations
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Advertisements

Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
PHP and the Web: Session : 4. Predefined variables PHP provides a large number of predefined global variables to any script which it runs also called.
Website Development Registering Users – Introducing Cookies.
Chapter 10 Managing State Information Using Sessions.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
Chapter 10 Managing State Information PHP Programming with MySQL.
Using Session Control in PHP tMyn1 Using Session Control in PHP HTTP is a stateless protocol, which means that the protocol has no built-in way of maintaining.
Chapter 10 Maintaining State Information Using Cookies.
Objectives Learn about state information
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Modified from Moseley ’s sli desWeb Applications Development. Lecture 6 Slide 1 Lecture 6: More PHP Instructor: Dr. Mohammad Anwar Hossain.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
JavaScript, Fourth Edition
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
Creating Databases for Web Applications cookie examples lab time: favorites cookies & Sessions class time for group work/questions on projects Next class:
Chapter 8 Cookies And Security JavaScript, Third Edition.
12/3/2012ISC329 Isabelle Bichindaritz1 PHP and MySQL Advanced Features.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
1 Maryland ColdFusion User Group Session Management December 2001 Michael Schuler
Nic Shulver, Introduction to Sessions in PHP Sessions What is a session? Example Software Software Organisation The login HTML.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Cookies & Session Web Technology
CSC 2720 Building Web Applications Server-side Scripting with PHP.
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
Advance web Programming Managing State Information (Cookies-Session) Date: 22 April 2014 Advance web Programming Managing State Information (Cookies-Session)
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
PHP Session ISYS 475. Session The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
How to maintain state in a stateless web Shirley Cohen
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Programming for the Web Cookies & Sessions Dónal Mulligan BSc MA
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
The need for persistence Consider these examples  Counting the number of “hits” on a website  i.e. how many times does a client load your web page source.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Web Programming Language
<?php require("header.htm"); ?>
Web Programming Language
Cookies and Sessions.
Web Programming Language
PHP-II.
Presentation transcript:

1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with HTML and PHP. How to persist client’s states over requests. How to persist client’s states over requests. How to authenticate a client by using cookies or PHP sessions. How to authenticate a client by using cookies or PHP sessions. Complete Assignment 6, part 1. Complete Assignment 6, part 1. Be ready to implement your Course Project. Be ready to implement your Course Project. PHP Support for HTML and HTTP

2 PHP HTTP Parameters submitted from an HTML form can be easily retrieved. Parameters submitted from an HTML form can be easily retrieved. Different HTTP requests from the same client can be interrelated with a cookie. Different HTTP requests from the same client can be interrelated with a cookie. Session variables allow data to persist during a session, which is a succession of requests from the same client. Session variables allow data to persist during a session, which is a succession of requests from the same client. PHP Support for HTML and HTTP

3 PHP HTTP HTML Form

4 PHP HTTP HTML Form HTML Form <html> Sample HTML Form Sample HTML Form <FORM ACTION="submit_form.php" METHOD="POST" <FORM ACTION="submit_form.php" METHOD="POST" NAME = "TestForm"> NAME = "TestForm"> First Name: First Name: <INPUT TYPE="TEXT" NAME = "first_name" SIZE = "30" <INPUT TYPE="TEXT" NAME = "first_name" SIZE = "30" MAXLENGTH = "30"> MAXLENGTH = "30"> Last Name: Last Name: <INPUT TYPE="TEXT" NAME = "last_name" SIZE = "30" <INPUT TYPE="TEXT" NAME = "last_name" SIZE = "30" MAXLENGTH = "30"> MAXLENGTH = "30"> </html>

5 PHP HTTP Form Attributes ACTION = URL_of_script ACTION = URL_of_script The script specified is activated when the form is submitted. METHOD = method_for_passing_data METHOD = method_for_passing_data "GET" "GET" "POST" "POST"

6 PHP HTTP Passing HTML Parameters with Method GET The form parameters are passed as part of the URL. The form parameters are passed as part of the URL /submit_form.php?first_name=Tuan&last _name=Pham 275/submit_form.php?first_name=Tuan&last _name=Pham 275/submit_form.php?first_name=Tuan&last _name=Pham 275/submit_form.php?first_name=Tuan&last _name=Pham The length of the URL is limited (imposed by web browser and server software). The length of the URL is limited (imposed by web browser and server software). The user can see and modify the parameters. The user can see and modify the parameters.

7 PHP HTTP Passing HTML Parameters with Method POST The form parameters are passed as the HTTP Request body. The form parameters are passed as the HTTP Request body. URL: cs275/submit_form.php URL: cs275/submit_form.php cs275/submit_form.php cs275/submit_form.php HTTP Request Body: first_name=tuan&last_name=pham& HTTP Request Body: first_name=tuan&last_name=pham& Submit=search Submit=search The size of the HTTP body can be large. The size of the HTTP body can be large. The user cannot see the parameters. The user cannot see the parameters.

8 PHP HTTP Attributes of an INPUT Element ( no closing tag ) ( no closing tag ) TYPE = TYPE = "TEXT", "PASSWORD" "RADIO", "CHECKBOX" "SUBMIT", "RESET" "IMAGE", "HIDDEN" NAME = form_parameter_name NAME = form_parameter_name VALUE = initial_value VALUE = initial_value SIZE, MAXLENGTH, CHECKED SIZE, MAXLENGTH, CHECKED

9 PHP HTTP Retrieving Form Parameters $_GET or $_POST arrays can be used: $_GET or $_POST arrays can be used: <?php... $first_name = $_GET[‘first_name']; $last_name = $_GET[‘last_name'];...?> <?php... $first_name = $_POST[‘first_name']; $last_name = $_POST[‘last_name'];...?>

10 PHP HTTP Retrieving HTTP and HTML Parameters Six superglobal arrays in PHP Six superglobal arrays in PHP $_GET, $_POST - GET, POST parameters $_GET, $_POST - GET, POST parameters $_COOKIE - cookie value $_COOKIE - cookie value $_FILES - information about uploaded files $_FILES - information about uploaded files $_SERVER - information about the web server $_SERVER - information about the web server $_ENV - environment variables $_ENV - environment variables

11 PHP HTTP ACTION FILE // submit_form.php <HTML> FORM SUBMISSION FORM SUBMISSION Result of Form Submission Result of Form Submission <?php $first_name = $_POST['first_name']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $last_name = $_POST['last_name']; $remote_addr = $_SERVER['REMOTE_ADDR']; $remote_addr = $_SERVER['REMOTE_ADDR']; echo "First Name = $first_name "; echo "First Name = $first_name "; echo "Last Name = $last_name "; echo "Last Name = $last_name "; echo "Client IP Address = $remote_addr "; echo "Client IP Address = $remote_addr "; echo "BYE "; echo "BYE ";?> </HTML>

12 PHP HTTP ACTION FILE

13 PHP HTTP Purpose of a Cookie HTTP protocol is stateless. That is, each request is independent. HTTP protocol is stateless. That is, each request is independent. One way to interrelate HTTP requests from the same client is to use a cookie. One way to interrelate HTTP requests from the same client is to use a cookie. A cookie is a piece of information sent to a browser by a Web Server. The browser then returns that information to the Web server in the following requests. A cookie is a piece of information sent to a browser by a Web Server. The browser then returns that information to the Web server in the following requests.

14 PHP HTTP Purpose of a Cookie Cookie is one way for the web sites to “remember” the users Cookie is one way for the web sites to “remember” the users For example: A client ID, generated by the server, can be attached to the reply to the first request -> stored in a cookie A client ID, generated by the server, can be attached to the reply to the first request -> stored in a cookie The client can submit this client ID to the subsequent requests. The client can submit this client ID to the subsequent requests. The client ID may be saved in the database. The client ID may be saved in the database.

15 PHP HTTP #1234 web browser web server first request first reply subsequent request1 #1234 Passing a Cookie #1234 subsequent request2 #1234

16 PHP HTTP Some Details on a Cookie A cookie contains information about a visit to a website: A cookie contains information about a visit to a website: Cookie name and value, Cookie name and value, Expiration time in seconds, Expiration time in seconds, Server domain name and application path, and Server domain name and application path, and Whether secure HTTP (HTTPS) should be used or not. Whether secure HTTP (HTTPS) should be used or not. Cookies are stored on client machines. Cookies are stored on client machines. A cookie is normally sent automatically to the server when the client revisits the application. A cookie is normally sent automatically to the server when the client revisits the application.

17 PHP HTTP Setting cookies with PHP <?php setcookie(“client_id", “12345", time()+ 60 * 60 * 24 * 100); time()+ 60 * 60 * 24 * 100);?> setcookie() should be executed before any HTML content is sent to the browser as it is stored in the HTTP response header. setcookie() should be executed before any HTML content is sent to the browser as it is stored in the HTTP response header. In the above example, the cookie will expire after 100 days. In the above example, the cookie will expire after 100 days.

18 PHP HTTP Identifying a Client with a Cookie A client ID is generated for each new client and sent to the client with a cookie. A client ID is generated for each new client and sent to the client with a cookie. A cookie is stored on the client machine. A cookie is stored on the client machine. An existing client submits the client ID with a cookie. An existing client submits the client ID with a cookie.

19 PHP HTTP Getting a Client ID from a Cookie <?php if ($client_id = $_COOKIE[‘client_id’]) { $client_name = $client_name = get_client_name($client_id); get_client_name($client_id); print “Welcome back, $client_name"; print “Welcome back, $client_name";} else { print “Welcome, New Client"; print “Welcome, New Client";}?>

20 PHP HTTP Setting Data in a Cookie <?php $client_id = generate_client_id(); setcookie(‘client_id', $client_id, time() ); setcookie(‘client_id', $client_id, time() );?>

21 PHP HTTP Removing a cookie <?php setcookie("cookiename", "", time()-60); ?><HTML>...</HTML>

22 PHP HTTP Sessions PHP has built-in support for sessions. PHP has built-in support for sessions. A session supports persistent variables accessible by different scripts and across multiple visits to the site. A session supports persistent variables accessible by different scripts and across multiple visits to the site. Sessions are a combination of a server-side cookie and a client-side cookie, where the client-side cookie is simply a reference id to the information stored in the server-side cookie. Sessions are a combination of a server-side cookie and a client-side cookie, where the client-side cookie is simply a reference id to the information stored in the server-side cookie. A session is also closed when a browser which started the session is closed. A session is also closed when a browser which started the session is closed.

23 PHP HTTP Starting and Continuing a Session Start/resume a session Start/resume a sessionsession_start(); Session variables are in array $_SESSION. Session variables are in array $_SESSION. <?phpsession_start();.... $_SESSION[‘user_id'] = get_user_id($user_name, $password); get_user_id($user_name, $password);?><HTML>......</HTML>

24 PHP HTTP Login page using session login.php page_1 page_N session session Register session global variables for each session $ _SESSION['USER_ID'] $_SESSION['USER_NAME'] $_SESSION['USER_ROLE'] calling session_start() call session_destroy() or close the browser Check session variables

25 PHP HTTP <?session_start(); $user_id = get_user_id($_POST['login_name], $user_id = get_user_id($_POST['login_name], $_POST['password']); $_POST['password']); if ($user_id) { if ($user_id) { // the user is registered. // the user is registered. $_SESSION[‘USER_ID'] = $user_id; $_SESSION[‘USER_ID'] = $user_id; $_SESSION['USER_NAME'] = get_name($user_id); $_SESSION['USER_NAME'] = get_name($user_id); $_SESSION['USER_ROLE'] = get_role($user_id); $_SESSION['USER_ROLE'] = get_role($user_id); echo “Welcome $_SESSION['USER_NAME']”; echo “Welcome $_SESSION['USER_NAME']”; } else { } else { // If the user is not registered, // If the user is not registered, // direct her/him to the registration page. // direct her/him to the registration page. header(“Location: register_new_user.php”); header(“Location: register_new_user.php”); }?> Login Page Using a Session Variable

26 PHP HTTP <?session_start(); if ($_SESSION[‘USER_ID']) { if ($_SESSION[‘USER_ID']) { // the user is logged in. // the user is logged in } else { } else { // If the user is not logged in, // If the user is not logged in, // direct her/him to the login page. // direct her/him to the login page. header(“Location: login_user.php”); header(“Location: login_user.php”); }?> Checking Logged-In User

27 PHP HTTP Additional session functions unset($_SESSION[‘var_name’]) unset($_SESSION[‘var_name’]) Unset a session variable. isset($_SESSION[‘var_name’]) isset($_SESSION[‘var_name’]) Find out whether a session variable is set. session_id() session_id() Get and /or set current session id session_destroy() session_destroy() Destroy all data registered to a session