SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

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

Semelhante 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

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
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
 
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 Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Último (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
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
 
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 Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
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...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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