Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
92 views22 pages

PSA5 Technical - PHP Web Forms and Form Validation

Uploaded by

Samuel Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views22 pages

PSA5 Technical - PHP Web Forms and Form Validation

Uploaded by

Samuel Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DEPARTMENT OF COMPUTER STUDIES

(Applications Development and Emerging


Technologies)

PRE-SUMMATIVE ASSESSMENT

5
USER INTERFACES, SESSION AND COOKIES

Student Name / Group


Name:

Name Role
Members (if Group):

Section:

Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE

● Design, implement and evaluate computer-based systems or applications to meet desired needs and

requirements.

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE

● Understand and apply best practices and standards in the development of website.

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:

● To apply the available super global variables for form processing and validation.

● To differentiate the use of $_GET, $_POST, and $_REQUEST super global variable in form

processing and know when to use it.

● To differentiate the use of Session and Cookies for form security of a Web Site.

● To know the proper syntax for validating user inputs using Regular Expression.

IV. BACKGROUND INFORMATION

Applications Development and Emerging Technologies Page 2 of 22


PHP Superglobal - $GLOBALS
Super global variables are built-in variables that are always available in all
scopes.
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is used to access global
variables from anywhere in the PHP script (also from within functions or
methods).
PHP stores all global variables in an array called $GLOBALS[index].
The index holds the name of the variable.
The example below shows how to use the super global variable $GLOBALS:
Example
<?php
$x = 75;
$y = 25;

function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

Applications Development and Emerging Technologies Page 3 of 22


addition();
echo $z;
?>

PHP Superglobal - $_POST


Super global variables are built-in variables that are always available in all
scopes.

PHP $_POST
PHP $_POST is a PHP super global variable which is used to collect form data
after submitting an HTML form with method="post". $_POST is also widely used
to pass variables.

The example below shows a form with an input field and a submit button. When
a user submits the data by clicking on "Submit", the form data is sent to the file
specified in the action attribute of the <form> tag. In this example, we point to
the file itself for processing form data. If you wish to use another PHP file to
process form data, replace that with the filename of your choice. Then, we can
use the super global variable $_POST to collect the value of the input field:

Example
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">


Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

Applications Development and Emerging Technologies Page 4 of 22


</body>
</html>

PHP Superglobal - $_GET


Super global variables are built-in variables that are always available in all
scopes.

PHP $_GET
PHP $_GET is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.

Assume we have an HTML page that contains a hyperlink with parameters:

<html>
<body>

<a href="test_get.php?subject=PHP&web=W3schools.com">Test $GET</a>

</body>
</html>

When a user clicks on the link "Test $GET", the parameters "subject" and "web"
are sent to "test_get.php", and you can then access their values in
"test_get.php" with $_GET.

The example below shows the code in "test_get.php":

Example
<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

Applications Development and Emerging Technologies Page 5 of 22


PHP Superglobal - $_REQUEST
Super global variables are built-in variables that are always available in all
scopes.

PHP $_REQUEST is a PHP super global variable which is used to collect data after
submitting an HTML form.

The example below shows a form with an input field and a submit button. When
a user submits the data by clicking on "Submit", the form data is sent to the file
specified in the action attribute of the <form> tag. In this example, we point to
this file itself for processing form data. If you wish to use another PHP file to
process form data, replace that with the filename of your choice. Then, we can
use the super global variable $_REQUEST to collect the value of the input field:

Example
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">


Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

</body>
</html>

PHP Cookies

Applications Development and Emerging Technologies Page 6 of 22


A cookie is often used to identify a user. A cookie is a small file that the server
embeds on the user's computer. Each time the same computer requests a page
with a browser, it will send the cookie too. With PHP, you can both create and
retrieve cookie values.

Create Cookies With PHP


A cookie is created with the setcookie() function.

Syntax
setcookie(name, value, expire, path, domain, secure, httponly);

Only the name parameter is required. All other parameters are optional.

PHP Create/Retrieve a Cookie


The following example creates a cookie named "user" with the value "John Doe".
The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie
is available in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie "user" (using the global variable
$_COOKIE). We also use the isset() function to find out if the cookie is set:

Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

Applications Development and Emerging Technologies Page 7 of 22


</body>
</html>

Note: The setcookie() function must appear BEFORE the <html> tag.

Note: The value of the cookie is automatically URLencoded when sending the
cookie, and automatically decoded when received (to prevent URLencoding,
use setrawcookie() instead).

Modify a Cookie Value


To modify a cookie, just set (again) the cookie using the setcookie() function:

Example
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the
past:

Example
<?php
// set the expiration date to one hour ago

Applications Development and Emerging Technologies Page 8 of 22


setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

Check if Cookies are Enabled


The following example creates a small script that checks whether cookies are
enabled. First, try to create a test cookie with the setcookie() function, then
count the $_COOKIE array variable:

Example
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>

</body>
</html>

Applications Development and Emerging Technologies Page 9 of 22


PHP Sessions
A session is a way to store information (in variables) to be used across
multiple pages.

Unlike a cookie, the information is not stored on the users computer.

What is a PHP Session?


When you work with an application, you open it, do some changes, and then you
close it. This is much like a Session. The computer knows who you are. It knows
when you start the application and when you end. But on the internet there is
one problem: the web server does not know who you are or what you do,
because the HTTP address doesn't maintain state.

