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

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
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
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
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
 
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
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
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
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Último (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
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
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
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
 
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!
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
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
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

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