在设置引用者脚本时调用未定义的函数

时间:2012-12-22 16:25:13

标签: php curl undefined

我使用此脚本将引荐来源设置为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;
}

?>

3 个答案:

答案 0 :(得分:1)

PHP手册 Conditional functions中的

When a function is defined in a conditional manner its definition must be processed prior to being called.

因此,在调用之前尝试定义geturl() 也请尝试这个SO帖子Php - a function inside an if structure

答案 1 :(得分:0)

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

答案 2 :(得分:0)

不要在if()控件结构中定义函数。这是一个混乱的确定方法。这就像多重继承 - 只是因为你并不意味着你应该![/ p>]

我认为如果你使用控制结构的良好缩进,你会发现你的逻辑更容易理解,代码更容易调试。此版本的脚本正常工作。 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;
}