PHP curl自动发送两个请求

时间:2016-04-07 11:48:50

标签: php curl

我使用PHP curl来保存用户刚刚点击的网站名称页面名称和我的数据库中的IP地址,但问题curl自动发送2个请求,其中一个正确数据,另一个只是将页面名称更改为索引。 php,我想中止第二个请求,意味着当curl执行一次然后没有请求curl直到另一个点击同一页面,希望我的描述将帮助你找到我的错误,这是我的代码。

**This IS My Curl Code**


$wb_nm = $_SERVER[HTTP_HOST];
$pg_nm = $_SERVER[PHP_SELF];
$usr_ip = $_SERVER[REMOTE_ADDR];
$pg_nm_ultr = trim($pg_nm , '/');

$n_url = "localhost/curl/new_page.php";

$data = array(
 "varhost" => $wb_nm,
 "varpage" => $pg_nm_ultr,
 "varip" => $usr_ip
);

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $n_url);

curl_setopt($post, CURLOPT_POSTFIELDS, $data);

curl_setopt($post, CURLOPT_RETURNTRANSFER, false);

$result = curl_exec($post);

curl_close($post);

?>



**And This Is new_page.php**



if(isset($_POST['varhost']) && isset($_POST['varpage']) && isset($_POST['varip']))
{
    $web_name = $_POST['varhost'];
    $page_name_ulter = $_POST['varpage'];
    $user_ip = $_POST['varip'];

    define("DB_HOSTNAME", "localhost");

    define("DB_USER", "root");

    define("DB_PASS", "");

    define("DB_DB", "pan");

    $connection = mysqli_connect(DB_HOSTNAME, DB_USER, DB_PASS, DB_DB);
    if(isset($connection)){
      $query = "SELECT * FROM tbl_agencies WHERE weblink = '".$web_name."'";
      $result  = mysqli_query($connection , $query);
      if($result){
        while($row = mysqli_fetch_array($result)){
          if(isset($row))
          {
           $D_id = $row['domain_id'];
           $D_name = $row['website'];
           if($D_id == 0)
           {
            return false;
        }
    }
    else
    {
    }
    break;
}
}
}

$date_date  = date('Y-m-d');
$time_stamp = date("H:i:s");

$query_Code = "INSERT INTO user_details (website_domain_id , website_domain_name , page_clicked , user_IP , access_date , access_time) VALUES ('".$D_id."','".$D_name."' , '".$page_name_ulter."' , '".$user_ip."' , '".$date_date."' , '".$time_stamp."')";

$result_Code  = mysqli_query($connection , $query_Code);
if($result_Code)
{
    mysqli_close($connection);

}
}
?>

1 个答案:

答案 0 :(得分:0)

curl_setopt($post, CURLOPT_POSTFIELDS, $fields);中,您可以将字段作为关联数组发送。所以这段代码:

$fields = '';

foreach($data as $key => $value) {
    $fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');

实际上不需要curl_setopt($post, CURLOPT_POSTFIELDS, $fields);成为

curl_setopt($post, CURLOPT_POSTFIELDS, $data);