class 6 - php web programming

28
PHP Web Programming

Upload: ahmed-swilam

Post on 13-Jan-2017

667 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Class 6 - PHP Web Programming

PHP Web Programming

Page 2: Class 6 - PHP Web Programming

Outline

Page 3: Class 6 - PHP Web Programming

Request Types

1- ‘GET’ request:

Is the most common type of request. An example of it is writing the URL in your browser then clicking enter.

Get requests can have parameters passed with the URL.

http://www.google.com/search?q=ronaldo&client=ubuntuParametersDomain nameProtocol Script

Page 4: Class 6 - PHP Web Programming

Request Types

1- ‘POST’ request:

It is another type of request in which the browser passes the parameters to the server in a hidden way. Example of this is submitting a form with a method=post.

<form action=“/search” method=“POST”>

Text : <input type=“text” name=“query” /><input type=“submit” value=“Search” />

</form>

Page 5: Class 6 - PHP Web Programming

Getting Parameter Values in PHP

In PHP we have some special variables that store the parameters that are passed during the request.

1- $_GET

This is an associative array that holds the parameters that are passed in a GET request.For example:If I have a php script named “getit.php” on my local machine that looks like this :

<?phpvar_dump($_GET);

?>

Page 6: Class 6 - PHP Web Programming

Getting Parameter Values in PHP

When opening it like this :

http://localhost/getit.php?name=john&age=15

The out put should be like this :

array(2) { ["name"]=> string(4) "john" ["age"]=> string(2) "15"}

Page 7: Class 6 - PHP Web Programming

Getting Parameter Values in PHP

2- $_POST :

This is an associative array that contains all the passed parameters using POST request.

For example:

If we have page called “form.php” that contains the following :

<form action=“/postit.php” method=“POST”>Text : <input type=“text” name=“query” /><input type=“submit” value=“Search” />

</form>

Page 8: Class 6 - PHP Web Programming

Getting Parameter Values in PHP

And the script “postit.php” has :<?php

var_dump($_POST);?>

Opening that script :http://localhost/form.php

After opening that and putting a value “Hello” in the text box and clicking on the search button, you should see :

array(1) { ["query"]=> string(5) "Hello"}

Page 9: Class 6 - PHP Web Programming

Getting Parameter Values in PHP

3- $_REQUEST:

This variable will contain all the values from the associative arrays $_GET and $_POST combined ( It also contains the values of the variable $_COOKIE which will talk about later).

It is used in case I allow the user to pass parameters with the method he likes ( GET or POST ).

Page 10: Class 6 - PHP Web Programming

Exercise

Write a PHP script that allows the user to enter his/her name in a field and after submitting, It should greet them.

For example:

if the user entered “Mohamed” the script should show “Hello Mohamed”.

Page 11: Class 6 - PHP Web Programming

Exercise Solution

1. Script name “form.php”:

<html><body>

<form action=“/greet.php” method=“POST”>

Text : <input type=“text” name=“name” />

<input type=“submit” value=“Submit” />

</form></body>

</html>

Page 12: Class 6 - PHP Web Programming

Exercise Solution

1. Script name “greet.php”:

<?php

echo “Hello “ . $_POST[‘name’];

?>

Page 13: Class 6 - PHP Web Programming

Handling file uploadsUploading a file to a PHP script is easy, you just need to set the enctype value to “multipart/form-data” and the form method=POST.

The multi-dimension array called “$_FILES” will contain any information related to the uploaded files.To demonstrate, here is an example :We have a script named “form.php” that contains :

<form enctype="multipart/form-data“ action=“upload.php" method="POST">File: <input name="uploadedfile" type="file" /><input type="submit" value="Upload" />

</form>

Page 14: Class 6 - PHP Web Programming

Handling file uploadsAnd we have another script named “upload.php” :

<?php

if( is_uploaded_file($_FILES['uploadedfile']['tmp_name']) ){echo file_get_contents($_FILES['uploadedfile']['tmp_name']);

}

?>

This script will show the contents of the file once uploaded.

Page 15: Class 6 - PHP Web Programming

ExerciseWork on the previous exercise to allow the user to enter an email address and check whether the email is valid or not and show a message to the user telling so.

Page 16: Class 6 - PHP Web Programming

Exercise SolutionThe first script is named “form.php” :

<html><body>

<form action=“/doit.php” method=“POST”>

Name: <input type=“text” name=“name” />E-mail: <input type=“text” name=“email” />

<input type=“submit” value=“Submit” />

</form></body>

</html>

Page 17: Class 6 - PHP Web Programming

Exercise SolutionThe first script is named “doit.php” :

<?php

echo "Your name : ". $_POST['name'] . "<br/>";echo "Your email: ";

if( preg_match( '/^[\w]+@[\w]+\.[a-z]{2,3}$/i', $_POST['email']) == 1 )

echo $_POST['email'];else

echo "Not Valid“;

?>

Page 18: Class 6 - PHP Web Programming

CookiesCookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users.

• Example of a cookie response header :Set-Cookie: name2=value2; Domain=.foo.com; Path=/;Expires=Wed, 09 Jun 2021 10:18:14 GMT

• Example of a cookie request header :Cookie: name=value; name2=value2

Flash Back – Class 1

Page 19: Class 6 - PHP Web Programming

Cookies in PHPPHP provides the function setcookie() and the global variable $_COOKIE to allow us to deal with cookies.

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script.

Page 20: Class 6 - PHP Web Programming

Cookies in PHPThe $_COOKIE super global is used to get the cookies set.

Example:

<?phpsetcookie(‘name’, ‘mohamed’, time() + 3600 );

?>

In another script on the same domain we can do this :

<?phpecho $_COOKIE[‘name’]; // mohamed

?>

Page 21: Class 6 - PHP Web Programming

Sessions• Session support in PHP consists of a way to preserve

certain data across subsequent accesses. • This is implemented by creating a cookie with a random

number for the user and associate this data with that id.• PHP maintains a list of the user ids on the server with

corresponding user data.

Example:

<?phpsession_start();$_SESSION[‘age’] = 20;

?>

Page 22: Class 6 - PHP Web Programming

SessionsAnother script on the same domain contains:

<?php

session_start();

echo $_SESSION[‘age’] ; // 20

?>

Page 23: Class 6 - PHP Web Programming

SessionsThings to note here :

•The session_start() function should be called the first thing in the script before any output in order to deal with sessions.•Use session_destroy() if you want to destroy the session data.

Page 24: Class 6 - PHP Web Programming

ExerciseWrite a web page that uses sessions to keep track of how many times a user has viewed the page. The first time a particular user looks at the page, it should print something like "Number of views: 1." The second time the user looks at the page, it should print "Number of views: 2," and so on.

Page 25: Class 6 - PHP Web Programming

Exercise Solution<?php

session_start();

if( !isset($_SESSION['views']) )$_SESSION['views'] = 0;

++$_SESSION['views'];

echo "Number of views : " . $_SESSION['views'];?>

Page 26: Class 6 - PHP Web Programming

AssignmentWrite a web page that uses sessions to keep track of how long the user viewed the page. It should output something like “You have been here for X seconds“. Tip use date function http://php.net/manual/en/function.date.php

Page 27: Class 6 - PHP Web Programming

What's Next?• Object Oriented Programming.

Page 28: Class 6 - PHP Web Programming

Questions?