advanced
Dynamic Webpage Development
PHP Lecture: Ryan Clifford L. Perez 1
1) Forms Handling
2) Sessions
3) Include and Require
4) Cookies
advanced
5) Headers
PHP Lecture: Ryan Clifford L. Perez 2
Advanced
HTML forms (web form) – allows the user to enter
data that is sent to a web server for processing.
Form Handling
PHP Lecture: Ryan Clifford L. Perez 3
How do PHP handle this forms and collect the
Advanced data for processing? By using…
<?php
Form Handling $_GET[“ ”];
$_POST[“ ”];
$_REQUEST[“ ”];
?>
PHP Lecture: Ryan Clifford L. Perez 4
The $_GET[“ ”]; variable is an array of variable
names and values sent by the HTTP GET method.
Advanced Index.php
<body>
<form action=“welcome.php" method=“GET">
Form Handling Name: <input type="text" name="name" />
$_GET[“ ”]; <input type="submit" />
</form>
</body>
PHP Lecture: Ryan Clifford L. Perez 5
Index.php
<body>
<form action=“welcome.php" method=“GET">
Name: <input type="text" name=“yourname" />
Advanced <input type="submit" />
</form>
</body>
Form Handling welcome.php
$_GET[“ ”];
<body>
Welcome <?php echo $_GET[“yourname”]; ?>
</body>
PHP Lecture: Ryan Clifford L. Perez 6
Index.php
<body>
<form action=“welcome.php" method=“POST">
Name: <input type="text" name=“yourname" />
Advanced <input type="submit" />
</form>
</body>
Form Handling welcome.php
$_POST[“ ”];
<body>
Welcome <?php echo $_POST[“yourname”]; ?>
</body>
PHP Lecture: Ryan Clifford L. Perez 7
Index.php (either GET or POST method)
<body>
<form action=“welcome.php" method=“GET">
Name: <input type="text" name=“yourname" />
Advanced <input type="submit" />
</form>
</body>
Form Handling welcome.php
$_REQUEST[“ ”];
<body>
Welcome <?php echo $_REQUEST[“yourname”]; ?>
</body>
PHP Lecture: Ryan Clifford L. Perez 8
When you work with an application, you open it, do
Advanced
some changes, and then you close it. This is much
like a Session.
Sessions A session is a way to store information to be used
across multiple pages.
PHP Lecture: Ryan Clifford L. Perez 9
A session is started with the session_start();
Advanced function.
Session variables are set with the PHP global
Sessions variable: $_SESSION.
PHP Lecture: Ryan Clifford L. Perez 10
A session is started with the session_start() function.
index.php
<?php
SESSION_START();
Advanced ?>
<body>
<?php
$_SESSION[“ Fullname”] = “Ryan Clifford Perez”;
Opening Sessions $_SESSION[“ Gender”] = “Male”;
echo $_SESSION[“ Fullname”];
?>
</body>
PHP Lecture: Ryan Clifford L. Perez 11
Accessing the session information we set on the index.php
viewsession.php
<?php
SESSION_START();
Advanced ?>
<body>
<?php
echo $_SESSION[“ Fullname”];
View Sessions
echo $_SESSION[“ Gender”];
?>
</body>
PHP Lecture: Ryan Clifford L. Perez 12
Display all the session variable values for a user session.
viewallsession.php
<?php
Advanced
SESSION_START();
?>
<body>
View all Sessions <?php
print_r($_SESSION);
?>
</body>
PHP Lecture: Ryan Clifford L. Perez 13
To change a session variable, just overwrite it.
index.php
<?php
Advanced
SESSION_START();
?>
<body>
Modify Sessions <?php $_SESSION[“ Fullname”] = “Ryan Clifford Perez”; ?>
<?php $_SESSION[“ Fullname”] = “Ryan Clifford”; ?>
</body>
PHP Lecture: Ryan Clifford L. Perez 14
To remove all global session variables and destroy the
session, use session_unset() and session_destroy().
<?php
SESSION_START();
Advanced ?>
<body>
<?php
// remove all session variables
SESSION_UNSET();
Destroy Sessions // destroy the session
SESSION_DESTROY();
?>
</body>
PHP Lecture: Ryan Clifford L. Perez 15