1 / 13

More on Variables

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.

brinda
Download Presentation

More on Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Variables Some related techniques

  2. 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.

  3. 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 */

  4. Application Example • Calculator example: need to provide 2 operands and select an operator • What if one field is blank or no operator is selected?

  5. 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. 6-2 HTTP Environment Variables <?php phpinfo(); ?> Look for Apache Environment REMOTE_ADDR HTTP_USER_AGENT Use getnev() function

  7. 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."; ?>

  8. <?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 */ ?>

  9. 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"; } ?>

  10. 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"; } ?>

  11. 7-3 generic form + string functions • header() • md5(text1) • strlen(text1) • strrev(text1) • strtoupper(text1) • strtolower(text1) • ucwords(text1)

  12. 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

  13. 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; } ?>

More Related