-1

I have some php code which works to chek the incoming url and i have hundreds of php pages in which i have to chek the incoming url and i want to do make that code like a procedure or function which can be called in all php pages. So i can apply easily my code effect to all pages apparantly.Here is my php code

<?php
ob_start();
$domain = array("http://a.sml.com.pk/default.aspx","http://a.sml.com.pk/");
if(!in_array($_SERVER['HTTP_REFERER'],$domain))
{
$URL1="http://a.sml.com.pk"; 
header ("Location: $URL1");

}
else{

set_time_limit(500);
$url = 'http://appsrv01.shakarganj.com.pk:7778/reports/rwservlet?reptestsfpl&report=sales_milk';
$pdf = 'milksales.pdf';
$pdfbak = 'bak/'.$pdf;

if (filesize($pdf) > 10000)
{
copy($pdf,$pdfbak);
}

if ((int)time() > filemtime($pdf) + 30) 
{
file_put_contents($pdf, file_get_contents($url));
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/pdf');

if (filesize($pdf) > 10000)
{
readfile($pdf);
}
else
{
readfile($pdfbak);
}
}
?>

Now I want to make procedure for this code which should be applied in all my files

ob_start();
    $domain = array("http://a.sml.com.pk/default.aspx","http://a.sml.com.pk/");
    if(!in_array($_SERVER['HTTP_REFERER'],$domain))
    {
    $URL1="http://a.sml.com.pk"; 
    header ("Location: $URL1");

    }
    else{

Please any one help me to convert my code before else portion into procedure and how that procedure will be called in all file regrding else portion

Adeel Aslam
  • 1,285
  • 10
  • 36
  • 69

2 Answers2

2

you can write below code in one php file like domainCheker.php

<?php
    $domain = array("http://a.sml.com.pk/default.aspx","http://a.sml.com.pk/");
    if (!in_array($_SERVER['HTTP_REFERER'], $domain)) 
    {
        header ("Location: http://a.sml.com.pk");
        exit;
    }
?>

Now, include this php file in you all pages on starting, like

<?php
    include 'PATH_CHECK_DOMAIN';//  give the above file path 
?>

after you can start coding........!

Chintan
  • 1,204
  • 1
  • 8
  • 22
0
function test_referer()
{
    static $domain = array("http://a.sml.com.pk/default.aspx","http://a.sml.com.pk/");
    if (!in_array($_SERVER['HTTP_REFERER'], $domain)) {
        header ("Location: http://a.sml.com.pk");
        exit;
    }
}

In your code, you would call like:

test_referer();
// domain verified, continue your code
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Ok Sir But I have hundreds of php files in which i have to chek the domain so may i have to write this function in all files or how to call this function in files – Adeel Aslam Apr 26 '12 at 10:29
  • I would get: `The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.` as my referrer is always null – Lawrence Cherone Apr 26 '12 at 10:30
  • 1
    @user1353261 you can put the code inside `test_referer` into a separate .php file and then use .htaccess to always prepend it to all your scripts; see also http://www.codingforums.com/archive/index.php/t-78287.html – Ja͢ck Apr 26 '12 at 10:43
  • @LawrenceCherone you're right; the redirect URL could contain a retry counter that increases until a limit is reached, after which a custom error page could be shown :) – Ja͢ck Apr 26 '12 at 10:45