LIS651 lecture 3 functions & sessions Thomas Krichel 2008-11-08.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

LIS651 lecture 2 mySQL and PHP mySQL functions Thomas Krichel
LIS651 lecture 3 taming PHP Thomas Krichel
LIS651 lecture 0 forms Thomas Krichel
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
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.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
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.
Website Development Registering Users – Introducing Cookies.
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.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Chapter 10 Maintaining State Information Using Cookies.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
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.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Validated.php
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
JavaScript, Fourth Edition
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
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.
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
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
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
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
Dynamic Programming with PHP (mktime), Cookies, SQL, Authentication.
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.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
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)
(Some from Chapter 11.9 – “Web” 4 th edition and
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.
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.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
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.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
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.
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
Cookies Tutorial Cavisson Systems Inc..
LIS651 lecture 3 functions & sessions
CSE 154 Lecture 20: Cookies.
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.
Sessions and cookies (part 2)
ITM 352 Cookies.
Cookies and Sessions in PHP
<?php require("header.htm"); ?>
CSE 154 Lecture 21: Sessions.
Web Programming Language
CSE 154 Lecture 22: Sessions.
Presentation transcript:

LIS651 lecture 3 functions & sessions Thomas Krichel

functions The PHP function reference is available on its web site It shows the impressive array of functions within PHP. But one of the strengths of PHP is that you can create your own functions as you please. If you recreate one of the built-in functions, your own function will have no effect.

simplest function function beer_print { print "beer\n"; } beer_print() ; // prints: beer and newline

A more practical example Stephanie Rubino was an English teacher and objects to sentences like You have ordered 1 bottles of Grosswald Pils. Let us define a function rubino_print(). It will take three arguments –a number to check for plural or singular –a word for the singular –a word for the plural

a function and its arguments declare the arguments to the function in parenthesis function rubino_print ($number, $singular,$plural) { if($number == 1) { print "one $singular"; } else { print "$number $plural"; } rubino_print(3,'woman','women'); // prints: 3 women

default arguments Sometimes you want to allow a function to be called without giving all its arguments. You can do this by declaring a default value, using an equal sign in the function list function thomas_need($thing='beer') { print "Thomas needs $thing.\n"; } thomas_need(); // prints: Thomas needs beer. thomas_need('vodka'); // prints: Thomas needs vodka.

rubino_print using common plurals function rubino_print ($num, $sing,$plur=1) { if($num == 1) { print "one $sing"; } elseif($plur ==1) { print "$num $sing"."s"; } else { print "$num $plur"; } rubino_print(6,'beer'); // prints: 6 beers

return value Up until now we have just looked at the side effect of a function. "return" is a special command that returns a value. It takes the return value as a parameter return $result; When return is used, the function is left. Example function good_beer () { return 'Festbock'; } $beer=good_beer(); print $beer ; // prints: Festbock.

rubino_print with return function rubino_print ($number, $singular,$plural) { if($number == 1) { return "one $singular"; } return "$number $plural"; } $order=rubino_print(2,"beer","beers"); print "you ordered $order\n"; // prints: you ordered 2 beers.

visibility of variables Variables used inside a function are not visible from the outside. Example $beer="Karlsberg"; function yankeefy ($name='Sam Adams') { $beer=$name; } yankeefy(); print $beer;// prints: Karlsberg The variable inside the function is something different than the variables outside.

accessing global variables. There are two ways to change a global variable, i.e. one that is defined in the main script. One is just to call it as $GLOBAL['name'] where name is the name of the global variable. function yankeefy ($name="Sam Adams") { $GLOBALS['beer']=$name; } The other is to change it outside a function definition. Example in brewer_quiz.php

working with many source files Many times it is useful to split a PHP script into several files. PHP has two mechanisms. require(file) requires the file file to be included. If the file is not there, PHP exits with an error. include(file) includes the file. If the file is not there no error is reported.

require() and include() Both assume that you leave PHP. Thus within your included file you can write simple HTML. If you want to include PHP in your included file, you have to surround it by, just like in a PHP script. Here is an example to use include to build the basic web page. Included files are customarily give the extension.inc.

top.inc <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " $title

bottom.inc <img style="border: 0pt" src=" alt="Valid XHTML 1.0!" height="31" width="88" />

trouble $title in the top.html is not understood as the title. It reads as $title, which means "idiot" for your web user. Even if you replace $title with $title is empty. The definition from the outer file is not seen in the included file. So you have to split into three files, and print the title in the main file. I leave that to you to figure out.

login.php & create_account.php Both require a database that has three fields –id which is an auto_increment int acting as a handle –username is the username of the account. it must be unique and this is enforced by mySQL –password is a varchar(41) because the sha1 of the password is stored. This is 40 chars long.

sessions You will recall that HTTP is a stateless protocol. Each request/response is self-contained. Statefulness is crucial in Web applications. Otherwise users have to authenticate every time they access a new page. Traditionally, one way to create statefulness is to use cookies. PHP uses cookies to create a concept of its own, sessions, that makes it all very easy.

cookies A cookie is a piece of attribute/value data. A server can send cookies as value of a HTTP header Set-Cookie:. Multiple headers may be sent. When the client visits the web site again, it will send the cookie back to the server with a HTTP header Cookie:

Set-Cookie Set-Cookie: name=value; [expires= date;] [path=path;] [domain=domain] [secure] where [ ] indicates optional components and –name is the variable name set in the cookie –value is the variable's value –date is a date when the cookie expires –path restricts the cookie to be sent only when requests to a path starting with path are made –domain restricts the sending of the cookie to a certain domain –secure restricts transmission to https

Cookies: The browser compares the request it wants to make with the URL and the domain that sent the cookie. If the path is not set the cookie will only be sent to a request with the originating URL. If the cookie matches the request a request header of the form Cookie: name1=value1 ; name2=value2 is sent.

sessions Sessions are a feature of PHP. PHP remembers a session through a special cookie PHPSESSID. To activate the sessions, include session_start(); at the beginning of your script, before any printing has been done. One a session is active, you have a special super-global variable $_SESSION. Session data is stored in special files on wotan.

$_SESSION This is an array where you can read and set variables that you want to keep during the session. if($_SESSION['user_name]) { print "welcome ". $_SESSION['user_name]; } else { // show users login form print login_form(); }

ending sessions At 9 and 39 past each hour, wotan deletes all session files that have not been changed for 24 minutes or more. If you want to remove a session yourself, you can call session_destroy() in your script. An example is in visit.php.

Thank you for your attention! Please switch off machines b4 leaving!