SlideShare a Scribd company logo
PHP Web Programming
Outline
Request Types
1- ‘GET’ request:
Is the most common type of request. An example of it is
writing the URL in your browser then clicking enter.
Get requests can have parameters passed with the URL.
http://www.google.com/search?q=ronaldo&client=ubuntuParametersDomain nameProtocol Script
Request Types
1- ‘POST’ request:
It is another type of request in which the browser passes the
parameters to the server in a hidden way. Example of this is
submitting a form with a method=post.
<form action=“/search” method=“POST”>
Text : <input type=“text” name=“query” />
<input type=“submit” value=“Search” />
</form>
Getting Parameter Values in PHP
In PHP we have some special variables that store the
parameters that are passed during the request.
1- $_GET
This is an associative array that holds the parameters
that are passed in a GET request.
For example:
If I have a php script named “getit.php” on my local machine
that looks like this :
<?php
var_dump($_GET);
?>
Getting Parameter Values in PHP
When opening it like this :
http://localhost/getit.php?name=john&age=15
The out put should be like this :
array(2) {
["name"]=>
string(4) "john"
["age"]=>
string(2) "15"
}
Getting Parameter Values in PHP
2- $_POST :
This is an associative array that contains all the passed
parameters using POST request.
For example:
If we have page called “form.php” that contains the
following :
<form action=“/postit.php” method=“POST”>
Text : <input type=“text” name=“query” />
<input type=“submit” value=“Search” />
</form>
Getting Parameter Values in PHP
And the script “postit.php” has :
<?php
var_dump($_POST);
?>
Opening that script :
http://localhost/form.php
After opening that and putting a value “Hello” in the text
box and clicking on the search button, you should see :
array(1) {
["query"]=>
string(5) "Hello"
}
Getting Parameter Values in PHP
3- $_REQUEST:
This variable will contain all the values from the associative
arrays $_GET and $_POST combined ( It also contains the
values of the variable $_COOKIE which will talk about later).
It is used in case I allow the user to pass parameters with the
method he likes ( GET or POST ).
Exercise
Write a PHP script that allows the user to enter his/her
name in a field and after submitting, It should greet them.
For example:
if the user entered “Mohamed” the script should show
“Hello Mohamed”.
Exercise Solution
1. Script name “form.php”:
<html>
<body>
<form action=“/greet.php” method=“POST”>
Text : <input type=“text” name=“name” />
<input type=“submit” value=“Submit” />
</form>
</body>
</html>
Exercise Solution
1. Script name “greet.php”:
<?php
echo “Hello “ . $_POST[‘name’];
?>
Handling file uploads
Uploading a file to a PHP script is easy, you just need to set
the enctype value to “multipart/form-data” and the form
method=POST.
The multi-dimension array called “$_FILES” will contain any
information related to the uploaded files.
To demonstrate, here is an example :
We have a script named “form.php” that contains :
<form enctype="multipart/form-data“ action=“upload.php" method="POST">
File: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload" />
</form>
Handling file uploads
And we have another script named “upload.php” :
<?php
if( is_uploaded_file($_FILES['uploadedfile']['tmp_name']) ){
echo file_get_contents($_FILES['uploadedfile']
['tmp_name']);
}
?>
This script will show the contents of the file once uploaded.
Exercise
Work on the previous exercise to allow the user to enter an
email address and check whether the email is valid or not
and show a message to the user telling so.
Exercise Solution
The first script is named “form.php” :
<html>
<body>
<form action=“/doit.php” method=“POST”>
Name: <input type=“text” name=“name” />
E-mail: <input type=“text” name=“email” />
<input type=“submit” value=“Submit” />
</form>
</body>
</html>
Exercise Solution
The first script is named “doit.php” :
<?php
echo "Your name : ". $_POST['name'] . "<br/>";
echo "Your email: ";
if( preg_match( '/^[w]+@[w]+.[a-z]{2,3}$/i',
$_POST['email']) == 1 )
echo $_POST['email'];
else
echo "Not Valid“;
?>
Cookies
Cookies are a mechanism for storing data in the remote
browser and thus tracking or identifying return users.
• Example of a cookie response header :
Set-Cookie: name2=value2; Domain=.foo.com;
Path=/;Expires=Wed, 09 Jun 2021 10:18:14 GMT
• Example of a cookie request header :
Cookie: name=value; name2=value2
Flash Back –
Class 1
Cookies in PHP
PHP provides the function setcookie() and the global
variable $_COOKIE to allow us to deal with cookies.
bool setcookie ( string $name [, string $value [, int $expire =
0 [, string $path [, string $domain [, bool $secure = false [,
bool $httponly = false ]]]]]] )
setcookie() defines a cookie to be sent along with the rest of
the HTTP headers. Like other headers, cookies must be sent
before any output from your script.
Cookies in PHP
The $_COOKIE super global is used to get the cookies set.
Example:
<?php
setcookie(‘name’, ‘mohamed’, time() + 3600 );
?>
In another script on the same domain we can do this :
<?php
echo $_COOKIE[‘name’]; // mohamed
?>
Sessions
• Session support in PHP consists of a way to preserve
certain data across subsequent accesses.
• This is implemented by creating a cookie with a random
number for the user and associate this data with that id.
• PHP maintains a list of the user ids on the server with
corresponding user data.
Example:
<?php
session_start();
$_SESSION[‘age’] = 20;
?>
Sessions
Another script on the same domain contains:
<?php
session_start();
echo $_SESSION[‘age’] ; // 20
?>
Sessions
Things to note here :
•The session_start() function should be called the first thing
in the script before any output in order to deal with sessions.
•Use session_destroy() if you want to destroy the session
data.
Exercise
Write a web page that uses sessions to keep track of how
many times a user has viewed the page. The first time a
particular user looks at the page, it should print something
like "Number of views: 1." The second time the user looks at
the page, it should print "Number of views: 2," and so on.
Exercise Solution
<?php
session_start();
if( !isset($_SESSION['views']) )
$_SESSION['views'] = 0;
++$_SESSION['views'];
echo "Number of views : " . $_SESSION['views'];
?>
Assignment
Write a web page that uses sessions to keep track of how
long the user viewed the page. It should output something
like “You have been here for X seconds“. Tip use date
function http://php.net/manual/en/function.date.php
What's Next?
• Object Oriented Programming.
Questions?
Ad

