PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu

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.
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.
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.
Session Management A290/A590, Fall /25/2014.
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.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
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.
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.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
JavaScript, Fourth Edition
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
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.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
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.
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.
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.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
ASP.Net, Web Forms and Web Controls 1 Outline Session Tracking Cookies Session Tracking with HttpSessionState.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
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.
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
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.
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:
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.
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.
COOKIES AND SESSIONS.
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.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
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.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
1 CHAPTER 10 ADVANCED PHP.
Web Programming Language
Cookies and Sessions in PHP
Cookies BIS1523 – Lecture 23.
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"); ?>
HTML5 and Local Storage.
CSE 154 Lecture 21: Sessions.
CSE 154 Lecture 22: Sessions.
CSc 337 Lecture 27: Cookies.
Web Programming Language
PHP-II.
CSc 337 Lecture 25: Cookies.
Presentation transcript:

PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu

PHP1-2 PHP  PHP overview  PHP General Syntactic Characteristics  PHP Output to browser  Primitives, Operations, and Expressions  Control Statement  Array  Function  File access  Cookie  Session  Form process

PHP1-3 PHP: Files  Deal with any file on the server $fptr = fopen(filename, use_indicator)  Use indicators:  “r”read only, from the beginning  “r+”read and write, from the beginning  “w”write only, from the beginning (also creates the file, if necessary)  “w+”read and write, from the beginning (also creates the file, if necessary)  “a”write only, at the end, if it exists (creates the file, if necessary)  “a+”read and write, read at the beginning, write at the end  “b”binary file: on systems which differentiate between binary and text files $fptr1=fopen(“test1.php”, “w+”);files.phpfiles.php $fptr2=fopen(“test1.php”, “r+”);

PHP1-4 PHP: Files  fopen could fail  it will return “false”  Use file_exists(filename) to determine whether file exists before trying to open it  Use fopen with “die”  Always use fclose(file_var) to close a file, after your reading/writing Fopen.php

PHP1-5 PHP: Write to Files  $bytes_written = fwrite(file_var, string)  fwrite returns the number of bytes it wrote  Return “false” on error fwrite.php

PHP1-6 PHP: Reading Files  Read all or part of the file into a string variable  $str = fread(file_var, #bytes)  To read the whole file, use filesize(file_name) as the second parameter  Read one line from the file  $line = fgets(file_var[, #bytes])  Reads characters until eoln, eof, or #bytes characters have been read  Read one character from the file  $ch = fgetc(file_var) Control reading lines or characters with eof detection using feof (TRUE for eof; FALSE otherwise) while(!feof($file_var)) { $ch = fgetc($file_var); } Readingfile.php

PHP1-7 PHP file access  Read the lines of the file into an array  array file ( string filename) file() returns the file in an array Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE File.phpFiltering.php

PHP1-8 PHP  PHP overview  PHP General Syntactic Characteristics  PHP Output to browser  Primitives, Operations, and Expressions  Control Statement  Array  Function  File access  Cookie  Session  Form process

PHP1-9 Building a Shopping Cart  What is a shopping cart  Putting things together and then check out.  A series of scripts that keep track of items a visitor picks to buy from your site until they proceed to the "checkout  Web server is basically stateless  HTTP is stateless  It does not store any information about a web client Why?  Web server records nothing about the previous actions of a web client It only knows the current web page the web browser is visiting  How are we going to keep our browser “continuously” connected to the server  So we can feel the we are connected to the server all the time  How are we going to collect information across different web pages  Cookie & Session  Hidden fields of a form

PHP1-10 Cookie  What is a cookie?  An message given to a web browser by a web server, then send back to the server each time the browser requests a page from the server.  What can cookie do for us?  Identify users and store client side information  Authenticate or identify a registered user No need to sign in again every time they access a website  Where is the cookie located? How does it look like?  C:\Documents and Settings\YourLoginID\Local Settings\Temporary Internet Files   Will browser return all cookies to each server?  No, only those matched domains  Malicious cookies setting  Are cookies dangerous to my computer?  No  Is this a major privacy issue?  You could lose identify information  You have choices of not using any cookie

PHP1-11 Cookie  Create a cookie with setcookie  setcookie(cookie_name, cookie_value, lifetime) setcookie("voted", "true", time() ); setcookie("voted", "true", time() + 60*60*24);  Is “lifetime” important? Without this value, cookie expires right after you close the current web browser window. Expired cookies will be deleted without sending back to the server  Cookies must be created before any other HTML is created by the script  Before …were sent  How to access the cookie value  Using the $_COOKIE array setcookie("visits“, $visitcount, time()+3600); $visitcount = $_COOKIE["visits"];  Check whether a cookie has been set before Isset($_COOKIE[“visits”]) Cookie_simple.phpcookie1.php

PHP1-12 Simple Shopping Cart  How to update cookie value based on your choice?  $_SERVER["PHP_SELF"] The name of the current script  Using implicit arrays for form values $_POST $_GET Simpleupdate.php Updatecookie.phpuppdatecookie1.php

PHP1-13 Simple Shopping Cart  At checkout page, summarize all the cookie values Checkout.phpcheckout1.php Checkoutmain.php

PHP1-14 Session Tracking  What is a session  SUN API: Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user  web applications make use of sessions to remember who you are  A session is available as long as the browser is opened  A session is closed as soon as the browser is closed or you go to another website  Why session?  Some users block cookies for privacy concerns  Are cookie & session same thing?  Cookie cookies are normally stored in your hard disk privacy  Session A session aren't stored in your hard disk Considered more secure

PHP1-15 Session tracking  For session tracking, PHP creates and maintains a session tracking id  Create the id with a call to session_start with no parameters  Subsequent calls to session_start retrieves any session variables that were previously registered in the session  To create a session variable  $_SESSION['myVar'] = $myVarValue;  Check session if(isset($_SESSION['myVar'])) { print $_SESSION['myVar']; $_SESSION['myVar']++; }  Example: count number of pages visited Session.phpmain_1.phpmain_2.php

PHP1-16 Form Process  Action property  Where to send form data  Method property  Post vs get  Each element has unique name  Using implicit arrays for form element access $_POST $_GET Simpleform.htmlsimpleform.php

PHP1-17 A complete Shopping Cart  Client Side:  Set cookie for each page  Collect information across web pages  A summarization form (before user submit the order)  “Submission” -> invoke form processing program  Server Side:  A form processing program  Read form values from $_POST or $_GET array  Process and save values  Confirmation formselection.phpformselection.php formselection1.php formprocess.phpformselection1.php formprocess.php

PHP1-18 PHP  PHP overview  PHP General Syntactic Characteristics  PHP Output to browser  Primitives, Operations, and Expressions  Control Statement  Array  Function  File access  Cookie  Session  Form process