SlideShare uma empresa Scribd logo
1 de 26
Session In Php
By: IQRA BALOCH.
Contents
 What is Session?
 Why, Where Session is Used?
 How Sessions Works?
 Session As File.
 Session Expiry
Copyright © 2012 Muhammad Baqar Qazi.
What Is Session?
 Generally session means limited period of time.
Or
 A meeting or period devoted to a particular activity.
 To get understood about sessions in php we need to go back
and review our concepts regarding http, html, client and
server.
 It was clearly observed that web is stateless.
 There is no continuous connection between client and server.
 For-example :
 The server does not know that the current page was
requested in past by the user or not.
 A browser request a page from server – connection opens
between both, server get the request and sends back the
response – connection closed.
Copyright © 2012 Iqra Baloch.
What is Session (Continued)
 Session:
 Is one that makes the web satefull. Simply
maintaining the state.
 Allows to track information of user.
 A session is basically a way of storing variables and
making them available across multiple pages on
your web site.
 Session is unlike other variables in the sense that we
are not passing them individually to each new page,
but instead retrieving them from the session we open
at beginning of each page.
Copyright © 2012 Iqra Baloch.
What is Session (Continued)
 In web applications, a session is the sequence of
interactions between the server and a user.
 A session refers to a period of activity when a PHP
script stores state information on a Web server.
 All session data is stored on the server (usually in
a database, to which the client does not have direct
access) linked to that identifier.
 A PHP session provides an easy, but effective, way
of passing variables between web pages in a PHP
application. The session starts when a PHP site is
accessed and ends when the user closes their web
browser.
Copyright © 2012 Iqra Baloch.
Why, Where Session is used (Need)?
 Previously we store the information in a hidden form
field. Even then, it persists only if the form is
submitted.
 To make data accessible across the various pages of
an entire website.
 Suppose a user is buying book online, there should
be a mechanism to keep track of books added by
user to basket.
 Very common example be the login system, where
there is need of state maintenance to check whether
user is logged in or not- is the user valid.
Copyright © 2012 Iqra Baloch.
How Session Work
 Session Is a file stored on Server.
 To find which file belongs to which user a cookie is
created.
 A special session cookie is set on their browser and
then we look that cookie to find the place where a file
resides on our server and then we can look in that
file to find information in session.
 The information is not there for them to see or to edit
they can only change id which reference to file.
 They can change id number is very long and
complicated to help to ensure we do not end up with
hijacking of other users session information.
Copyright © 2012 Iqra Baloch.
How Session Works (Continued)
 To use session we use to create a file and set cookie on
user machine. Then we need to find the cookie and the
corresponding file on the machine this be done with
session_start().
 The session_start() function starts a new session or
continues an existing one
 The session_start() function generates a unique session
ID to identify the session
 A session ID is a random alphanumeric string that looks
something like:
7f39d7dd020773f115d753c71290e11f
 The session_start() function creates a text file on the
Web server that is the same name as the session ID,
preceded by sess_
Copyright © 2012 Iqra Baloch.
How Session Works (Continued)
 <?php
 session_start();
 ?>
 This command should be called only once in each
page, and it must be called before the PHP script
generates any output, so the ideal position is
immediately after the opening PHP tag.
 If any output is generated before the call to
session_start(), the command fails and the session
won’t be activated for that page.
Copyright © 2012 Iqra Baloch.
How Session Works (Continued)
 When a session is initiated, the server stores
information in session variables that can be
accessed by other pages as long as the session
remains active (normally until the browser is closed).
 Because the identifier is unique to each visitor, the
information stored in session variables cannot be
seen by anyone else.
Copyright © 2012 Iqra Baloch.
Session As File.
 The cookie stored in the browser is called PHPSESSID,
unique identifier.
 A matching file, which contains the same unique identifier
as part of its filename, is created on the web server.
 PHP sessions store a unique identifier as a cookie in the
