PHP Day-3
PHP Day-3
<?PHP?>
[email protected]
Course Structure
2 Parts
1. Introduction to PHP (3 Days)
Introduction to PHP
Day-1 : PHP installations, PHP Language Basics, Basic Control Structures
PHP
What we will see today
Cookies
Sessions
PHP
Cookies
Cookies are text files stored on the client computer and they are kept of use
tracking purpose. PHP transparently supports HTTP cookies.
There are three steps involved in identifying returning users −
Server script sends a set of cookies to the browser. For example name, age, or
identification number etc.
Browser stores this information on local machine for future use.
When next time browser sends any request to web server then it sends those cookies
information to the server and server uses that information to identify the user.
PHP
Sessions
An alternative way to make data accessible across the various pages
of an entire website is to use a PHP Session.
A session creates a file in a temporary directory on the server where
registered session variables and their values are stored. This data will
be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the
php.ini file called session.save_path. Before using any session variable
make sure you have setup this path.
Sessions
A PHP session is easily started by making a call to the session_start()
function.
This function first checks if a session is already started and if none is
started then it starts one.
It is recommended to put the call to session_start() at the beginning of
the page.
Session variables are stored in associative array called $_SESSION[].
These variables can be accessed during lifetime of a session.
Starting a session
A PHP session is easily started by making a call to the session_start()
function.This function first checks if a session is already started and if
none is started then it starts one. It is recommended to put the call to
session_start() at the beginning of the page.
Session variables are stored in associative array called $_SESSION[].
These variables can be accessed during lifetime of a session.
The following example starts a session then register a variable called
counter that is incremented each time the page is visited during the
session.
Make use of isset() function to check if session variable is already set or
not
Destroying a session
A PHP session can be destroyed by session_destroy() function. This function
does not need any argument and a single call can destroy all the session
variables. If you want to destroy a single session variable then you can use
unset() function to unset a session variable.
Here is the example to unset a single variable −
<?php
unset($_SESSION['counter']);
?>
Here is the call which will destroy all the session variables −
<?php
session_destroy();
?>
That’s all for the Day-3
Thanks