PHP header() ਫੰਕਸਨ

ਪਰਿਭਾਸ਼ਾ ਅਤੇ ਵਰਤੋਂ

header() ਫੰਕਸਨ ਕਲਾਈਮੈਂਟ ਨੂੰ ਆਧਾਰਿਤ ਹੈੱਡਰ ਭੇਜਦਾ ਹੈ।

ਇੱਕ ਅਹਿਮ ਗੱਲ ਮਹਿਸੂਸ ਕਰਨਾ ਹੈ ਕਿ ਹੈੱਡਰ() ਫੰਕਸਨ ਨੂੰ ਕਿਸੇ ਵੀ ਅਸਲ ਆਉਟਪੁਟ ਭੇਜਣ ਤੋਂ ਪਹਿਲਾਂ ਬੁਲਾਉਣਾ ਚਾਹੀਦਾ ਹੈ (PHP 4 ਅਤੇ ਉੱਚ ਦੀਆਂ ਸੰਸਕਰਣਾਂ ਵਿੱਚ ਤੁਸੀਂ ਆਉਟਪੁਟ ਕੈਂਸ਼ਨ ਦੀ ਵਰਤੋਂ ਕਰ ਸਕਦੇ ਹੋ)।

<html>
<?php
// ਨਤੀਜਾ ਗਲਤ ਹੈ
// ਹੈੱਡਰ() ਕਾਲ ਤੋਂ ਪਹਿਲਾਂ ਹੋਰ ਆਉਟਪੁਟ ਹੈ
header('Location: http://www.example.com/');
?>

ਸਿਧਾਂਤ

header(string,replace,http_response_code)
ਪੈਰਾਮੀਟਰ ਵਰਣਨ
string ਲਾਜ਼ਮੀ।ਭੇਜਣ ਵਾਲੇ ਹੈੱਡਰ ਸਟਰਿੰਗ ਨੂੰ ਨਿਰਧਾਰਿਤ ਕਰਦਾ ਹੈ।
replace

ਵਿਕਲਪਿਤ।ਇਹ ਇਹ ਸੁਝਾਵਦਾ ਹੈ ਕਿ ਹੈੱਡਰ ਪਹਿਲਾਂ ਦੇ ਹੈੱਡਰ ਨੂੰ ਬਦਲੇ ਜਾਵੇ ਜਾਂ ਦੂਜਾ ਹੈੱਡਰ ਜੋੜੇ ਜਾਵੇ।

ਮੂਲ ਰੂਪ ਵਿੱਚ ਹੈ (ਬਦਲਣਾ)।false (ਇੱਕ ਹੀ ਤਰ੍ਹਾਂ ਦੇ ਕਈ ਹੈੱਡਰ ਮਨਜ਼ੂਰ ਕਰਨਾ)।

http_response_code Optional. Force the HTTP response code to the specified value. (Available in PHP 4 and higher versions)}

Tips and Comments

Note:From PHP 4.4 onwards, this function prevents the sending of multiple headers at once. This is a protective measure against header injection attacks.

Example

Example 1

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");

?>
<html>
<body>
...
...

Note:Users may have set some options to change the browser's default cache settings. By sending the above headers, you can override any of these settings and force the browser not to cache!

Example 2

Prompt the user to save a generated PDF file (The Content-Disposition header is used to provide a recommended filename and force the browser to display a save dialog):

<?php
header("Content-type:application/pdf");
// The file will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// PDF source is in original.pdf
readfile("original.pdf");
?>
<html>
<body>
...
...

Note:Microsoft IE 5.5 has a bug that prevents the above mechanism. This bug can be resolved by upgrading to Service Pack 2 or a higher version.