php global function or procedure
-
03-06-2021 - |
Question
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
La solution
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
Autres conseils
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........!