Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3066: Web Programming and Design Spring 2016

Similar presentations


Presentation on theme: "CGS 3066: Web Programming and Design Spring 2016"— Presentation transcript:

1 CGS 3066: Web Programming and Design Spring 2016
Session and Cookies Partially adapted from “Introduction to Server-Side Programming”Charles Liu

2 Cookies and Sessions Sometimes we need to keep track of information about the web user/client between consecutive HTTP requests Example: Shopping Cart “Remember Me”

3 Cookies Cookies are files stored on the client side
Contains relevant data in name-value pairs Comes with expiration dates. Expired cookie contents are no longer accessed by the browser Can be managed by server-side scripts(PHP) Relevant cookies are automatically submitted from client to server with HTTP request PHP stores information in $_COOKIE superglobal

4 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

5 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)

6 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

7 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

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

9 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

10 PHP File Handling

11 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()

12 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:

13 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);

14 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);

15 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);

16 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);


Download ppt "CGS 3066: Web Programming and Design Spring 2016"

Similar presentations


Ads by Google