PHP Lecture 2 Xingquan Hill Zhu xqzhucse fau

  • Slides: 18
Download presentation
PHP Lecture 2 Xingquan (Hill) Zhu xqzhu@cse. fau. edu PHP 1

PHP Lecture 2 Xingquan (Hill) Zhu xqzhu@cse. fau. edu PHP 1

PHP q PHP overview q PHP General Syntactic Characteristics q PHP Output to browser

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

PHP: Files q Deal with any file on the server $fptr = fopen(filename, use_indicator)

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

PHP: Files q fopen could fail v it will return “false” v Use file_exists(filename)

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

PHP: Write to Files q $bytes_written = fwrite(file_var, string) v fwrite returns the number

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

PHP: Reading Files q Read all or part of the file into a string

PHP: Reading Files q Read all or part of the file into a string variable v $str = fread(file_var, #bytes) v To read the whole file, use filesize(file_name) as the second parameter q Read one line from the file v $line = fgets(file_var[, #bytes]) v Reads characters until eoln, eof, or #bytes characters have been read q Read one character from the file v $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 PHP 6

PHP file access q Read the lines of the file into an array v

PHP file access q Read the lines of the file into an array v 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. php Filtering. php PHP 7

PHP q PHP overview q PHP General Syntactic Characteristics q PHP Output to browser

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

Building a Shopping Cart q What is a shopping cart v Putting things together

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

Cookie q What is a cookie? v An message given to a web browser

Cookie q What is a cookie? v 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. q What can cookie do for us? v Identify users and store client side information v Authenticate or identify a registered user • No need to sign in again every time they access a website q Where is the cookie located? How does it look like? v C: Documents and SettingsYour. Login. IDLocal SettingsTemporary Internet Files v Cookie: hill@google. com q Will browser return all cookies to each server? v No, only those matched domains v Malicious cookies setting q Are cookies dangerous to my computer? v No q Is this a major privacy issue? v You could lose identify information v You have choices of not using any cookie PHP 10

Cookie q Create a cookie with setcookie v setcookie(cookie_name, cookie_value, lifetime) • setcookie("voted", "true",

Cookie q Create a cookie with setcookie v setcookie(cookie_name, cookie_value, lifetime) • setcookie("voted", "true", time() + 86400); • setcookie("voted", "true", time() + 60*60*24); v 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 q Cookies must be created before any other HTML is created by the script v Before <html><body>…were sent q How to access the cookie value v Using the $_COOKIE array • setcookie("visits“, $visitcount, time()+3600); • $visitcount = $_COOKIE["visits"]; v Check whether a cookie has been set before • Isset($_COOKIE[“visits”]) Cookie_simple. php cookie 1. php PHP 11

Simple Shopping Cart q How to update cookie value based on your choice? v

Simple Shopping Cart q How to update cookie value based on your choice? v $_SERVER["PHP_SELF"] • The name of the current script v Using implicit arrays form values • $_POST • $_GET Simpleupdate. php Updatecookie. php uppdatecookie 1. php PHP 12

Simple Shopping Cart q At checkout page, summarize all the cookie values Checkout. php

Simple Shopping Cart q At checkout page, summarize all the cookie values Checkout. php checkout 1. php Checkoutmain. php PHP 13

Session Tracking q What is a session v SUN API: Provides a way to

Session Tracking q What is a session v 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 v web applications make use of sessions to remember who you are v A session is available as long as the browser is opened v A session is closed as soon as the browser is closed or you go to another website q Why session? v Some users block cookies for privacy concerns q Are cookie & session same thing? v Cookie • cookies are normally stored in your hard disk • privacy v Session • A session aren't stored in your hard disk • Considered more secure PHP 14

Session tracking q For session tracking, PHP creates and maintains a session tracking id

Session tracking q For session tracking, PHP creates and maintains a session tracking id v v 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 q To create a session variable v $_SESSION['my. Var'] = $my. Var. Value; v Check session if(isset($_SESSION['my. Var'])) { print $_SESSION['my. Var']; $_SESSION['my. Var']++; } q Example: count number of pages visited Session. php main_1. php main_2. php PHP 15

Form Process q Action property v Where to send form data q Method property

Form Process q Action property v Where to send form data q Method property v Post vs get q Each element has unique name v Using implicit arrays form element access • $_POST • $_GET Simpleform. html simpleform. php PHP 16

A complete Shopping Cart q Client Side: v Set cookie for each page v

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

PHP q PHP overview q PHP General Syntactic Characteristics q PHP Output to browser

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