Recommended

Introduction to php web programming - get and post
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
PHP Workshop Notes
PHP Workshop Notes
Pamela Fox
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Loops PHP 04
Loops PHP 04
mohamedsaad24
 
Php Lecture Notes
Php Lecture Notes
Santhiya Grace
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Php mysql
Php mysql
Manish Jain
 
PHP Tutorials
PHP Tutorials
Yuriy Krapivko
 
Php and MySQL
Php and MySQL
Tiji Thomas
 
PHP for hacks
PHP for hacks
Tom Praison Praison
 
Phphacku iitd
Phphacku iitd
Sorabh Jain
 
Using PHP
Using PHP
Mark Casias
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
PHP Basic
PHP Basic
Yoeung Vibol
 
Introduction to PHP
Introduction to PHP
Jussi Pohjolainen
 
Web Development Course: PHP lecture 4
Web Development Course: PHP lecture 4
Gheyath M. Othman
 
Introduction to PHP
Introduction to PHP
Bradley Holt
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
What Is Php
What Is Php
AVC
 
Web technology html5 php_mysql
Web technology html5 php_mysql
durai arasan
 
Chapter 02 php basic syntax
Chapter 02 php basic syntax
Dhani Ahmad
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Basics PHP
Basics PHP
Alokin Software Pvt Ltd
 
Sa
Sa
sahul azzez m.i
 
Php mysql
Php mysql
Abu Bakar
 
Php
Php
Shyam Khant
 
Php mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PHP Web Programming
PHP Web Programming
Muthuselvam RS
 

More Related Content

What's hot (20)

PHP Tutorials
PHP Tutorials
Yuriy Krapivko
 
Php and MySQL
Php and MySQL
Tiji Thomas
 
PHP for hacks
PHP for hacks
Tom Praison Praison
 
Phphacku iitd
Phphacku iitd
Sorabh Jain
 
Using PHP
Using PHP
Mark Casias
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
PHP Basic
PHP Basic
Yoeung Vibol
 
Introduction to PHP
Introduction to PHP
Jussi Pohjolainen
 
Web Development Course: PHP lecture 4
Web Development Course: PHP lecture 4
Gheyath M. Othman
 
Introduction to PHP
Introduction to PHP
Bradley Holt
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
What Is Php
What Is Php
AVC
 
Web technology html5 php_mysql
Web technology html5 php_mysql
durai arasan
 
Chapter 02 php basic syntax
Chapter 02 php basic syntax
Dhani Ahmad
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Basics PHP
Basics PHP
Alokin Software Pvt Ltd
 
Sa
Sa
sahul azzez m.i
 
Php mysql
Php mysql
Abu Bakar
 
Php
Php
Shyam Khant
 

Viewers also liked (20)

Php mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Php & mysql course syllabus
Php & mysql course syllabus
Papitha Velumani
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
Ahmed Swilam
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHP
Ahmed Swilam
 
