插入数据库时​​,跟踪像素会生成500(内部服务器错误)

时间:2017-01-08 18:42:26

标签: php mysql database tracking-pixel

我正在创建一个跟踪像素,并使用this answer作为起点。但是,在我尝试将数据插入数据库之前,一切正常。此时我得到500(内部服务器错误)。

这是我的代码:

<?php
$im=imagecreate(1,1);
$white=imagecolorallocate($im,255,255,255);

imagesetpixel($im,1,1,$white);
header("content-type:image/jpg");
imagejpeg($im);
imagedestroy($im);

$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = $_SERVER['REMOTE_ADDR'];

function getUserIP() {
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP)) {
        $ip = $client;
    } else if (filter_var($forward, FILTER_VALIDATE_IP)) {
        $ip = $forward; 
    } else { 
        $ip = $remote;
    }

    return $ip;
}

$user_ip = getUserIP();
$ip_integer = ip2long($user_ip);
$web_url = 'myurl.com';
$sqlWebLeads = "INSERT INTO ip_details (ip_address, web_url) 
    VALUES ('$ip_integer','$web_url')";
$wpdb->query($sqlWebLeads);
?>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

  

它在wordpress网站上运行,因此$wpdb->query是一个全局函数,通常不需要定义。

这不是魔术,所以,如果你创建一个独立的php文件,它将不会有wordpress全局定义的对象,作为你的$wpdb数据库链接。

您需要在页面中设置该对象才能使用它,请查看Using WPDB in standalone script?

  

$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

// $wpdb is available, do stuff

答案 1 :(得分:0)

问题(由@Blag正确指出)是$ wpdb-&gt;查询仅在wordpress框架内工作,即如果所有wordpress包含文件都被加载。所以我现在的解决方案是使用MySQLi函数创建一个新的独立数据库连接。我在下面发布了我的代码,供其他遇到相同问题的用户以及在wordpress中创建跟踪像素的工作代码。

感谢@Blag帮助我得到这个答案。

<?php
  $im=imagecreate(1,1);
  $white=imagecolorallocate($im,255,255,255);
  $transparent = imagecolortransparent($im,$white);

  imagesetpixel($im,1,1,$white);
  header("content-type:image/png");
  imagepng($im);
  imagedestroy($im);

    function getUserIP()
      {
          $client  = @$_SERVER['HTTP_CLIENT_IP'];
          $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
          $remote  = $_SERVER['REMOTE_ADDR'];

          if(filter_var($client, FILTER_VALIDATE_IP))
          { $ip = $client; }
          elseif(filter_var($forward, FILTER_VALIDATE_IP))
          { $ip = $forward; }
          else
          { $ip = $remote; }
          return $ip;
      }
      $user_ip = getUserIP();
      $ip_integer = ip2long($user_ip);
      $web_url = 'myurl.com';

      $servername = "XXXXXX";
      $username = "XXXXXX";
      $password = "XXXXXX";
      $dbname = "XXXXXX";

      $conn = new mysqli($servername, $username, $password, $dbname);
      if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

      $sql = "INSERT INTO ip_details (ip_address, web_url) VALUES ('$ip_integer','$web_url')";

      if ($conn->query($sql) === TRUE) { echo "New record created successfully";}
      else { echo "Error: " . $sql . "<br>" . $conn->error;}

      $conn->close();
?>