browser (left) and on the server(right).
Copyright © 2012 Iqra Baloch.
Session As File(Continued).
 There is no danger of private
information being exposed through
someone examining the contents of
a cookie on a shared computer.
 The session variables and their
values are stored on the web server.
 Figure Shows contents of a simple
session file.
 As you can see, it’s in plain text, and
the content isn’t difficult to decipher.
 The session shown in the figure has
two variables: name and location.
 The variable names are followed by
a vertical pipe, then the letter “s”, a
colon, a number, another colon, and
the variable’s value in quotes. The
“s” stands for string, and the number
indicates how many characters the
string contains.
Copyright © 2012 Iqra Baloch.
Storing & Retrieving Session Data
 Php provides us the $_SESSION[] – Environment
variable.
 We can use $_SESSION[“varibaleName”] to store
and retrieve data as we do in our normal practice.
 Example:
<?php
session_start();
$_SESSION[“varibaleName”]=“THIS IS DATA TO BE STORED”;
echo $_SESSION[“varibaleName”];
?>
Copyright © 2012 Iqra Baloch.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Using Session Data/Variables
• Having established a session, you can now create, store and
retrieve information pertaining to that session.
• Information to be stored for a session is kept in session variables.
• This is where you both store and retrieve session data.
• These variables are all stored in a single array $_SESSION.
• <?php
session_start(); // this starts the session
$_SESSION['color']='red'; // this sets variables in the session
$_SESSION['size']='small';
$_SESSION['shape']='round';
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Retrieving Session Data
• The PHP session's $_SESSION variables can be
used in exactly the same way as other PHP variables.
• To retrieve a session variable you simply need to echo
that variable.
• If you need to access that variable on any other page
you need to start session first.
• <?php
session_start(); // this starts the session
echo $_SESSION['color']; // this is session variable.
echo $_SESSION['size'];
echo $_SESSION['shape'];
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Page 1
Page 2
Page 3
Page 4
Page 5
Page 6
Page 2
Page 1
Page 3
Page 4
Page 5
Page 6
Page 3
Page 1
Page 2
Page 4
Page 5
Page 6
Page 4
Page 1
Page 2
Page 3
Page 5
Page 6
Page 5
Page 1
Page 2
Page 3
Page 4
Page 6
Page 6
Page 1 Visits = 3
Page 2 Visits = 5
Page 3 Visits = 7
Page 4 Visits = 2
Page 5 Visits = 9
Assignment 1:
Make six web pages, every page contain the links of all other
pages, on the sixth page the result of all visited pages should be
shown that how many times the user has visited each page.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Assignment 2:
• User login validation with session maintenance.
Username:
Password:
Hidaya
Trust
Login
Enter Username
& Password
Click
Welcome Hidaya
MY SITE
Direct access
Welcome Hidaya
MY SITE
Username:
Password:
Login
Please Login First.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Session Expiry:
• By default, PHP sessions expire:
• After a certain length of inactivity (default
1440s/24 min), the PHP garbage collection
processes deletes session variables.
• To check session expiry time stored in php.ini
file we use:
ini_get(“session.gc_maxlifetime”);
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Unset():
Description:
• unset() destroys the specified variables.
Return Values
• No value is returned.
Note :
• If $_SESSION is used, use unset() to unregister a session
variable, i.e. unset ($_SESSION['varname']);.
Caution:
• Do NOT unset the whole $_SESSION with unset($_SESSION)
as this will disable the registering of session variables through
the $_SESSION superglobal.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_destroy:
Description:
• Destroys all data registered to a session.
• session_destroy() destroys all of the data associated with the
current session.
• It does not unset any of the global variables associated with the
session, or unset the session cookie.
Return Values
• Returns TRUE on success or FALSE on failure.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_id:
Description:
• session_id() Get and/or set the current session id.
Parameters:
• id
• If id is specified, it will replace the current session id.
• session_id() needs to be called before session_start() for that
purpose.
• Depending on the session handler, not all characters are allowed
within the session id. For example, the file session handler only
allows characters in the range a-z A-Z 0-9 , (comma) and - (minus)!
Return Values
• session_id() returns the session id for the current session or the
empty string ("") if there is no current session (no current session id
exists).
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_id (Contd:)
Example :
<?php
/* set the session id to hidaya */
session_id(“hidaya");
session_start();
echo “My session id is session_id<br />";
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_name:
Description:
• Get and/or set the current session name.
Parameters:
• name
• The session name references the name of the session, which is
used in cookies. It should contain only alphanumeric characters;
it should be short and descriptive. If name is specified, the name
of the current session is changed to its value.
Warning The session name can't consist of digits only, at least one
letter must be present. Otherwise a new session id is generated every
time.
Return Values
• Returns the name of the current session.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_name (Contd:)
Example :
<?php
/* set the session name to MyWebsite */
session_name(“MyWebsite");
session_start();
echo “My session name is session_name<br />";
?>
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_save_path:
Description:
• Get and/or set the current session save path.
• session_save_path() returns the path of the current directory
used to save session data.
Parameters:
• Path
• Session data path.
• If specified, the path to which data is saved will be changed.
• session_save_path() needs to be called before session_start()
for that purpose.
Return Values:
• Returns the path of the current directory used for data storage.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
session_save_path:(Contd:)
Example :
<?php
session_save_path("C:users");
session_start();
echo “My session path is session_save_path <br />";
?>

Mais conteúdo relacionado

Mais procurados (20)

Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
java Cookies
java Cookiesjava Cookies
java Cookies
 
Files in php
Files in phpFiles in php
Files in php
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Css box-model
Css box-modelCss box-model
Css box-model
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Html frames
Html framesHtml frames
Html frames
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
Presentation of bootstrap
Presentation of bootstrapPresentation of bootstrap
Presentation of bootstrap
 
Php array
Php arrayPhp array
Php array
 
jQuery
jQueryjQuery
jQuery
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response Structure
 
Bootstrap 5 ppt
Bootstrap 5 pptBootstrap 5 ppt
Bootstrap 5 ppt
 

Destaque

Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topicsSpy Seat
 
Php 03 Sessoes Cookies Cabecalhos
Php 03 Sessoes Cookies CabecalhosPhp 03 Sessoes Cookies Cabecalhos
Php 03 Sessoes Cookies CabecalhosRegis Magalhães
 
Aula 11 - Controle de sessão em PHP - Programação Web
Aula 11  - Controle de sessão em PHP - Programação WebAula 11  - Controle de sessão em PHP - Programação Web
Aula 11 - Controle de sessão em PHP - Programação WebDalton Martins
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with sessionFirdaus Adib
 
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.Leonardo Soares
 
Cookies e Sessões e PHP
Cookies e Sessões e PHPCookies e Sessões e PHP
Cookies e Sessões e PHPHumberto Moura
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introductionProgrammer Blog
 

Destaque (13)

Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topics
 
Php 03 Sessoes Cookies Cabecalhos
Php 03 Sessoes Cookies CabecalhosPhp 03 Sessoes Cookies Cabecalhos
Php 03 Sessoes Cookies Cabecalhos
 
Aula 11 - Controle de sessão em PHP - Programação Web
Aula 11  - Controle de sessão em PHP - Programação WebAula 11  - Controle de sessão em PHP - Programação Web
Aula 11 - Controle de sessão em PHP - Programação Web
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
Aula 5 - Cookies e Sessões em PHP
Aula 5 - Cookies e Sessões em PHPAula 5 - Cookies e Sessões em PHP
Aula 5 - Cookies e Sessões em PHP
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Session por nieves
Session por nievesSession por nieves
Session por nieves
 
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.
Manipulação de formulários com PHP. Uso de Cookies e Session com PHP.
 
Cookies e Sessões e PHP
Cookies e Sessões e PHPCookies e Sessões e PHP
Cookies e Sessões e PHP
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Php session
Php sessionPhp session
Php session
 
Php string function
Php string function Php string function
Php string function
 
Session php
Session phpSession php
Session php
 

Semelhante a Sessions in php

season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)kunjan shah
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptxITNet
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxShitalGhotekar
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erickokelloerick
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfHumphreyOwuor1
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptSreejithVP7
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In PhpHarit Kothari
 
php $_GET / $_POST / $_SESSION
php  $_GET / $_POST / $_SESSIONphp  $_GET / $_POST / $_SESSION
php $_GET / $_POST / $_SESSIONtumetr1
 
Php 07-cookies-sessions
Php 07-cookies-sessionsPhp 07-cookies-sessions
Php 07-cookies-sessionsYUSRA FERNANDO
 
Jsp session tracking
Jsp   session trackingJsp   session tracking
Jsp session trackingrvarshneyp
 
Authentication and Single Sing on
Authentication and Single Sing onAuthentication and Single Sing on
Authentication and Single Sing onguest648519
 
PHP Interview Questions-ppt
PHP Interview Questions-pptPHP Interview Questions-ppt
PHP Interview Questions-pptMayank Kumar
 
PHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPPHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPLariya Minhaz
 

Semelhante a Sessions in php (20)

season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
Php sessions
Php sessionsPhp sessions
Php sessions
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptx
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
Security in php
Security in phpSecurity in php
Security in php
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
Manish
ManishManish
Manish
 
Sessions n cookies
Sessions n cookiesSessions n cookies
Sessions n cookies
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
 
php $_GET / $_POST / $_SESSION
php  $_GET / $_POST / $_SESSIONphp  $_GET / $_POST / $_SESSION
php $_GET / $_POST / $_SESSION
 
session.ppt
session.pptsession.ppt
session.ppt
 
Php 07-cookies-sessions
Php 07-cookies-sessionsPhp 07-cookies-sessions
Php 07-cookies-sessions
 
Jsp session tracking
Jsp   session trackingJsp   session tracking
Jsp session tracking
 
Authentication and Single Sing on
Authentication and Single Sing onAuthentication and Single Sing on
Authentication and Single Sing on
 
PHP Interview Questions-ppt
PHP Interview Questions-pptPHP Interview Questions-ppt
PHP Interview Questions-ppt
 
PHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPPHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHP
 
It and ej
It and ejIt and ej
It and ej
 

Mais de Mudasir Syed

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php Mudasir Syed
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2Mudasir Syed
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1Mudasir Syed
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDFMudasir Syed
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHPMudasir Syed
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2Mudasir Syed
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1 Mudasir Syed
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminMudasir Syed
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joinsMudasir Syed
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction databaseMudasir Syed
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1Mudasir Syed
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagramMudasir Syed
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatinMudasir Syed
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functionsMudasir Syed
 

Mais de Mudasir Syed (20)

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1
 
Ajax
Ajax Ajax
Ajax
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDF
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1
 
Php Mysql
Php Mysql Php Mysql
Php Mysql
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdmin
 
Sql select
Sql select Sql select
Sql select
 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql Sql
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joins
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagram
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatin
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 

Último

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 

Último (20)

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 

Sessions in php

  • 1. Session In Php By: IQRA BALOCH.
  • 2. Contents  What is Session?  Why, Where Session is Used?  How Sessions Works?  Session As File.  Session Expiry Copyright © 2012 Muhammad Baqar Qazi.
  • 3. What Is Session?  Generally session means limited period of time. Or  A meeting or period devoted to a particular activity.  To get understood about sessions in php we need to go back and review our concepts regarding http, html, client and server.  It was clearly observed that web is stateless.  There is no continuous connection between client and server.  For-example :  The server does not know that the current page was requested in past by the user or not.  A browser request a page from server – connection opens between both, server get the request and sends back the response – connection closed. Copyright © 2012 Iqra Baloch.
  • 4. What is Session (Continued)  Session:  Is one that makes the web satefull. Simply maintaining the state.  Allows to track information of user.  A session is basically a way of storing variables and making them available across multiple pages on your web site.  Session is unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page. Copyright © 2012 Iqra Baloch.
  • 5. What is Session (Continued)  In web applications, a session is the sequence of interactions between the server and a user.  A session refers to a period of activity when a PHP script stores state information on a Web server.  All session data is stored on the server (usually in a database, to which the client does not have direct access) linked to that identifier.  A PHP session provides an easy, but effective, way of passing variables between web pages in a PHP application. The session starts when a PHP site is accessed and ends when the user closes their web browser. Copyright © 2012 Iqra Baloch.
  • 6. Why, Where Session is used (Need)?  Previously we store the information in a hidden form field. Even then, it persists only if the form is submitted.  To make data accessible across the various pages of an entire website.  Suppose a user is buying book online, there should be a mechanism to keep track of books added by user to basket.  Very common example be the login system, where there is need of state maintenance to check whether user is logged in or not- is the user valid. Copyright © 2012 Iqra Baloch.
  • 7. How Session Work  Session Is a file stored on Server.  To find which file belongs to which user a cookie is created.  A special session cookie is set on their browser and then we look that cookie to find the place where a file resides on our server and then we can look in that file to find information in session.  The information is not there for them to see or to edit they can only change id which reference to file.  They can change id number is very long and complicated to help to ensure we do not end up with hijacking of other users session information. Copyright © 2012 Iqra Baloch.
  • 8. How Session Works (Continued)  To use session we use to create a file and set cookie on user machine. Then we need to find the cookie and the corresponding file on the machine this be done with session_start().  The session_start() function starts a new session or continues an existing one  The session_start() function generates a unique session ID to identify the session  A session ID is a random alphanumeric string that looks something like: 7f39d7dd020773f115d753c71290e11f  The session_start() function creates a text file on the Web server that is the same name as the session ID, preceded by sess_ Copyright © 2012 Iqra Baloch.
  • 9. How Session Works (Continued)  <?php  session_start();  ?>  This command should be called only once in each page, and it must be called before the PHP script generates any output, so the ideal position is immediately after the opening PHP tag.  If any output is generated before the call to session_start(), the command fails and the session won’t be activated for that page. Copyright © 2012 Iqra Baloch.
  • 10. How Session Works (Continued)  When a session is initiated, the server stores information in session variables that can be accessed by other pages as long as the session remains active (normally until the browser is closed).  Because the identifier is unique to each visitor, the information stored in session variables cannot be seen by anyone else. Copyright © 2012 Iqra Baloch.
  • 11. Session As File.  The cookie stored in the browser is called PHPSESSID, unique identifier.  A matching file, which contains the same unique identifier as part of its filename, is created on the web server.  PHP sessions store a unique identifier as a cookie in the browser (left) and on the server(right). Copyright © 2012 Iqra Baloch.
  • 12. Session As File(Continued).  There is no danger of private information being exposed through someone examining the contents of a cookie on a shared computer.  The session variables and their values are stored on the web server.  Figure Shows contents of a simple session file.  As you can see, it’s in plain text, and the content isn’t difficult to decipher.  The session shown in the figure has two variables: name and location.  The variable names are followed by a vertical pipe, then the letter “s”, a colon, a number, another colon, and the variable’s value in quotes. The “s” stands for string, and the number indicates how many characters the string contains. Copyright © 2012 Iqra Baloch.
  • 13. Storing & Retrieving Session Data  Php provides us the $_SESSION[] – Environment variable.  We can use $_SESSION[“varibaleName”] to store and retrieve data as we do in our normal practice.  Example: <?php session_start(); $_SESSION[“varibaleName”]=“THIS IS DATA TO BE STORED”; echo $_SESSION[“varibaleName”]; ?> Copyright © 2012 Iqra Baloch.
  • 14. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Using Session Data/Variables • Having established a session, you can now create, store and retrieve information pertaining to that session. • Information to be stored for a session is kept in session variables. • This is where you both store and retrieve session data. • These variables are all stored in a single array $_SESSION. • <?php session_start(); // this starts the session $_SESSION['color']='red'; // this sets variables in the session $_SESSION['size']='small'; $_SESSION['shape']='round'; ?>
  • 15. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Retrieving Session Data • The PHP session's $_SESSION variables can be used in exactly the same way as other PHP variables. • To retrieve a session variable you simply need to echo that variable. • If you need to access that variable on any other page you need to start session first. • <?php session_start(); // this starts the session echo $_SESSION['color']; // this is session variable. echo $_SESSION['size']; echo $_SESSION['shape']; ?>
  • 16. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 2 Page 1 Page 3 Page 4 Page 5 Page 6 Page 3 Page 1 Page 2 Page 4 Page 5 Page 6 Page 4 Page 1 Page 2 Page 3 Page 5 Page 6 Page 5 Page 1 Page 2 Page 3 Page 4 Page 6 Page 6 Page 1 Visits = 3 Page 2 Visits = 5 Page 3 Visits = 7 Page 4 Visits = 2 Page 5 Visits = 9 Assignment 1: Make six web pages, every page contain the links of all other pages, on the sixth page the result of all visited pages should be shown that how many times the user has visited each page.
  • 17. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Assignment 2: • User login validation with session maintenance. Username: Password: Hidaya Trust Login Enter Username & Password Click Welcome Hidaya MY SITE Direct access Welcome Hidaya MY SITE Username: Password: Login Please Login First.
  • 18. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Session Expiry: • By default, PHP sessions expire: • After a certain length of inactivity (default 1440s/24 min), the PHP garbage collection processes deletes session variables. • To check session expiry time stored in php.ini file we use: ini_get(“session.gc_maxlifetime”);
  • 19. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Unset(): Description: • unset() destroys the specified variables. Return Values • No value is returned. Note : • If $_SESSION is used, use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);. Caution: • Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.
  • 20. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_destroy: Description: • Destroys all data registered to a session. • session_destroy() destroys all of the data associated with the current session. • It does not unset any of the global variables associated with the session, or unset the session cookie. Return Values • Returns TRUE on success or FALSE on failure.
  • 21. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_id: Description: • session_id() Get and/or set the current session id. Parameters: • id • If id is specified, it will replace the current session id. • session_id() needs to be called before session_start() for that purpose. • Depending on the session handler, not all characters are allowed within the session id. For example, the file session handler only allows characters in the range a-z A-Z 0-9 , (comma) and - (minus)! Return Values • session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).
  • 22. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_id (Contd:) Example : <?php /* set the session id to hidaya */ session_id(“hidaya"); session_start(); echo “My session id is session_id<br />"; ?>
  • 23. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_name: Description: • Get and/or set the current session name. Parameters: • name • The session name references the name of the session, which is used in cookies. It should contain only alphanumeric characters; it should be short and descriptive. If name is specified, the name of the current session is changed to its value. Warning The session name can't consist of digits only, at least one letter must be present. Otherwise a new session id is generated every time. Return Values • Returns the name of the current session.
  • 24. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_name (Contd:) Example : <?php /* set the session name to MyWebsite */ session_name(“MyWebsite"); session_start(); echo “My session name is session_name<br />"; ?>
  • 25. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_save_path: Description: • Get and/or set the current session save path. • session_save_path() returns the path of the current directory used to save session data. Parameters: • Path • Session data path. • If specified, the path to which data is saved will be changed. • session_save_path() needs to be called before session_start() for that purpose. Return Values: • Returns the path of the current directory used for data storage.
  • 26. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org session_save_path:(Contd:) Example : <?php session_save_path("C:users"); session_start(); echo “My session path is session_save_path <br />"; ?>