SlideShare a Scribd company logo
1 of 26
Download to read offline
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 />";
?>

More Related Content

What's hot (20)

Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Php array
Php arrayPhp array
Php array
 
Php forms
Php formsPhp forms
Php forms
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java beans
Java beansJava beans
Java beans
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web Architecture
 
Get method and post method
Get method and post methodGet method and post method
Get method and post method
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Php session
Php sessionPhp session
Php session
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
MVC Architecture
MVC ArchitectureMVC Architecture
MVC Architecture
 

Viewers also liked

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
 

Viewers also liked (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 string function
Php string function Php string function
Php string function
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Session php
Session phpSession php
Session php
 

Similar to Sessions in php

Similar to 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
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
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
 

More from 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
 

More from 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
 

Recently uploaded

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfTechSoup
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 

Recently uploaded (20)

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 

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 />"; ?>