Photo credits: Andrew E. Larson and John Seb Barber cc
We're going to practice retrieving data from requests to gather information from your users. We'll need a few more files.Download a copy of today's practice files and unzip them directly into your gdi folder.
Photo credit: Rym DeCoster cc
You use URL parameters to send information via GET request. URL parameters go at the end of a URL. Parameters use the form ?var1=val1&var2=val2&.... For example, you could send a first and last name like this:
http://example.com/index.php?firstname=ada&lastname=lovelace
Remember global and local variables? PHP has a special class of variables called superglobals. Superglobals are built-in variables that are always available in all scopes. $_GET is a superglobal that contains all the information from a GET request.
To retrieve the first name and last name values from this url:
http://example.com/index.php?firstname=ada&lastname=lovelace
You can use $_GET
$firstName = htmlspecialchars($_GET["firstname"]);
$lastName = htmlspecialchars($_GET["lastname"]);
Using $_GET["urlparameter"] retrieves the value from the $_GET superglobal. The function htmlspecialchars is a built-in function to help clean up user data.
Let's practice using $_GET.
A simple HTML form may look something like this:
<form action="myform.php" method="post">
<label for="question">A question?</label>
<input type="text" name="question" maxlength="50">
<input type="submit" id="formSubmit" value="Submit">
</form>
The action attribute tells the browser where to send the form. The method attribute tells the browser what type of HTTP request to use. In this case, when the user selects the submit button, the browser will send the form data to myform.php in a POST request.
$_POST is a superglobal variable, just like $_GET. When a user submits a form, the $_POST variable will store information based on the name (not id!) of the HTML form field. So to retrieve data from this field:
<input type="text" name="question">
You would use this code:
$question = htmlspecialchars($_POST["question"]);
Using $_POST["fieldname"] retrieves the value from the $_POST superglobal.
There are two common ways to submit a form with PHP:
If you are submitting a form to the same PHP file, you can use an if statement to wrap your form-processing script:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//Process the form data and display results
}
Using a sample form we'll process some user-submitted data. This is a big task. Work piece by piece and test as you go.
Photo credit: Theo Crazzolara cc
PHP has built in features to set and read cookies. This uses a special superglobal, $_COOKIE. When we add vaues to this superglobal, PHP will also send the cookie to the user's computer.
The basic values used to set a cookie are:
The basic syntax is:
setcookie(name, value, expire, path)
For example:
$cookie_name = "username";
$cookie_value = "Grace Hopper";
$cookie_time = time() + (86400 * 30); /*The current time, plus 30 days.
The time stamp is in seconds, and there are 86400 seconds in one day*/
setcookie($cookie_name, $cookie_value, $cookie_time, "/");
We can use the isset() function to check to see if cookies exist. If they do, we can retrieve the information from the superglobal.
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];
}
Set a cookie on one of your pages. Then see if you can retrieve the value on a different page.