SlideShare una empresa de Scribd logo
1 de 26
SIMPLE ROUTER
Salim Malakouti
Goal
 Work with dates in PHP
 Handle a simple form
 Get to know $_SERVER and $_GET variables
more
 Work with .htaccess files
What are we doing?
 We are creating a website that has two
functionalities:
 Calendar
 Shows the current date
 Birthday
 Receives birthday
 Prints it and tells you your age
What else
 Originally, the url of the pages are in the fomr:
 localhost/example1/calendar.php
 We want to change that to the style:
 localhost/example1/calendar
 Note that there are no .php extensions any more?
Where to start?
 Download
 Example 1
 From:
 http://salimm.me/courses/summer-2014/cs1520/
 Or
 http://salimm.me/courses/summer-
2014/cs1520/example1.zip
First lets take a tour
 Example 1 code shown in the class
Calendar.php
 Calendar.php ues the date function to current date. The
arguments of the function indicates the format to report the
date. For example:
 date('l the jS') will return “Monday the 10th”
 l stands for day in letters,
 the stands for “the” and use  to escape these characters
 J stands for # of day in month and S stands for “th”
 date(“d”) returns day number
 date(“y”) returns 14 for 2014
 date(“Y”) returns year in complete form (2014)
 date(“d”) returns day of month with two leading zeros but date(“j”)
returns them without leading zeros
 date("F j, Y, g:i a"); returns May 19, 2014, 3:57 pm
 See the full doc here:
 http://php.net/manual/en/function.date.php
Birthday
 Change birthday.php in pages to accept the
inputs from user and print the following:
 Current Date (date only)
 Received birthday
 Calculate and print approximate age in years
Birthday
 Date
 Do not use the code in calendar.php. Retrieve year, month and
day individually using the date function and print the date
yourself. (HINT: you can use “Y”,”m”,”d”. The difference
between “Y” and “y” is that the later returns 14 for 2014.)
 In the second line Print the Birthday of user (Where can
you get that? Check the reserved variables $_GET,
$_SERVER, $_POST)
 In the third line print how old is the user in years
 You will need to cast the current and input year to integers to
be able to subtract them
 Sample output
 Today is 05/19/2014
 Your bithday is 5/3/2000
 You are 14 years old
Next we will create the
Router
 What do I we mean by a router?
 Routers work similar to a map. It receives the URL
user is requesting and allocates the
PHP|JSP|RUBY|etc file that is needed to process that
request
 Why?
 We will hide the structure of our code
 Simplicity: user doesn’t have to use odd long URL like
pages/birthday.php
 Security: We don’t want the hackers to know the structure of
our code
 Users don’t have to know the file extensions that we are
using
 Less confusing and more secure (we don’t want the hackers
to know the technology that we are using)
 Etc.
HTTP Protocol
 Before start with the code lets see what is
HTTP protocol:
 As we said last time: Is a protocol for
communication between server and client
 This communications are in form of requests and
responses
 (This is fairly an intuitive definition – refer to
teacher’s slides for the more adequate ones.)
Example
Request 1
Response 1
Request 2
Response 2
Request 3
Response 3
A sample request and
respone
Lets check the requests and
responses in our browser
 Open chrome
 Open Developer Tools
 View>Developer>Developer Tools
 Go to Network panel
A couple examples
 http://localhost/example1
 http://google.com
 http://localhost/example1/pages/birthday.php
 Submit the form
Request formats
 Request:
 GET
 Request to get a page
 HEAD
 Request to get only the response’s header
 POST
 Request to send data to the server (form)
 PUT
 Request to put a file or etc in server side (file or database)
 DELETE
 Delete a file from serverside
 Etc.
GET request in PHP
 How can we access the responses and
content of the requests?
 $_SERVER
 Include server info and request headers
 $_GET
 Includes al get data such as the data for the
birthday form
How to do it?
 Lets go back to our own example
 We want to create a router file which here will
be “index.php” that will receive all requests
and invoke the required php file for it?
 What do you think is the first step required here?
.htaccess
 We need to redirect everything to “index.php”
even if they are requesting something else.
 How?
 PHP can’t be used for that
 We use .htaccess file processed by Apache Server