Class 8 - Database Programming
Class 8 - Database Programming
Ahmed Swilam
 
Introduction to php web programming - sessions and cookies
Introduction to php web programming - sessions and cookies
baabtra.com - No. 1 supplier of quality freshers
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Class 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Class 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Class 3 - PHP Functions
Class 3 - PHP Functions
Ahmed Swilam
 
Php Presentation
Php Presentation
Manish Bothra
 
01 Php Introduction
01 Php Introduction
Geshan Manandhar
 
Beginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQL
Alphorm
 
Oops in PHP
Oops in PHP
Mindfire Solutions
 
Php project training in ahmedabad
Php project training in ahmedabad
Developers Academy
 
Coding In Php
Coding In Php
Harit Kothari
 
Php Coding Convention
Php Coding Convention
Phuong Vy
 
Php & mysql course syllabus
Php & mysql course syllabus
Papitha Velumani
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
Ahmed Swilam
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHP
Ahmed Swilam
 
Class 8 - Database Programming
Class 8 - Database Programming
Ahmed Swilam
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Class 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Class 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Class 3 - PHP Functions
Class 3 - PHP Functions
Ahmed Swilam
 
Beginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQL
Alphorm
 
Php project training in ahmedabad
Php project training in ahmedabad
Developers Academy
 
Php Coding Convention
Php Coding Convention
Phuong Vy
 
Ad

Similar to Class 6 - PHP Web Programming (20)

Web app development_php_07
Web app development_php_07
Hassen Poreya
 
PHP 2
PHP 2
Richa Goel
 
Php Tutorial
Php Tutorial
Dr. Ramkumar Lakshminarayanan
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATION
krutitrivedi
 
Php mysql
Php mysql
Ajit Yadav
 
PHP Making Web Forms
PHP Making Web Forms
krishnapriya Tadepalli
 
Php with mysql ppt
Php with mysql ppt
Rajamanickam Gomathijayam
 
Chapter 08 php advance
Chapter 08 php advance
Dhani Ahmad
 
Unit 1
Unit 1
tamilmozhiyaltamilmo
 
PHP language presentation
PHP language presentation
Annujj Agrawaal
 
php_postgresql.ppt
php_postgresql.ppt
ElieNGOMSEU
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connecting
PraveenHegde20
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....
Lalith86
 
php_postgresql.ppt
php_postgresql.ppt
SibabrataChoudhury2
 
php_postgresql.pptyyguyg7g7g777g76776777
php_postgresql.pptyyguyg7g7g777g76776777
kanakulyakevin9
 
Learn php with PSK
Learn php with PSK
Prabhjot Singh Kainth
 
PHP Hypertext Preprocessor
PHP Hypertext Preprocessor
adeel990
 
Php with my sql
Php with my sql
husnara mohammad
 
Working with data.pptx
Working with data.pptx
SherinRappai
 
An introduction to PHP : PHP and Using PHP, Variables Program control and Bui...
An introduction to PHP : PHP and Using PHP, Variables Program control and Bui...
Vigneshkumar Ponnusamy
 
Ad

Recently uploaded (20)

Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 

