26
SIMPLE ROUTER Salim Malakouti

CS1520 Session 2 - Simple Router

Embed Size (px)

DESCRIPTION

CS1520 recitation class slides

Citation preview

Page 1: CS1520 Session 2 - Simple Router

SIMPLE ROUTER

Salim Malakouti

Page 2: CS1520 Session 2 - Simple Router

Goal

Work with dates in PHP Handle a simple form Get to know $_SERVER and $_GET

variables more Work with .htaccess files

Page 3: CS1520 Session 2 - Simple Router

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

Page 4: CS1520 Session 2 - Simple Router

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?

Page 5: CS1520 Session 2 - Simple Router

Where to start?

Download Example 1

From: http://salimm.me/courses/summer-2014/cs1520/ Or http://salimm.me/courses/summer-2014/cs1520

/example1.zip

Page 6: CS1520 Session 2 - Simple Router

First lets take a tour

Example 1 code shown in the class

Page 7: CS1520 Session 2 - Simple Router

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 \t\h\e jS') will return “Monday the 10th”

l stands for day in letters, \t\h\e 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

Page 8: CS1520 Session 2 - Simple Router

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

Page 9: CS1520 Session 2 - Simple Router

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

Page 10: CS1520 Session 2 - Simple Router

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.

Page 11: CS1520 Session 2 - Simple Router

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.)

Page 12: CS1520 Session 2 - Simple Router

Example

Request 1

Response 1

Request 2

Response 2

Request 3

Response 3

Page 13: CS1520 Session 2 - Simple Router
Page 14: CS1520 Session 2 - Simple Router

A sample request and respone

Page 15: CS1520 Session 2 - Simple Router

Lets check the requests and responses in our browser

Open chrome Open Developer Tools

View>Developer>Developer Tools Go to Network panel

Page 16: CS1520 Session 2 - Simple Router

A couple examples

http://localhost/example1 http://google.com http://localhost/

example1/pages/birthday.php Submit the form

Page 17: CS1520 Session 2 - Simple Router

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.

Page 18: CS1520 Session 2 - Simple Router

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

Page 19: CS1520 Session 2 - Simple Router

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?

Page 20: CS1520 Session 2 - Simple Router

.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

Page 21: CS1520 Session 2 - Simple Router

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

Page 22: CS1520 Session 2 - Simple Router

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”

Page 23: CS1520 Session 2 - Simple Router

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?

Page 24: CS1520 Session 2 - Simple Router

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

Page 25: CS1520 Session 2 - Simple Router

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

Page 26: CS1520 Session 2 - Simple Router

Test your application

DONE