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)

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

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.
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.
SWU, Computer systems and technologies. The Objective of This Lecture To give you a very high-level overview of some of the tools for Web Programming.
Chapter 10 Managing State Information PHP Programming with MySQL.
Intermediate PHP & 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.
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.
Objectives Learn about state information
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
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.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
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.
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 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Dynamic Programming with PHP (mktime), Cookies, SQL, Authentication.
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.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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.
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
8 th Semester, Batch 2008 Department of Computer Science SSUET.
 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.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
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.
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
Week 7 Server side programming PHP Scripting Language MySQL Database Apache Server IT4103 Web Programming
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.
CSE 154 Lecture 20: Cookies.
CHAPTER 5 SERVER SIDE SCRIPTING
CGS 3066: Web Programming and Design Spring 2016
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.
ITM 352 Cookies.
Cookies and Sessions in PHP
Open Source Programming
ISC440: Web Programming 2 Server-side Scripting PHP 3
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
<?php require("header.htm"); ?>
Cookies Cookie :- A cookie is often used to identify a user. A cookie is often used to identify a user. A cookie is a small file that the server embeds.
Web Programming Language
SESSION TRACKING BY DINESH KUMAR.R.
Cookies and Sessions.
Advanced Concepts and AJAX
PHP-II.
Presentation transcript:

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) ? Returned_if_expression_is_true: returned_if_expression_is_false; 2

Example Example <?php $mood="sad"; $text=($mood=="happy")?"I’m in a good mood":"Not happy but $mood"; print "$text"; ?> 3

Date The PHP date() function is used to format a time or a date The PHP date() function formats a timestamp to a more readable date and time. 4

Example Untitled 1 <?php echo("Result with date(): "); //The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday) echo(date("l"). " "); //d - date in numbers //S - suffix for date //F - A full textual representation of a month (January through December) //Y - A four digit representation of a year //h - 12-hour format of an hour (01 to 12) //i - Minutes with leading zeros (00 to 59) //s - Seconds, with leading zeros (00 to 59) //A - Uppercase AM or PM echo(date("l dS \of F Y h:i:s A"). " "); ?> 5

Server Side Includes You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function The two functions are identical in every way, except how they handle errors The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error) 6

Include Files These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages This can save the developer a considerable amount of time This means that you can create a standard header or menu file that you want all your web pages to include 7

include() The include() function takes all the text in a specified file and copies it into the file that uses the include function. 8

<?php include("inc2.php"); ?> Welcome to my home page Some text 9

Include File – incnew.php Yahoo | Hotmail | ireland 10

The require() Function Identical to include(), they only handle errors differently The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error) 11

Sessions When you are working with an application, you open it, do some changes and then you close it The computer knows who you are. It knows when you start the application and when you end But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state

Sessions A PHP session allows you to store user information on the server for later use (i.e. username, shopping items, etc) Session information is temporary and will be deleted after the user has left the website If you need a permanent storage you may want to store the data in a database A PHP session variable is used to store information about, or change settings for a user session Session variables hold information about one single user, and are available to all pages in one application

Session Variables Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID The UID is either stored in a cookie or is propagated in the URL

Starting a session This code register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

Storing a session variable <?php session_start(); // store session data $_SESSION['newuser']=1; ?> <?php //retrieve session data echo "New user has viewed the page=". $_SESSION['newuser']; ?>

Session Example <?php session_start(); if(isset($_SESSION['pageview' ])) $_SESSION['pageview']=$_ SESSION['pageview']+1; else $_SESSION['pageview']=1; echo "Number of times page viewed by user=". $_SESSION['pageview']; ?>

Killing a Session If you wish to delete some session data use the unset() or the session_destroy() function The unset() function is used to free the specified session variable session_destroy() will reset your session and you will lose all your stored session data

End a Session <?php session_start(); if(isset($_SESSION['pageview'])) $_SESSION['pageview']=$_SE SSION['pageview']+1; else $_SESSION['pageview']=1; echo "Number of times page viewed by user=". $_SESSION['pageview']; unset($_SESSION['pageview']); ?> <?php session_destroy(); ?>

