KEMBAR78
Class 6 - PHP Web Programming | PPT
PHP Web Programming
Outline
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
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>
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 :
<?php
var_dump($_GET);
?>
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"
}
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>
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"
}
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 ).
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”.
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>
Exercise Solution
1. Script name “greet.php”:
<?php
echo “Hello “ . $_POST[‘name’];
?>
Handling file uploads
Uploading 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>
Handling file uploads
And 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.
Exercise
Work 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.
Exercise Solution
The 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>
Exercise Solution
The 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“;
?>
Cookies
Cookies 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
Cookies in PHP
PHP 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.
Cookies in PHP
The $_COOKIE super global is used to get the cookies set.
Example:
<?php
setcookie(‘name’, ‘mohamed’, time() + 3600 );
?>
In another script on the same domain we can do this :
<?php
echo $_COOKIE[‘name’]; // mohamed
?>
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:
<?php
session_start();
$_SESSION[‘age’] = 20;
?>
Sessions
Another script on the same domain contains:
<?php
session_start();
echo $_SESSION[‘age’] ; // 20
?>
Sessions
Things 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.
Exercise
Write 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.
Exercise Solution
<?php
session_start();
if( !isset($_SESSION['views']) )
$_SESSION['views'] = 0;
++$_SESSION['views'];
echo "Number of views : " . $_SESSION['views'];
?>
Assignment
Write 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
What's Next?
• Object Oriented Programming.
Questions?

Class 6 - PHP Web Programming

  • 1.
  • 2.
  • 3.
    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
  • 4.
    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>
  • 5.
    Getting Parameter Valuesin 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 : <?php var_dump($_GET); ?>
  • 6.
    Getting Parameter Valuesin 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" }
  • 7.
    Getting Parameter Valuesin 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>
  • 8.
    Getting Parameter Valuesin 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" }
  • 9.
    Getting Parameter Valuesin 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 ).
  • 10.
    Exercise Write a PHPscript 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”.
  • 11.
    Exercise Solution 1. Scriptname “form.php”: <html> <body> <form action=“/greet.php” method=“POST”> Text : <input type=“text” name=“name” /> <input type=“submit” value=“Submit” /> </form> </body> </html>
  • 12.
    Exercise Solution 1. Scriptname “greet.php”: <?php echo “Hello “ . $_POST[‘name’]; ?>
  • 13.
    Handling file uploads Uploadinga 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>
  • 14.
    Handling file uploads Andwe 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.
  • 15.
    Exercise Work on theprevious 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.
  • 16.
    Exercise Solution The firstscript 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>
  • 17.
    Exercise Solution The firstscript 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“; ?>
  • 18.
    Cookies Cookies are amechanism 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
  • 19.
    Cookies in PHP PHPprovides 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.
  • 20.
    Cookies in PHP The$_COOKIE super global is used to get the cookies set. Example: <?php setcookie(‘name’, ‘mohamed’, time() + 3600 ); ?> In another script on the same domain we can do this : <?php echo $_COOKIE[‘name’]; // mohamed ?>
  • 21.
    Sessions • Session supportin 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: <?php session_start(); $_SESSION[‘age’] = 20; ?>
  • 22.
    Sessions Another script onthe same domain contains: <?php session_start(); echo $_SESSION[‘age’] ; // 20 ?>
  • 23.
    Sessions Things to notehere : •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.
  • 24.
    Exercise Write a webpage 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.
  • 25.
    Exercise Solution <?php session_start(); if( !isset($_SESSION['views'])) $_SESSION['views'] = 0; ++$_SESSION['views']; echo "Number of views : " . $_SESSION['views']; ?>
  • 26.
    Assignment Write a webpage 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
  • 27.
    What's Next? • ObjectOriented Programming.
  • 28.