Download presentation
Presentation is loading. Please wait.
1
<?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"); ?>
2
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)
3
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.
4
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> …
5
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
6
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)
7
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>
8
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.
9
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.
10
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
11
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
12
How to Create a Session The session_start() function is used to create cookies. <?php session_start(); ?>
13
Example Code <?php $_SESSION['views']=1;
if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?>
14
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']++; ?>
15
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
16
PHP File Handling
17
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()
18
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:
19
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);
20
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);
21
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);
22
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);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.