Creating .htaccess file
 Create a .htaccess file in the root directory of
your project with the following content
# Turn rewriting on
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/example1/index.php
RewriteRule .* /example1/index.php
What does this do?
# Turn rewriting on
RewriteEngine On
Turns on the RewriteEngine
RewriteCond %{REQUEST_URI} !=/example1/index.php
We will rewrite every request except ”example1/index.php” since we
want to avoid recursive redirections
RewriteRule .* /example1/index.php
We will change everything to “/example1/index.php”
Creating the router
 Check $_SERVER varaible usingthe following
command and see the content
 print_r($_SERVER);
 How can you use these information to find
which request is sent to “index.php” and which
file to invoke for it?
REDIRECT_URL
 Use $_SERVER['REDIRECT_URL'] and check
which page has been requested
 Use conditions
 How to invoke the proper php file for the
request?
 Use include
Index.php format
 Check for the following requests:
 /example1/birthday
 /example1/calendar
 /example1/index.php or /example1/
 Note that index.php by default is the router now. You
need to have the content of the old index.php in file
like main.php and invoke main.php when ever user is
actually requesting index.php
 Otherwise
 Print : Erro: Invalid Reuqest!!!
Test your application
 DONE

Más contenido relacionado

La actualidad más candente

La actualidad más candente (6)

Robots and-sitemap - Version 1.0.1
Robots and-sitemap - Version 1.0.1Robots and-sitemap - Version 1.0.1
Robots and-sitemap - Version 1.0.1
 
Cocoon gem example
Cocoon gem exampleCocoon gem example
Cocoon gem example
 
Swift Meetup HH 2016/09
Swift Meetup HH 2016/09Swift Meetup HH 2016/09
Swift Meetup HH 2016/09
 
Unit 2.1 Part 4
Unit 2.1 Part 4Unit 2.1 Part 4
Unit 2.1 Part 4
 
Rail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendranRail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendran
 
External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQuery
 

Similar a CS1520 Session 2 - Simple Router

10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptMercyL2
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptGiyaShefin
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end appsZohar Arad
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questionssekar c
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxShitalGhotekar
 
PHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPPHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPLariya Minhaz
 
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010 Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010 Matt Gauger
 
Web develop in flask
Web develop in flaskWeb develop in flask
Web develop in flaskJim Yeh
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/tutorialsruby
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
Php Interview Questions
Php Interview QuestionsPhp Interview Questions
Php Interview QuestionsUmeshSingh159
 
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!
 

Similar a CS1520 Session 2 - Simple Router (20)

PHP
PHPPHP
PHP
 
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
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
Cqrs api v2
Cqrs api v2Cqrs api v2
Cqrs api v2
 
Day02 a pi.
Day02   a pi.Day02   a pi.
Day02 a pi.
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptx
 
PHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHPPHP Unit-1 Introduction to PHP
PHP Unit-1 Introduction to PHP
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010 Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
 
Web develop in flask
Web develop in flaskWeb develop in flask
Web develop in flask
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
Php Interview Questions
Php Interview QuestionsPhp Interview Questions
Php Interview Questions
 
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
 

Último

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

