The examples below are for demonstration only. Add these techniques throughout the app wherever there are form variables.
BASIC ENHANCEMENTS:
ADVANCED ENHANCEMENTS:
IMPORTANT NOTE: Online forms can be a "gateway" for malicious attacks unless the data that are entered into them have valid format. However, unless you are collecting sensitive information (e.g., social security numbers, medical records, etc.), you do not have to worry about it as much. However, if it does, you need to seek advise from a security expert to avoid any security risks to your site.
Any admin page should have access to it restricted. To do this, we will add some PHP script to prevent these pages from being open without the proper credentials. If a user attempt to open an admin page directly, he or she should be taken to the login page to login first.
Once a connection has been opened, it is best practice to close the connection once a query has been made.
To make query more secure, it is best to use PDO extensions instead of my_sql and my_sqli extensions to avoid SQL injections. We have already done this a part of the basic steps. (Page 368)
Proper form validation is important to protect form from hackers and spammers!
The PHP $_GET and $_POST are superglobal variables that are used to collect data from a form. Superglobal variables are always accessible, regardless of scope which means that you can access them from any function, class, or file without having to do anything.
The array of variables (e.g., array(key1 =>value1, key2 =>value2, key3 =>value3) is a set of key/value or name/value pairs where the keys are form element names and values are user input data that is entered (e.g., input fields) or selected (e.g., combo box) by a user.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login Test</title>
</head> <body>
<form action="login_test.php" method="POST">
<label for =”name”>Name: </label>
<input type="text" name="name"><br>
<label for =”email”>Email: </label>
<input type="text" name="email"><br>
<input type="submit" value="Login"></form>
</body>
</html>
When a user fill out the form and click the Submit button, the form element (sometimes called form variables or form fields), is sent for processing to a PHP file named login_test.php using the HTTP POST method. To see the values of the form elements, you could use the echo command to display them anywhere on the page:
</form>NOTE: In Dreamweaver, notice that the POST variables are rendered as {Form.name} and {Form.email} and both of them are highlighted.
<p>Welcome: <?php echo $_POST["name"];?></p>
<p>Your email is: <?php echo $_POST["email"];?></p>
Welcome:
Notice: Undefined index: name in C:\xampp\htdocs\EmployeeDirectory_JQM_PDO\login_test.php on line 16
Your email is:
Notice: Undefined index: email in C:\xampp\htdocs\EmployeeDirectory_JQM_PDO\login_test.php on line 17
Welcome: Cornelius Chopin
Your email is: cornelius@richmediacs.com
When To Use POST?
The HTTP GET method could have just as easily been used.
<form action="login.php" method="GET">
Welcome: <?php echo $_GET["name"];?> <br/>NOTE: In Dreamweaver, notice that the GET variables are rendered as {URL.name} and {URL.email} and both of them are highlighted.
Your email is: <?php echo $_GET["email"];?>
Welcome: Cornelius Chopin
Your email is: cornelius@richmediacs.com
When To Use GET?Cross-Site Scripting (XSS) attach is a type of computer security vulnerability that enables attackers to INJECT client-side script into a web app.
<form method=”POST” action="login_test.php">
<form method=”POST” action="<?php echo $_SERVER['PHP_SELF'];?>">
CODE EXPLANATION:
- The $_SERVER['PHP_SELF'] is a super global variable that returns the filename of the currently executing script.
So it sends the submitted data to the page ITSELF instead of going to another page. This is typically done to
check to see if the form has been submitted and also to set error messages on the same page as the form.
This technique is sometimes referred to a POST BACK or SELF POSTING.
It can be done with (action=”<?php echo $_SERVER['PHP_SELF'];?>”) or without (action=”login.php”) the PHP_SELF variable.
- Notice the PHP_SELF is in SINGLE quotes because the attribute value is in DOUBLE quotes.
http://localhost/EmployeeDirectory_JQM_PDO/login_test.php
http://localhost/EmployeeDirectory_JQM_PDO/login_test.php/%22%3E%3Cscript%3Ealert('You have been hacked!!!')%3C/script%3E
<form method=”POST” action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>”>
CODE EXPLANATION:
- The htmlspecialchars() function converts special characters to HTML entities. This means it will replace HTML characters
like < and > with < and > to prevent attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in a form.
While we could add the htmlspecialchars() method on every input field, a more convenient way is to create a function that will handle ALL input fields for us. We will also include in this function the ability to:
</body> <?php
// define variables and set to empty values
$name = $email = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
}
?>
CODE EXPLANATION:
- The “if” statement is used to check it the form is using the POST method and if so run the test_input() method.
- Currently, all fields are optional.
- Note that while we only have two variables set to an empty string AND tied together with a series of equal signs,
you could set as many variables as you need based on the form elements on the page.
</form> <?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
?>
(e.g., Passwords in a separate table from employee data)
As we have already seen, normally, any HTML or PHP print() statement outside the PHP script is immediately sent to the browser. However, there are several methods (e.g., header(), setcookies()) that can only be called if nothing (including HTML or a blank space) has been sent to the browser; otherwise, a header already sent error message will be displayed. To avoid this error, you can implement what is called output buffering.
With output buffering any HTML or PHP print() statement will be placed in a memory buffer and then it can be sent to the browser or cleared without being sent to the browser if needed.
To begin output buffering, add a ob_start() method to the top of the script and an ob_end_flush() method at the end of the script to send the content of the buffer to the browser. You can also use ob_end_clean() method to delete the content of the buffer without sending it to the browser.
We will add the first two methods in the header.inc.php and footer.inc.php pages, respectively, so that is can be used by all pages within the site or app.
{ // Correct email and password! print "<p>You are logged in!</p>"; header("Location: index.php"); }- The highlighted code is used to redirect a user to another page. (In this case, index.php).
<?php ob_start();?> <!doctype html> <html>
</html> <?php ob_end_flush();?>- PHP automatically executes the ob_end_flush method at the end of a script if it does not exist; however, it is still good idea to use it.