SlideShare a Scribd company logo
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
National Diploma in Information and Communication Technology
PHP :4-PHP-ADVANCED>
K72C001M07 - Web Programming
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 1
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
What is a Cookie?
• A cookie is often used to identify a user.
• A cookie is a small text file that lets you store a small amount of data
(nearly 4KB) on the user's computer.
• They are typically used to keeping track of information such as
username that the site can retrieve to personalize the page when
user visit the website next time.
Note: Each time the browser requests a page to the server, all the data
in the cookie is automatically sent to the server within the request.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 2
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Setting a Cookie in PHP
The setcookie() function is used to set a cookie in PHP. Make
sure you call the setcookie() function before any output
generated by your script otherwise cookie will not set.
• Syntax
setcookie(name, value, expire, path, domain,
secure);
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 3
Parameter Description
name The name of the cookie.
value The value of the cookie. Do not store sensitive information since this value is stored on the
user's computer.
expires The expiry date in UNIX timestamp format. After this time cookie will become inaccessible. The
default value is 0.
path Specify the path on the server for which the cookie will be available. If set to /, the cookie will
be available within the entire domain.
domain Specify the domain for which the cookie is available to e.g www.example.com.
secure This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection
exists.
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Create a Cookie
The following example creates a cookie named "user" with the value
"John Smith". The cookie will expire after 30 days (30 days * 24 hours *
60 min * 60 sec)
//setCookie.php
<?php
$cookie_name = "user";
$cookie_value = "John Smith";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/");
?>
Note: The setcookie() function must appear BEFORE the <html> tag.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 4
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Retrieve a Cookie
The PHP $_COOKIE super global variable is used to retrieve a cookie value. It typically an
associative array that contains a list of all the cookies values sent by the browser in the
current request, keyed by cookie name.
//cookie.php
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not
set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>"; echo
"Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 5
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Removing Cookies
You can delete a cookie by calling the same setcookie() function with
the cookie name and any value (such as an empty string) however this
time you need the set the expiration date in the past, as shown in the
example below:
//removeCookie.php
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() – 3600, "/");
?>
Note: You should pass exactly the same path, domain, and other
arguments that you have used when you first created the cookie in
order to ensure that the correct cookie is deleted.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 6
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
What is a PHP Session?
When you work with an application, you open it, do some changes,
and then you close it. This is much like a Session.
• The computer knows who you are. It knows when you start the
application and when you end. But on the internet there is one
problem: the web server does not know who you are or what you do,
because the HTTP address doesn't maintain state.
• Session variables solve this problem by storing user information to be
used across multiple pages (e.g. username, favorite color, etc). By
default, session variables last until the user closes the browser.
• Session variables hold information about one single user, and are
available to all pages in one application.
Note: If you need a permanent storage, you may want to store the
data in a database.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 7
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Start a PHP Session
Before you can store any information in session variables, you must
first start up the session. To begin a new session, simply call the PHP
session_start() function. It will create a new session and generate a
unique session ID for the user.
The PHP code in the example below simply starts a new session.
<?php
// Starting session
session_start();
?>
Note: You must call the session_start() function at the beginning of the
page i.e. before any output generated by your script in the browser,
much like you do while setting the cookies with setcookie() function.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 8
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Storing Session Data
You can store all your session data as key-value pairs in the $_SESSION[]
superglobal array. The stored data can be accessed during lifetime of a
session. Consider the following script, which creates a new session and
registers two session variables.
<?php
// Starting session
session_start();
// Storing session data
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 9
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Accessing Session Data
To access the session data we set on our previous example from any other
page on the same web domain — simply recreate the session by calling
session_start() and then pass the corresponding key to the $_SESSION
associative array.
<?php
session_start();
if(!isset($_SESSION["firstname"])) {
echo "<h3>Session is not set!</h3>";
} else{
echo 'Hi, ' . $_SESSION["firstname"] . ' '
. $_SESSION["lastname"];
}
?>
Note: To access the session data in the same page there is no need to recreate
the session since it has been already started on the top of the page.
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 10
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Destroying a Session
If you want to remove certain session data, simply unset the corresponding
key of the $_SESSION associative array, as shown in the following example:
<?php
session_start();
if(isset($_SESSION["lastname"])){
unset($_SESSION["lastname"]);
}
?>
However, to destroy a session completely, simply call the session_destroy()
function. This function does not need any argument and a single call destroys
all the session data.
session_unset(); //remove all session variables
session_destroy(); //destroy the session
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 11
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Reference
www.w3schools.com
www.php.net
23 November 2018 K72C001M07 - Web Programming / Advanced PHP 12

More Related Content

PPT
Cookies and sessions
PPT
Manish
ODP
Session Management & Cookies In Php
PDF
Web app development_cookies_sessions_14
PPTX
Sessions in php
PDF
Digital certificates
PPT
Lecture8 php page control by okello erick
PDF
Open SSL and MS Crypto API EKON21
Cookies and sessions
Manish
Session Management & Cookies In Php
Web app development_cookies_sessions_14
Sessions in php
Digital certificates
Lecture8 php page control by okello erick
Open SSL and MS Crypto API EKON21

Similar to 4 php-advanced (20)