Session variables solve this problem by storing user information to be used


across multiple pages (e.g. username, favorite color, etc). By default, session
variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available
to all pages in one application.

Applications Development and Emerging Technologies Page 10 of


22
Tip: If you need a permanent storage, you may want to store the data in
a database.

Start a PHP Session


A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

Now, let's create a new page called "demo_session1.php". In this page, we start
a new PHP session and set some session variables:

Example
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>

Note: The session_start() function must be the very first thing in your
document. Before any HTML tags.

Get PHP Session Variable Values


Next, we create another page called "demo_session2.php". From this page, we
will access the session information we set on the first page
("demo_session1.php").

Notice that session variables are not passed individually to each new page,
instead they are retrieved from the session we open at the beginning of each
page (session_start()).

Applications Development and Emerging Technologies Page 11 of


22
Also notice that all session variable values are stored in the global $_SESSION
variable:

Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>

Another way to show all the session variable values for a user session is to run
the following code:

Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>

Modify a PHP Session Variable


To change a session variable, just overwrite it:

Example
<?php
session_start();
?>

Applications Development and Emerging Technologies Page 12 of


22
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>

Destroy a PHP Session


To remove all global session variables and destroy the session,
use session_unset() and session_destroy():

Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>

Applications Development and Emerging Technologies Page 13 of


22
V. GRADING SYSTEM / RUBRIC (please see separate sheet)
VI. LABORATORY ACTIVITY
1. Create a personal information webpage. Using $_GET and $_POST get the
data (firstname, middlename, lastname, date of birth and address) from the
forms and display below.

After click the submit button:

Applications Development and Emerging Technologies Page 14 of


22
Note: you need to create 2 php webpages, 1 is for $_GET function and the
other 1 is for $_POST function.

2. Create a personal information webpage. Using setcookie() function, create


cookies for the firstname, middle name and lastname. It must be show the
cookies after 10, 20 and 30 seconds.

3. Create a webpage that will set a session for your 5 favorite colors and use it
to the other webpage. Integrate HTML and CSS, you can create your own
design and include images. Please see the example below:

FavoriteColor.php

Applications Development and Emerging Technologies Page 15 of


22
After click the button send colors:
ResultColors.php

Snip and paste your source codes here. Snip it directly from the IDE so that colors of the codes are
preserved for readability. Include additional pages if necessary.

VII. QUESTION AND ANSWER

1. What is the usage of super global?


2. What are the differences between $_POST and $_GET?
3. What is the importance of cookies on a webpage or website?
4. What is a session?
5. What is the importance of session?

VIII. REFERENCES

1. https://www.w3schools.com/css/
2. https://www.w3schools.com/html/
3. https://www.w3schools.com/php/php_variables.asp
4. https://www.w3schools.com/php/php_superglobals_globals.asp
5. https://www.w3schools.com/php/php_superglobals_post.asp
6. https://www.w3schools.com/php/php_superglobals_get.asp
7. https://www.w3schools.com/php/php_superglobals_request.asp
8. https://www.w3schools.com/php/php_cookies.asp
9. https://www.w3schools.com/php/php_sessions.asp

setcookie($cookie_name, $cookie_value, time() +


(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

Applications Development and Emerging Technologies Page 16 of


22
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Applications Development and Emerging Technologies Page 17 of


22
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

Applications Development and Emerging Technologies Page 18 of


22
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>

</body>
</html>

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>

<?php
session_start();
?>

Applications Development and Emerging Technologies Page 19 of


22
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>

Applications Development and Emerging Technologies Page 20 of


22
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>

Applications Development and Emerging Technologies Page 21 of


22
Note: The following rubrics/metrics will be used to grade students’ output.

Program (100 pts.) (Excellent) (Good) (Fair) (Poor)


Program execution Program executes Program executes Program executes Program does not
(20pts) correctly with no with less than 3 with more than 3 execute (10-
syntax or runtime errors (15-17pts) errors (12-14pts) 11pts)
errors (18-20pts)
Correct output Program displays Output has minor Output has Output is incorrect
(20pts) correct output errors (15-17pts) multiple errors (10-11pts)
with no errors (12-14pts)
(18-20pts)
Design of output Program displays Program displays Program does not Output is poorly
(10pts) more than minimally display the designed (5pts)
expected (10pts) expected output required output
(8-9pts) (6-7pts)
Design of logic Program is Program has Program has Program is
(20pts) logically well slight logic errors significant logic incorrect (10-
designed (18- that do no errors (3-5pts) 11pts)
20pts) significantly
affect the results
(15-17pts)
Standards Program code is Few inappropriate Several Program is poorly
(20pts) stylistically well design choices inappropriate written (10-11pts)
designed (18- (i.e. poor variable design choices
20pts) names, improper (i.e. poor variable
indentation) (15- names, improper
17pts) indentation) (12-
14pts)
Delivery The program was The program was The program was The program was
(10pts) delivered on time. delivered a day delivered two delivered more
(10pts) after the deadline. days after the than two days
(8-9pts) deadline. (6-7pts) after the deadline.
(5pts)

Applications Development and Emerging Technologies Page 22 of


22

You might also like