我有一些php代码可以检查传入的url,我有数百个php页面,我必须在其中检查传入的url,我想使该代码像可以在所有php页面中调用的过程或函数。因此,我可以轻松地将我的代码效果应用于所有页面。这是我的 php 代码
<?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);
}
}
?>
现在我想为这段代码制定过程,它应该应用于我的所有文件
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{
请任何人帮助我将我的代码在 else 部分转换为过程以及如何在所有文件重新执行 else 部分中调用该过程
你可以在一个php文件中编写下面的代码,比如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;
}
?>
现在,将此php文件包含在启动的所有页面中,例如
<?php
include 'PATH_CHECK_DOMAIN';// give the above file path
?>
在你可以开始编码之后。..!
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;
}
}
在你的代码中,你会这样调用:
test_referer();
// domain verified, continue your code