Download presentation
Presentation is loading. Please wait.
1
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu xqzhu@cse.fau.edu
2
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
3
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+”);
4
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
5
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
6
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
7
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
8
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
9
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
10
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 Cookie:hill@google.com 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
11
PHP1-11 Cookie Create a cookie with setcookie setcookie(cookie_name, cookie_value, lifetime) setcookie("voted", "true", time() + 86400); 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
12
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
13
PHP1-13 Simple Shopping Cart At checkout page, summarize all the cookie values Checkout.phpcheckout1.php Checkoutmain.php
14
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
15
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
16
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
17
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
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.