PPT
PDF
PHP-Cookies-Sessions.pdf
PDF
Cookies and sessions
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
Php ssession - cookies -introduction
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPSX
Sessions and cookies
PPTX
Php file upload, cookies & session
PPTX
FP512 Cookies sessions
PPTX
lecture 12.pptx
PPTX
PHP SESSIONS & COOKIE.pptx
PPTX
Silverlight 4 @ MSDN Live
PPTX
PHP COOKIES AND SESSIONS
PDF
4.4 PHP Session
PPTX
Microsoft Windows Server AppFabric
ODP
Caching and tuning fun for high scalability
PPT
9780538745840 ppt ch09
PDF
Whatever it takes - Fixing SQLIA and XSS in the process
PPTX
murach12.pptx
PHP-Cookies-Sessions.pdf
Cookies and sessions
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Php ssession - cookies -introduction
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Sessions and cookies
Php file upload, cookies & session
FP512 Cookies sessions
lecture 12.pptx
PHP SESSIONS & COOKIE.pptx
Silverlight 4 @ MSDN Live
PHP COOKIES AND SESSIONS
4.4 PHP Session
Microsoft Windows Server AppFabric
Caching and tuning fun for high scalability
9780538745840 ppt ch09
Whatever it takes - Fixing SQLIA and XSS in the process
murach12.pptx
Ad

More from Achchuthan Yogarajah (11)

PPTX
Managing the design process
PPTX
intoduction to network devices
PPTX
basic network concepts
PPTX
3 php-connect-to-my sql
PPTX
PHP Form Handling
PPTX
PHP-introduction
PPTX
Introduction to Web Programming
PPTX
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
PPTX
PADDY CULTIVATION MANAGEMENT SYSTEM
PPTX
Statistical Machine Translation for Language Localisation
PDF
Greedy Knapsack Problem - by Y Achchuthan
Managing the design process
intoduction to network devices
basic network concepts
3 php-connect-to-my sql
PHP Form Handling
PHP-introduction
Introduction to Web Programming
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
PADDY CULTIVATION MANAGEMENT SYSTEM
Statistical Machine Translation for Language Localisation
Greedy Knapsack Problem - by Y Achchuthan
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Sports Quiz easy sports quiz sports quiz
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Computing-Curriculum for Schools in Ghana
Module 4: Burden of Disease Tutorial Slides S2 2025
Sports Quiz easy sports quiz sports quiz
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf

4 php-advanced

  • 1. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology National Diploma in Information and Communication Technology PHP :4-PHP-ADVANCED> K72C001M07 - Web Programming 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 1
  • 2. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology What is a Cookie? • A cookie is often used to identify a user. • A cookie is a small text file that lets you store a small amount of data (nearly 4KB) on the user's computer. • They are typically used to keeping track of information such as username that the site can retrieve to personalize the page when user visit the website next time. Note: Each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 2
  • 3. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Setting a Cookie in PHP The setcookie() function is used to set a cookie in PHP. Make sure you call the setcookie() function before any output generated by your script otherwise cookie will not set. • Syntax setcookie(name, value, expire, path, domain, secure); 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 3 Parameter Description name The name of the cookie. value The value of the cookie. Do not store sensitive information since this value is stored on the user's computer. expires The expiry date in UNIX timestamp format. After this time cookie will become inaccessible. The default value is 0. path Specify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain. domain Specify the domain for which the cookie is available to e.g www.example.com. secure This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists.
  • 4. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Create a Cookie The following example creates a cookie named "user" with the value "John Smith". The cookie will expire after 30 days (30 days * 24 hours * 60 min * 60 sec) //setCookie.php <?php $cookie_name = "user"; $cookie_value = "John Smith"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); ?> Note: The setcookie() function must appear BEFORE the <html> tag. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 4
  • 5. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Retrieve a Cookie The PHP $_COOKIE super global variable is used to retrieve a cookie value. It typically an associative array that contains a list of all the cookies values sent by the browser in the current request, keyed by cookie name. //cookie.php <html> <body> <?php $cookie_name = "user"; if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> </body> </html> 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 5
  • 6. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Removing Cookies You can delete a cookie by calling the same setcookie() function with the cookie name and any value (such as an empty string) however this time you need the set the expiration date in the past, as shown in the example below: //removeCookie.php <?php // set the expiration date to one hour ago setcookie("user", "", time() – 3600, "/"); ?> Note: You should pass exactly the same path, domain, and other arguments that you have used when you first created the cookie in order to ensure that the correct cookie is deleted. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 6
  • 7. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology What is a PHP Session? When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. • The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state. • Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser. • Session variables hold information about one single user, and are available to all pages in one application. Note: If you need a permanent storage, you may want to store the data in a database. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 7
  • 8. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Start a PHP Session Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session. <?php // Starting session session_start(); ?> Note: You must call the session_start() function at the beginning of the page i.e. before any output generated by your script in the browser, much like you do while setting the cookies with setcookie() function. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 8
  • 9. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Storing Session Data You can store all your session data as key-value pairs in the $_SESSION[] superglobal array. The stored data can be accessed during lifetime of a session. Consider the following script, which creates a new session and registers two session variables. <?php // Starting session session_start(); // Storing session data $_SESSION["firstname"] = "Peter"; $_SESSION["lastname"] = "Parker"; ?> 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 9
  • 10. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Accessing Session Data To access the session data we set on our previous example from any other page on the same web domain — simply recreate the session by calling session_start() and then pass the corresponding key to the $_SESSION associative array. <?php session_start(); if(!isset($_SESSION["firstname"])) { echo "<h3>Session is not set!</h3>"; } else{ echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"]; } ?> Note: To access the session data in the same page there is no need to recreate the session since it has been already started on the top of the page. 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 10
  • 11. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Destroying a Session If you want to remove certain session data, simply unset the corresponding key of the $_SESSION associative array, as shown in the following example: <?php session_start(); if(isset($_SESSION["lastname"])){ unset($_SESSION["lastname"]); } ?> However, to destroy a session completely, simply call the session_destroy() function. This function does not need any argument and a single call destroys all the session data. session_unset(); //remove all session variables session_destroy(); //destroy the session 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 11
  • 12. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Reference www.w3schools.com www.php.net 23 November 2018 K72C001M07 - Web Programming / Advanced PHP 12