More Sessions A PHP session is started either explicitly by session_start(), or implicitly by registering a variable for the session, using session_register() Call session_start() on top of the page, so that session variables are available to your script, and register variables to the session later in the script

More Sessions if you registered your session variables with session_register() in the head of the script and left out the session_start() call session_register() calls session_start() internally, if the session isn't started yet When you start a session, the following happens:  PHP checks whether a valid session ID exists  If there is no session ID, PHP creates a new ID  If a valid ID exists, the frozen variables of that session are reactivated and introduced back to the global namespace

Sessions Note that this function takes the name of a variable as argument, not the variable itself You can use session_unregister() to remove variables from the session, for example, when the user removes a product item from the shopping cart

<?php session_start(); print(" "); $_SESSION["MyLogin"] = "Ciara"; print("A value saved in the session named as Login.\n"); $_SESSION["MyColor"] = "Blue"; print("A value saved in the session named as Colour.\n"); print("Click Next Page "." to retrieve the values.\n"); print(" \n"); ?> New Page 1

<?php session_start(); print(" "); $myLogin = $_SESSION["MyLogin"]; print("Value of MyLogin has been retrieved: ".$myLogin."\n"); $myColor = $_SESSION["MyColor"]; print("Value of MyColor has been retrieved: ".$myColor."\n"); print(" \n"); ?> New Page 1

PHP Cookies 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 a page with a browser, it will send the cookie too With PHP, you can both create and retrieve cookie values 25

How to create a cookie? The setcookie() function is used to set a cookie The setcookie() function must appear BEFORE the tag Syntax  setcookie(name, value, expire, path, domain); 26

setcookie(name, value, expire, path, domain); Name = $_COOKIE[“cookiename”] Value = value of cookie Expire= expiry time of cookie Path=the path on the server on which the cookie will be available e.g. test/exam/ Domain = the domain on which it will be available e.g. example.com Secure= whether or not it should use a secure HTTP connection 27

How to retrieve a Cookie value? The PHP $_COOKIE variable is used to retrieve a cookie value Use the isset() function to find out if a cookie has been 28

Cookie Example <?php setcookie("user","Nora Batty",time()+3600); setcookie("age", 6,time()+3600); setcookie("country","Ireland",time()+3600); ?> Set Cookie and Read Cookie on same page <?php echo $_COOKIE["user"]; echo " "; echo $_COOKIE["age"]; echo " "; echo $_COOKIE["country"]; ?> 29

How to delete a cookie? When deleting a cookie you should assure that the expiration date is in the past Example <?php // set the expiration date to one hour ago setcookie("user", "", time()-3600); ?> 30

If your browser does not support cookies? If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application One method is to pass the data through forms 31

Cookie Example <?php setcookie("veg","carrot", time()+3600); ?> New Cookie <?php if (isset($_COOKIE["veg"])) print $_COOKIE["veg"]; else print " Hello. This may be your first visit "; ?> 32

Cookies In order to see all of the cookies set: print_r($_COOKIE); This will show all cookies currently contained in the cookie array 33

More Cookies <?php setcookie("LoginName","Ciara"); setcookie("PreferredColor","Blue"); print("2 cookies were delivered.\n"); ?> New Page 1 Move to get cookie page

Cookie <?php if (isset($_COOKIE["LoginName"])) { $loginName = $_COOKIE["LoginName"]; print("Received a cookie named as LoginName: ".$loginName."\n "); } else { print("Did not received any cookie named as LoginName.\n "); } print("All cookies received:\n "); foreach ($_COOKIE as $name => $value) { print " $name = $value\n "; } ?>

<?php $Month = time(); //this adds 30 days to the current time setcookie("AboutVisit", date("F jS - g:i a"), $Month); ?> Cookie Tracking <?php if(isset($_COOKIE["AboutVisit"])) { $last = $_COOKIE["AboutVisit"]; echo "Welcome back! You last visited on ". $last; } else { echo "Welcome to our site!"; } ?>