PHP header()

header() function sends a raw HTTP header. header() must be called before any output of the web page. To other words, header() must be used before the <html> tag.
   void header(str string [, bool replace = true, int http_response_code])

There may be several HTTP headers of the same type in a web page. By default, the second HTTP header will replace the first. But if the replace parameter is false, then all HTTP headers will be kept.

<?PHP
	header("HTTP/1.0 404 Not Found");
	header("HTTP/1.0 301 Moved Permanently");
	header("HTTP/1.0 408 Request Time-out");
?>

header() is usually used for redirect a web page. e.g. following code will redirect to the "index.php" page, no more content after header() will be showed.

<?PHP
	header("LOCATION: index.php");
	header('cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: post-check=0, pre-check=0',false);	
?>

You may set up a specific wait time before the redirection. During the wait, content after header() will be showed.

<?PHP
	header("refresh:4; url=index.php");
	echo "Please wait, you will be redirect to a new page ...\n";
	...
?>

To use variables in the redirection:

<?PHP
	$url="index.php";	
	header("LOCATION: http://https://convert.idontcarewhatyouthink.net/$url");
?>

If you have "&" and variables in the redirection, the variables should be concatenated with the url string to avoid confusion:

<?PHP
	$p1="a";
	$p2="b";
	header('LOCATION: xxx.php?p1=".$p1."&p2=".$p2');
?>

header() can be used to forbidden using cache or proxy for the web page. This is important for some web pages that update frequently.

<?PHP
	header('cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: post-check=0, pre-check=0',false);
	header('Pragma: no-cache');
?>

header() can be used to download file. The following code will download current page.

<?PHP
	header("Content-Type:application/octet-stream");
	header("Content-Disposition: attachment;");
	header("Accept-ranges: bytes");
?>

For more information, visit HTTP Headers.

:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home