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

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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
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
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
[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
 

Último (20)

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...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
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
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
[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
 

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!!!