SlideShare a Scribd company logo
1 of 28
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?

More Related Content

What's hot (20)

PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
Using PHP
Using PHPUsing PHP
Using PHP
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
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 ...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 ...
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Web technology html5 php_mysql
Web technology html5 php_mysqlWeb technology html5 php_mysql
Web technology html5 php_mysql
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Sa
SaSa
Sa
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php
PhpPhp
Php
 

Viewers also liked

Php & mysql course syllabus
Php & mysql course syllabusPhp & mysql course syllabus
Php & mysql course syllabusPapitha Velumani
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionAhmed Swilam
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorialalexjones89
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answersiimjobs and hirist
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm
 
Php project training in ahmedabad
Php project training in ahmedabadPhp project training in ahmedabad
Php project training in ahmedabadDevelopers Academy
 
Php Coding Convention
Php Coding ConventionPhp Coding Convention
Php Coding ConventionPhuong Vy
 

Viewers also liked (20)

Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Php & mysql course syllabus
Php & mysql course syllabusPhp & mysql course syllabus
Php & mysql course syllabus
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Introduction to php web programming - sessions and cookies
Introduction to php   web programming - sessions and cookiesIntroduction to php   web programming - sessions and cookies
Introduction to php web programming - sessions and cookies
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQL
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Php project training in ahmedabad
Php project training in ahmedabadPhp project training in ahmedabad
Php project training in ahmedabad
 
Coding In Php
Coding In PhpCoding In Php
Coding In Php
 
Php Coding Convention
Php Coding ConventionPhp Coding Convention
Php Coding Convention
 

Similar to Class 6 - PHP Web Programming

PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxShitalGhotekar
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Subhasis Nayak
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentPHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentEdureka!
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Salvatore Iaconesi
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiNaveen Kumar Veligeti
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptGiyaShefin
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptMercyL2
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfShaimaaMohamedGalal
 
04 Html Form Get Post Login System
04 Html Form Get Post Login System04 Html Form Get Post Login System
04 Html Form Get Post Login SystemGeshan Manandhar
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 

Similar to Class 6 - PHP Web Programming (20)

Php session
Php sessionPhp session
Php session
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
PHP 2
PHP 2PHP 2
PHP 2
 
18.register login
18.register login18.register login
18.register login
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentPHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Php BASIC
Php BASICPhp BASIC
Php BASIC
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
introduction_php.ppt
introduction_php.pptintroduction_php.ppt
introduction_php.ppt
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
 
04 Html Form Get Post Login System
04 Html Form Get Post Login System04 Html Form Get Post Login System
04 Html Form Get Post Login System
 
Php
PhpPhp
Php
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 

Recently uploaded

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

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.