Loading...
PHP

Header

It is a small amount of data packet used to transfer some data between browser and server. Along with form data if you want to send extra information between browser and server we can go for header
Headers are mainly divided into two types those are
1. Request Header
2. Response Header

  1. Request Header: Data transmission between browser and server is called as request header.
  2. Response Header: Data transmission between server and browser is called as response header .

In PHP we use header functions to declare headers in script. Different types of headers are available those are location, content-type, content-disposition. By using these headers we can redirect from one page to another page.

<?php
if(isset($_POST[‘sub’])){
if($_POST[‘sub’]== ‘page1)
header(“location:page1.php”);
else
header(“location:page2.php”);
}
?>|
<form method= “post” action=” “>
<input type= ‘submit’ name= ‘sub’ value= ‘page1’>
<input type= ‘submit’ name= ‘sub’ value= ‘page2’>
</form>

Content-disposition: By using this header we can display download dialog box on browser , download dialog box is used to save a file a file in client system. File is a property used to specify a file name in download dialog box.
<?php
header(“content_disposition:attachment:filename= abc.txt”);
readfile(“abc.txt”);
?>

While pressing link to download the file:
<a herf= “pro1.php? fn= abcd.txt”> Download1 </a>
<a herf= “pro1.php? fn=cherry.jpg”> Download2 </a>
<a herf= “pro1.php? fn=wordpress.2”> Download3 </a>
<?php
$qs=$_REQUEST[‘fn’];
$size=filesize($qs);
header(“content_disposition: attachment :filename= $qs”);
header(“content_length:$size”);
readfile($qs);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *