0

我正在使用此脚本将引荐来源网址设置为 url,或者如果引荐来源网址为空或设置了 cookie,请点击图片。下面的代码是我到目前为止所拥有的,如果我将 curl 函数部分单独放在一个文件中,但我无法将它与 cookie 部分放在一起,则它可以工作。

错误是调用未定义的函数 geturl()

<?php
$image = 'image_url';


if($_COOKIE["6346"] == 1) {
$show = 0;
} 

else {
$show = 1;
$hours = rand(24,68);
setcookie('6346', 1, time()+(60*60*$hours));

}

if (!empty($_SERVER['HTTP_REFERER'])) {
$goodreferer = 0;
}
else {
$goodreferer = 1;
}

if ($show == 1 && $goodreferer == 0) {
echo geturl('url(dot)com', 'referer(dot)com');

function geturl($url, $referer) { 


    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg,text/html,application/xhtml+xml'; 
    $headers[] = 'Connection: Keep-Alive'; 
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
    $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 

    $process = curl_init($url); 
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($process, CURLOPT_HEADER, 0); 
    curl_setopt($process, CURLOPT_USERAGENT, $useragent);
    curl_setopt($process, CURLOPT_REFERER, $referer);
    curl_setopt($process, CURLOPT_TIMEOUT, 30); 
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 

    $return = curl_exec($process); 
    curl_close($process); 

    return $return; 

} 
}
else {
header('Location: ' . $image);

exit;
}

?>
4

3 回答 3

1

When a function is defined in a conditional manner its definition must be processed prior to being called.来自 PHP Manual Conditional functions

所以尝试geturl()在它被调用之前定义它。
也试试这个 SO 帖子Php - if 结构中的一个函数

于 2012-12-22T16:32:07.167 回答
0

不要在 if() 控制结构中定义你的函数。这肯定是造成混乱的秘诀。这就像多重继承——仅仅因为你可以并不意味着你应该!

我认为如果你使用良好的控制结构缩进,你会发现你的逻辑更容易理解,代码也更容易调试。此版本的脚本工作正常。 http://www.laprbass.com/RAY_temp_user1923808.php

<?php // RAY_temp_user1923808.php
error_reporting(E_ALL);

$image = 'image_url';

if( isset($_COOKIE["6346"]) && ( $_COOKIE["6346"] == 1) )
{
    $show = 0;
}
else
{
    $show = 1;
    $hours = rand(24,68);
    setcookie('6346', 1, time()+(60*60*$hours));
}

if (!empty($_SERVER['HTTP_REFERER']))
{
    $goodreferer = 0;
}
else
{
    $goodreferer = 1;
}

if ($show == 1 && $goodreferer == 0)
{
    echo geturl('url(dot)com', 'referer(dot)com');
}
else
{
    header('Location: ' . $image);
    exit;
}

function geturl($url, $referer) {


    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg,text/html,application/xhtml+xml';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';

    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERAGENT, $useragent);
    curl_setopt($process, CURLOPT_REFERER, $referer);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);

    $return = curl_exec($process);
    curl_close($process);

    return $return;
}
于 2012-12-22T16:59:13.550 回答
0

您在定义之前调用该函数。将函数移动到文件顶部。此外,您不应在 if 语句中定义函数。

于 2012-12-22T16:31:09.357 回答