Thursday, November 7, 2013

Using Header function in PHP



Header function in PHP is quite powerful function. You have came across with such situations like: if some condition is false then the user should re-directed on the Index page of your website or may be after some seconds he should. Also Header function is useful while send the Content type of your webpage.

PHP: Re-directing to a webpage using Header Function.
<?php
   if(isset($_GET['id'])) {
        // Further Code..
   }else {
        header("Location: index.php");
   }
?>

PHP: Re-directing to a webpage after 10 seconds using Header Function.
<?php
   if(isset($_GET['id'])) {
       //Further code...
   }else {
       header("Refresh: 10; url=index.php");
   }
?>

PHP: Header Function to output a PDF file.
<?php
   header('Content-type: application/pdf');
?>

PHP: Header Function to convert PHP file into Text File.
<?php
   header('Content-Type: text/plain');
   echo "This is just a text file...we have converted a PHP file into a Plain Text File.";
?>

PHP: Header Function to convert PHP file into XML.
<?php
   header("Content-type: text/xml");
   echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
   echo "<person>";
   echo "<name>Ashwin Pathak</name>";
   echo "<age>14</age>";
   echo "<twitter>@TheCodePress</twitter>";
   echo "</person>";
?>

Their are many other header functions you'll discover it by searching.

Liked the post ? subscribe us with your email to get upcoming tutorials directly in your inbox:

No comments:

Post a Comment

Your Comment Will Be Visible After Approval, Thanks !