Tracking pixel generates 500 (Internal Server Error) when insert into database

318 Views Asked by At

I'm creating a tracking pixel and have used this answer as the starting point. However, it all works fine until I try to insert data into the database. At that point I get a 500 (Internal Server Error).

This is my code:

<?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);
?>

Any ideas?

2

There are 2 best solutions below

0
On

The issue (as correctly pointed out by @Blag) was the $wpdb->query only works within the wordpress framework, i.e. if all the wordpress include files are loaded. So my solution which now works was to create a new standalone database connection using MySQLi function. I've posted my code below for anyone else who comes across the same issue and also as working code for creating a tracking pixel in wordpress.

Thanks to @Blag for helping me get to this answer.

<?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();
?>
1
On

It's running on a wordpress site so $wpdb->query is a global function and does not usually need to be defined.

This is not magic, so, if you make a standalone php file, it'll not have wordpress global defined object, as your $wpdb DB link.

You need to set that object in your page to use it, look at 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