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

Similar to 4 php-advanced

PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfHumphreyOwuor1
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionssalissal
 
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
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introductionProgrammer Blog
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & sessionJamshid Hashimi
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptxITNet
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxShitalGhotekar
 
Silverlight 4 @ MSDN Live
Silverlight 4 @ MSDN LiveSilverlight 4 @ MSDN Live
Silverlight 4 @ MSDN Livegoeran
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSDegu8
 
Microsoft Windows Server AppFabric
Microsoft Windows Server AppFabricMicrosoft Windows Server AppFabric
Microsoft Windows Server AppFabricMark Ginnebaugh
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
Whatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processWhatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processguest3379bd
 
murach12.pptx
murach12.pptxmurach12.pptx
murach12.pptxxiso
 
PHP - Getting good with cookies
PHP - Getting good with cookiesPHP - Getting good with cookies
PHP - Getting good with cookiesFirdaus Adib
 

Similar to 4 php-advanced (20)

Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
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
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & session
 
FP512 Cookies sessions
FP512 Cookies sessionsFP512 Cookies sessions
FP512 Cookies sessions
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Silverlight 4 @ MSDN Live
Silverlight 4 @ MSDN LiveSilverlight 4 @ MSDN Live
Silverlight 4 @ MSDN Live
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
Microsoft Windows Server AppFabric
Microsoft Windows Server AppFabricMicrosoft Windows Server AppFabric
Microsoft Windows Server AppFabric
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Sessions n cookies
Sessions n cookiesSessions n cookies
Sessions n cookies
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
Whatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processWhatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the process
 
murach12.pptx
murach12.pptxmurach12.pptx
murach12.pptx
 
PHP - Getting good with cookies
PHP - Getting good with cookiesPHP - Getting good with cookies
PHP - Getting good with cookies
 

More from Achchuthan Yogarajah

Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Achchuthan Yogarajah
 
PADDY CULTIVATION MANAGEMENT SYSTEM
PADDY CULTIVATION MANAGEMENT  SYSTEMPADDY CULTIVATION MANAGEMENT  SYSTEM
PADDY CULTIVATION MANAGEMENT SYSTEMAchchuthan Yogarajah
 
Statistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationStatistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationAchchuthan Yogarajah
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y AchchuthanAchchuthan Yogarajah
 

More from Achchuthan Yogarajah (11)

Managing the design process
Managing the design processManaging the design process
Managing the design process
 
intoduction to network devices
intoduction to network devicesintoduction to network devices
intoduction to network devices
 
basic network concepts
basic network conceptsbasic network concepts
basic network concepts
 
3 php-connect-to-my sql
3 php-connect-to-my sql3 php-connect-to-my sql
3 php-connect-to-my sql
 
PHP Form Handling
PHP Form HandlingPHP Form Handling
PHP Form Handling
 
PHP-introduction
PHP-introductionPHP-introduction
PHP-introduction
 
Introduction to Web Programming
Introduction to Web Programming Introduction to Web Programming
Introduction to Web Programming
 
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
 
PADDY CULTIVATION MANAGEMENT SYSTEM
PADDY CULTIVATION MANAGEMENT  SYSTEMPADDY CULTIVATION MANAGEMENT  SYSTEM
PADDY CULTIVATION MANAGEMENT SYSTEM
 
Statistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationStatistical Machine Translation for Language Localisation
Statistical Machine Translation for Language Localisation
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y Achchuthan
 

Recently uploaded

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptxJosvitaDsouza2
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...SachinKumar945617
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfSpecial education needs
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsparmarsneha2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxJisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 

Recently uploaded (20)

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.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