130 likes | 346 Views
More on Variables. Some related techniques. Header() function. void header ( string $string [, bool $replace = true [, int $ http_response_code ]] ) header() is used to send a raw HTTP header.
E N D
More on Variables Some related techniques
Header() function • void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) • header() is used to send a raw HTTP header. • Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Error in using header() • <html><?php/* This will give an error. Note the output * above, which is before the header() call */header('Location: http://www.example.com/');?> • /* Discuss the above URL */
Application Example • Calculator example: need to provide 2 operands and select an operator • What if one field is blank or no operator is selected?
6-1 Use header() to reload a file <?php /* Note: 1. For security reasons, do not use implicit variables in forms passed directly to the server. Use $_POST[xxx] instead. 2. All variables passed via $_POST should be in quotes, double or single 3. IF the textfields are empty, the server will reload the form */ if (($_POST["val1"] == "") || ($_POST["val2"] == "") || ($_POST["calc"] == "")) { header("Location: http://localhost/m06/6-1calculate_form.html"); exit; }
6-2 HTTP Environment Variables <?php phpinfo(); ?> Look for Apache Environment REMOTE_ADDR HTTP_USER_AGENT Use getnev() function
getenv() in 6-3remoteaddress.php <?php /* getenv() function * To get the REMOTE_ADDR value * REMOTE_ADDR environment variable contains the IP address * of the machine making the request. */ $address = getenv("REMOTE_ADDR"); echo "Your IP address is $address."; ?>
<?php $agent = getenv("HTTP_USER_AGENT"); echo " You are using $agent."; /* HTTP_USER_AGENT variable contains * the browser type * the browser version * language encoding * platform */ ?>
7-1 Detecting User’s Browser • M07/7-1browsermatch.php <?php $agent = getenv("HTTP_USER_AGENT"); if (preg_match("/MSIE/i", "$agent")) { $result = "You are using Microsoft Internet Explorer."; } else if (preg_match("/Firefox/i", "$agent")) { $result = "You are using Firefox."; } else if (preg_match("/Mozilla/i", "$agent")) { $result = "You are using Netscape."; } else { $result = "You are using $agent"; } ?>
7-2 Displaying Platform-Specific HTML <?php $agent = getenv("HTTP_USER_AGENT"); if (preg_match("/Win/i", "$agent")) { $style = "win"; } else if (preg_match("/Linux/i", "$agent")) { $style = "linux"; } ?> <?php if ($style == "win") { echo "$win_style"; } else if ($style == "linux") { echo "$linux_style"; } ?>
7-3 generic form + string functions • header() • md5(text1) • strlen(text1) • strrev(text1) • strtoupper(text1) • strtolower(text1) • ucwords(text1)
7-4 Redirecting to a New Location • Sending an HTTP header to the browser • Authentication, redirection, cookies etc. must be sent to the browser before anything else (including white space, line breaks, and any characters) • 7-4do_redirect.php
Code <?php if ($_POST['location'] == ""){ /* works with or without double quotes around location */ header("Location: 7-4redirect_form.html"); exit; } else { header("Location: $_POST[location]"); /* Here quotes are not allowed!! */ exit; } ?>