Class 6 - PHP Web Programming

  • 3. Request Types 1- ‘GET’ request: Is the most common type of request. An example of it is writing the URL in your browser then clicking enter. Get requests can have parameters passed with the URL. http://www.google.com/search?q=ronaldo&client=ubuntuParametersDomain nameProtocol Script
  • 4. Request Types 1- ‘POST’ request: It is another type of request in which the browser passes the parameters to the server in a hidden way. Example of this is submitting a form with a method=post. <form action=“/search” method=“POST”> Text : <input type=“text” name=“query” /> <input type=“submit” value=“Search” /> </form>
  • 5. Getting Parameter Values in PHP In PHP we have some special variables that store the parameters that are passed during the request. 1- $_GET This is an associative array that holds the parameters that are passed in a GET request. For example: If I have a php script named “getit.php” on my local machine that looks like this : <?php var_dump($_GET); ?>
  • 6. Getting Parameter Values in PHP When opening it like this : http://localhost/getit.php?name=john&age=15 The out put should be like this : array(2) { ["name"]=> string(4) "john" ["age"]=> string(2) "15" }
  • 7. Getting Parameter Values in PHP 2- $_POST : This is an associative array that contains all the passed parameters using POST request. For example: If we have page called “form.php” that contains the following : <form action=“/postit.php” method=“POST”> Text : <input type=“text” name=“query” /> <input type=“submit” value=“Search” /> </form>
  • 8. Getting Parameter Values in PHP And the script “postit.php” has : <?php var_dump($_POST); ?> Opening that script : http://localhost/form.php After opening that and putting a value “Hello” in the text box and clicking on the search button, you should see : array(1) { ["query"]=> string(5) "Hello" }
  • 9. Getting Parameter Values in PHP 3- $_REQUEST: This variable will contain all the values from the associative arrays $_GET and $_POST combined ( It also contains the values of the variable $_COOKIE which will talk about later). It is used in case I allow the user to pass parameters with the method he likes ( GET or POST ).
  • 10. Exercise Write a PHP script that allows the user to enter his/her name in a field and after submitting, It should greet them. For example: if the user entered “Mohamed” the script should show “Hello Mohamed”.
  • 11. Exercise Solution 1. Script name “form.php”: <html> <body> <form action=“/greet.php” method=“POST”> Text : <input type=“text” name=“name” /> <input type=“submit” value=“Submit” /> </form> </body> </html>
  • 12. Exercise Solution 1. Script name “greet.php”: <?php echo “Hello “ . $_POST[‘name’]; ?>
  • 13. Handling file uploads Uploading a file to a PHP script is easy, you just need to set the enctype value to “multipart/form-data” and the form method=POST. The multi-dimension array called “$_FILES” will contain any information related to the uploaded files. To demonstrate, here is an example : We have a script named “form.php” that contains : <form enctype="multipart/form-data“ action=“upload.php" method="POST"> File: <input name="uploadedfile" type="file" /> <input type="submit" value="Upload" /> </form>
  • 14. Handling file uploads And we have another script named “upload.php” : <?php if( is_uploaded_file($_FILES['uploadedfile']['tmp_name']) ){ echo file_get_contents($_FILES['uploadedfile'] ['tmp_name']); } ?> This script will show the contents of the file once uploaded.
  • 15. Exercise Work on the previous exercise to allow the user to enter an email address and check whether the email is valid or not and show a message to the user telling so.
  • 16. Exercise Solution The first script is named “form.php” : <html> <body> <form action=“/doit.php” method=“POST”> Name: <input type=“text” name=“name” /> E-mail: <input type=“text” name=“email” /> <input type=“submit” value=“Submit” /> </form> </body> </html>
  • 17. Exercise Solution The first script is named “doit.php” : <?php echo "Your name : ". $_POST['name'] . "<br/>"; echo "Your email: "; if( preg_match( '/^[w]+@[w]+.[a-z]{2,3}$/i', $_POST['email']) == 1 ) echo $_POST['email']; else echo "Not Valid“; ?>
  • 18. Cookies Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. • Example of a cookie response header : Set-Cookie: name2=value2; Domain=.foo.com; Path=/;Expires=Wed, 09 Jun 2021 10:18:14 GMT • Example of a cookie request header : Cookie: name=value; name2=value2 Flash Back – Class 1
  • 19. Cookies in PHP PHP provides the function setcookie() and the global variable $_COOKIE to allow us to deal with cookies. bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] ) setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script.
  • 20. Cookies in PHP The $_COOKIE super global is used to get the cookies set. Example: <?php setcookie(‘name’, ‘mohamed’, time() + 3600 ); ?> In another script on the same domain we can do this : <?php echo $_COOKIE[‘name’]; // mohamed ?>
  • 21. Sessions • Session support in PHP consists of a way to preserve certain data across subsequent accesses. • This is implemented by creating a cookie with a random number for the user and associate this data with that id. • PHP maintains a list of the user ids on the server with corresponding user data. Example: <?php session_start(); $_SESSION[‘age’] = 20; ?>
  • 22. Sessions Another script on the same domain contains: <?php session_start(); echo $_SESSION[‘age’] ; // 20 ?>
  • 23. Sessions Things to note here : •The session_start() function should be called the first thing in the script before any output in order to deal with sessions. •Use session_destroy() if you want to destroy the session data.
  • 24. Exercise Write a web page that uses sessions to keep track of how many times a user has viewed the page. The first time a particular user looks at the page, it should print something like "Number of views: 1." The second time the user looks at the page, it should print "Number of views: 2," and so on.
  • 25. Exercise Solution <?php session_start(); if( !isset($_SESSION['views']) ) $_SESSION['views'] = 0; ++$_SESSION['views']; echo "Number of views : " . $_SESSION['views']; ?>
  • 26. Assignment Write a web page that uses sessions to keep track of how long the user viewed the page. It should output something like “You have been here for X seconds“. Tip use date function http://php.net/manual/en/function.date.php
  • 27. What's Next? • Object Oriented Programming.