PHP Header() Function


PHP Header Function

The header() function has various uses. It can output headers for an xml file, force downloads and redirect pages to a new url. If it is used to redirect a url, there must not be any html or text of any sort.

Redirect With Header() Function

To redirect with header(),
1) Type: header("Location: filename.php");

PHP Force Downloading

   header("Content-Type: application/force-download");
   //the filename will be given below
   header("Content-Disposition:filename=\"$myfilename\"");
   
   header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($myfilename));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

CREATING XML File

header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>'.'<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>';

echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';