1

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?

Community
  • 1
  • 1
The Sumo
  • 627
  • 1
  • 8
  • 18
  • Are you using any proxy in your browser ? That might cause this issue. – Faizan Younus Jan 08 '17 at 19:07
  • I don't know anything about proxies. How would I know if I'm using a proxy? – The Sumo Jan 08 '17 at 19:41
  • How do you make your `$wpdb` ? if you use a flat file instead, like `file_put_contents('db.csv', $ip_integer.','.$web_url, FILE_APPEND);` do you still have the error 500 ? – Blag Jan 08 '17 at 21:26
  • @Blag can you explain what you mean? I'm using the same process and code for adding to the database as I do elsewhere on the website. No problems anywhere else. – The Sumo Jan 08 '17 at 21:57
  • I don't see code for `$wpdb`, if you run this file, it'll not connect anywhere; second point, a good practice is to avoid sending data/header before doing the work: your image part should be after the DB log – Blag Jan 08 '17 at 22:04
  • @Blag just tried your method and it writes the data to the csv file ok. And there is no 500 error. So it would appear that the issue is definitely with writing to the db. – The Sumo Jan 08 '17 at 22:04
  • It's running on a wordpress site so $wpdb->query is a global function and does not usually need to be defined. As I said, I use the same code to insert data into the database in other files on the same site with no issues. I will try creating the connection with mysqli but It's late here now so will have to do it tomorrow. Thanks for your help so far @Blag – The Sumo Jan 08 '17 at 22:10

2 Answers2

0

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
Community
  • 1
  • 1
Blag
  • 5,818
  • 2
  • 22
  • 45
  • Sorry @Blag this didn't work, I still get the 500 server error. However, also I don't think I want to include the whole wordpress library in a standalone file when most of it isn't needed. Creating a new MySQLi connection worked for me so I'll post my code as an answer because it it works as a tracking pixel solution for other wordpress users. – The Sumo Jan 09 '17 at 07:43
0

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();
?>
The Sumo
  • 627
  • 1
  • 8
  • 18