<?php require("header.htm"); ?>

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
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.
©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.
Php cookies & sessions.
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.
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.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
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.
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
Website Development with PHP and MySQL Saving Data.
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
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
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.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
PHP Cookies. Cookies are small files that are stored in the visitor's browser. Cookies can be used to identify return visitors, keep a user logged into.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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)
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.
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.
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
CSE 154 Lecture 20: Cookies.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Cookies What is a Cookie?
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.
1 CHAPTER 10 ADVANCED PHP.
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Web Programming Language
Cookies and Sessions in PHP
Open Source Programming
Implementing Cookies in PHP
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.
CSE 154 Lecture 21: Sessions.
Maintaining State in PHP Part II - Sessions
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
CSE 154 Lecture 22: Sessions.
SESSION TRACKING BY DINESH KUMAR.R.
Cookies and Sessions.
Web Programming Language
Advanced Concepts and AJAX
Lecture 6: Processing Forms with PHP
PHP-II.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

<?php require("header.htm"); ?> Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function. The require() function is used to create functions, headers, footers, or elements that will be reused on multiple pages. <?php require("header.htm"); ?>

How to create variables storing values across php scripts’ calls? . Client-server connection is not permanent => Cannot be saved in program memory There are many clients connecting simultaneously => Cannot be saved in file (you cannot identify clients as well sometimes)

What is a Cookie? A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

setcookie(name, [value], [expire], [path], [domain], [secure]); How to Create a Cookie The setcookie() function is used to create cookies. Note: The setcookie() function must appear BEFORE the <html> tag. setcookie(name, [value], [expire], [path], [domain], [secure]); This sets a cookie named "uname" - that expires after ten hours. <?php setcookie("uname", $name, time()+36000); ?> <html> <body> …

Setcookie() To set a cookie in PHP: setcookie(name, value) Or, setcookie(name, value, time_of_expiry) Time of expiry entered in seconds. Present time + time in seconds until expiration Present time can be looked up using PHP time() function

Setcookie() Example: setcookie("TestCookie", ”testvalue”); setcookie("TestCookie", ”testvalue”,  time()+3600);  //set to expire after 1 hour from present time Once set, S_COOKIE[‘TestCookie’] will have value ‘testvalue’ Always check with isset($_COOKIE[$cookie_name]) before trying to use the cookie’s value To delete a cookie, set a new cookie with same arguments but expiration in the past (e.g. 1)

How to Retrieve a Cookie Value To access a cookie you just refer to the cookie name as a variable or use $_COOKIE array Tip: Use the isset() function to find out if a cookie has been set. <html> <body> <?php if (isset($uname)) echo "Welcome " . $uname . "!<br />"; else echo "You are not logged in!<br />"; ?> </body> </html>

How to Delete a Cookie It will expire or Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string (""), and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

What is a Session? The session support allows you to register arbitrary numbers of variables to be preserved across requests. A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

Sessions data stored on the server, managed by Server-side script(PHP) In PHP, session variables store information about user session in $_SESSION superglobal array. Session variables hold information about one single user, and are available to all pages in one application. Session variables expire when the browser is closed

PHP Session management session_start() Before you can store user information in your PHP session, you must first start up the session. The session_start() function must appear at the top of EVERY page, BEFORE the <html> tag

How to Create a Session The session_start() function is used to create cookies. <?php session_start(); ?>

Example Code <?php $_SESSION['views']=1; if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?>

How to Retrieve a Session Value Register Session variable session_register('var1','var2',...); // will also create a session PS: Session variable will be created on using even if you will not register it! Use it <?php session_start(); if (!isset($_SESSION['count'])) $_SESSION['count'] = 0; else $_SESSION['count']++; ?>

Session Management functions unset(): function used to free the specified session variable Example: unset($_SESSION[‘shopping_cart']); session_destroy(); Resets current session. You will lose all your stored session data. Call when a user signs out

PHP File Handling

Open/Close file in PHP A file on the server-side file system is opened with fopen() Fopen() returns a ‘handle’ to the file that can be used to reference the opened file Each file is opened in a particular mode. A file is closed with fclose()

fopen() Used to create/open a file on the server Parameters: filename with optional pathname, one of the following modes: Example: $myfile = fopen("testfile.txt", "w") 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. For more details and complete list of modes: http://php.net/manual/en/function.fopen.php

Read a File using fread() Syntax: fread(open_file_handle, length_in_bytes); Reads content of a specific length from the file $my_file = 'file.txt'; $handle = fopen($my_file, 'r') or die('Cannot open file’); //will read upto 4096 characters or until the end-of-file //whichever comes first $data = fread($handle,4096);

Read a File using fgets() Syntax: fgets(open_file_handle) Reads upto the next newline $my_file = 'file.txt'; $handle = fopen($my_file, 'r') or die('Cannot open file’); //reads one line from the file and echoes to HTTP Response echo fgets($my_file);

Write to a File using fwrite() $my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file’); $data = ‘data to be written to file'; fwrite($handle, $data);

Reset file pointer using fseek() Syntax: fseek($handle, $offset) Updates file pointer position to $offset bytes from the file beginning Used to “jump to” a specific part of the file To rewind back to the beginning, call fseek($handle,0);