CS1520 Session 2 - Simple Router

  • 2. Goal  Work with dates in PHP  Handle a simple form  Get to know $_SERVER and $_GET variables more  Work with .htaccess files
  • 3. What are we doing?  We are creating a website that has two functionalities:  Calendar  Shows the current date  Birthday  Receives birthday  Prints it and tells you your age
  • 4. What else  Originally, the url of the pages are in the fomr:  localhost/example1/calendar.php  We want to change that to the style:  localhost/example1/calendar  Note that there are no .php extensions any more?
  • 5. Where to start?  Download  Example 1  From:  http://salimm.me/courses/summer-2014/cs1520/  Or  http://salimm.me/courses/summer- 2014/cs1520/example1.zip
  • 6. First lets take a tour  Example 1 code shown in the class
  • 7. Calendar.php  Calendar.php ues the date function to current date. The arguments of the function indicates the format to report the date. For example:  date('l the jS') will return “Monday the 10th”  l stands for day in letters,  the stands for “the” and use to escape these characters  J stands for # of day in month and S stands for “th”  date(“d”) returns day number  date(“y”) returns 14 for 2014  date(“Y”) returns year in complete form (2014)  date(“d”) returns day of month with two leading zeros but date(“j”) returns them without leading zeros  date("F j, Y, g:i a"); returns May 19, 2014, 3:57 pm  See the full doc here:  http://php.net/manual/en/function.date.php
  • 8. Birthday  Change birthday.php in pages to accept the inputs from user and print the following:  Current Date (date only)  Received birthday  Calculate and print approximate age in years
  • 9. Birthday  Date  Do not use the code in calendar.php. Retrieve year, month and day individually using the date function and print the date yourself. (HINT: you can use “Y”,”m”,”d”. The difference between “Y” and “y” is that the later returns 14 for 2014.)  In the second line Print the Birthday of user (Where can you get that? Check the reserved variables $_GET, $_SERVER, $_POST)  In the third line print how old is the user in years  You will need to cast the current and input year to integers to be able to subtract them  Sample output  Today is 05/19/2014  Your bithday is 5/3/2000  You are 14 years old
  • 10. Next we will create the Router  What do I we mean by a router?  Routers work similar to a map. It receives the URL user is requesting and allocates the PHP|JSP|RUBY|etc file that is needed to process that request  Why?  We will hide the structure of our code  Simplicity: user doesn’t have to use odd long URL like pages/birthday.php  Security: We don’t want the hackers to know the structure of our code  Users don’t have to know the file extensions that we are using  Less confusing and more secure (we don’t want the hackers to know the technology that we are using)  Etc.
  • 11. HTTP Protocol  Before start with the code lets see what is HTTP protocol:  As we said last time: Is a protocol for communication between server and client  This communications are in form of requests and responses  (This is fairly an intuitive definition – refer to teacher’s slides for the more adequate ones.)
  • 12. Example Request 1 Response 1 Request 2 Response 2 Request 3 Response 3
  • 13.
  • 14. A sample request and respone
  • 15. Lets check the requests and responses in our browser  Open chrome  Open Developer Tools  View>Developer>Developer Tools  Go to Network panel
  • 16. A couple examples  http://localhost/example1  http://google.com  http://localhost/example1/pages/birthday.php  Submit the form
  • 17. Request formats  Request:  GET  Request to get a page  HEAD  Request to get only the response’s header  POST  Request to send data to the server (form)  PUT  Request to put a file or etc in server side (file or database)  DELETE  Delete a file from serverside  Etc.
  • 18. GET request in PHP  How can we access the responses and content of the requests?  $_SERVER  Include server info and request headers  $_GET  Includes al get data such as the data for the birthday form
  • 19. How to do it?  Lets go back to our own example  We want to create a router file which here will be “index.php” that will receive all requests and invoke the required php file for it?  What do you think is the first step required here?
  • 20. .htaccess  We need to redirect everything to “index.php” even if they are requesting something else.  How?  PHP can’t be used for that  We use .htaccess file processed by Apache Server
  • 21. Creating .htaccess file  Create a .htaccess file in the root directory of your project with the following content # Turn rewriting on RewriteEngine On RewriteCond %{REQUEST_URI} !=/example1/index.php RewriteRule .* /example1/index.php
  • 22. What does this do? # Turn rewriting on RewriteEngine On Turns on the RewriteEngine RewriteCond %{REQUEST_URI} !=/example1/index.php We will rewrite every request except ”example1/index.php” since we want to avoid recursive redirections RewriteRule .* /example1/index.php We will change everything to “/example1/index.php”
  • 23. Creating the router  Check $_SERVER varaible usingthe following command and see the content  print_r($_SERVER);  How can you use these information to find which request is sent to “index.php” and which file to invoke for it?
  • 24. REDIRECT_URL  Use $_SERVER['REDIRECT_URL'] and check which page has been requested  Use conditions  How to invoke the proper php file for the request?  Use include
  • 25. Index.php format  Check for the following requests:  /example1/birthday  /example1/calendar  /example1/index.php or /example1/  Note that index.php by default is the router now. You need to have the content of the old index.php in file like main.php and invoke main.php when ever user is actually requesting index.php  Otherwise  Print : Erro: Invalid Reuqest!!!