php-use the header() function-php

An HTTP (HyperText Transfer Protocol) header is used to send information back and forth between the server and the client (the Web browser). Normally this information is in the form of HTML, which is why the address for Web pages begins with http://.

But HTTP headers are a complicated enough subject to warrant a little more attention. There are actually dozens upon dozens of uses for HTTP headers, all of which you can take advantage of using PHP's header() function.. Here I'll demonstrate the most frequently used purpose—redirecting the user from one page to another—although the World Wide Web consortium's specifications on the subject are vast (Php header).

To redirect the user's browser with PHP, you would code:

header("Location: page.php");

You can also use the header function to send cookies, which is a good backup to the setcookie() function which sometimes has inconsistent results from one browser to the next.

header ("Set-cookie: name=value;
  expires=expiration");

The most important thing to understand about using header() is that it must be called before anything else is sent to the Web browser, just as you have to be careful when using the setcookie() function.

To demonstrate redirection, let's create a simple login script that sends a user to one page if they use the correct username and password or to another if they don't.

<?php

header ("Location: index.php");

exit;

?>
Related Posts:
  • $_ENV and $_SERVER ? PHP sets several variables for you containing information about the server, the environment, and your visitor's request. These are stored in the superglobal arrays $_ENV and $_SERVER, but their availability depends on whe… Read More
  • Setting Default Values for Function Parameters Assign the default value to the parameters inside the function prototype: function wrap_html_tag($string, $tag = 'b') { return "<$tag>$string</$tag>"; }     The example in the Solution sets the… Read More
  • magic_quotes_gpc, magic_quotes_runtime Magic quotes is the name of a PHP feature that automatically quotes inputdata, by using the addslashes() function. Historically, this was used so thatform data could be used directly in SQL queries without any security or qu… Read More
  • Web server compression? The best way to understand web server compression is to think of sending ZIP filesinstead of uncompressed files from your web server to your web user. Sending less dataover the network will minimize network latency and your … Read More
  • $_GET , $_POST,$_COOKIE?? $_GET contains any variables provided to a script through the GET method.  $_POST contains any variables provided to a script through the POST method.  $_COOKIE contains any variables provided to a script through a… Read More
  • Checking Variable Values and Types FUNCTION is_numeric() True if number or numeric stringctype_digit() True if all digits are numeric charactersis_bool() True if variable is a Booleanis_null() True if variable is NULLis_float() True if variable type is a fl o… Read More
  • How get the value of current session id? Session_id() returns the session id for the current session. … Read More
  • PHP Classes Classes do not use scope keywords, but you can prevent people from instantiating the class by makingthe __construct() method and the __clone() methods private or protected. The __construct()method is used to create the objec… Read More
  • Associative Arrays? You can use string values as keys. For example, you might create an arraylike this: $myStuff = array();$myStuff[“name”] = “andy”;$myStuff[“email”] = “andy@fsdsd.ca”;Print $myStuff[“name”]; Associative arrays are different t… Read More
  • How we know browser properties? echo $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(); foreach ($browser as $name => $value) { echo “$name $value \n”; } get_browser   returns the capabilities of the user's browser. … Read More
  • Reading DOC file in php   read PDF and DOC files using PHP Reading PDF Files   $content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');     Reading DOC Files  $content = shell_exec('/usr/local/bin/antiword '.$fi… Read More
  • Cookies Versus Sessions?  Cookies The setcookie( ) call needs to be before the HTML form because of the way the web works. HTTP operates by sending all "header" information before it sends "body" information. In the header, it sends t… Read More
  • What is triggers? A trigger is a database object which is associated with particular database table. Triggers gets called automatically when particular event(INSERT, UPDATE, DELETE) occurs on table. … Read More
  • What are the different tables present in mysql? Total 5 types of tables we can create 1. MyISAM 2. Heap 3. Merge 4. InnoDB 5. ISAM 6. BDB MyISAM is the default storage engine … Read More
  • PHP? PHP (Hyper text Pre Processor) is a scripting language commonly used for web applications … Read More