SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances (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
 

Similaire à CS1520 Session 2 - Simple Router

10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
GiyaShefin
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
Zohar Arad
 
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
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
tutorialsruby
 
<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-3
tutorialsruby
 

Similaire à 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
 

Dernier

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
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
vu2urc
 

Dernier (20)

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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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...
 
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
 
[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
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

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