0

I used to work on a tracking pixel based upon this answer. Unfortunately I have a problem with loading the file which is the source in the image-tag. The pixel-tracker (pixel.php) works as I tried it by addressing the file-URL myself and it inserts data into the database.

Applied on an email, the file won't load. I tried this on two email-providers, Gmail and Yahoo. No response.

Then, because I thought it could be blocked or anything else - who knows - I put it on to my own website. No result. So I assume that the file doesn't get loaded into the image-tag. But how can I solve it?

The image-tag I use is quite simple:

<img src='http://www.mypage.com/data/pixel.php?a=value1&b=value2&c=value3' />

Thanks in advance for any help.

UPDATE: This is the code for the pixel.php-file. It's a bit weird I assume, but works so far if I address the file directly.

<?php
require 'config.php';
  // Create an image, 1x1 pixel in size
  $im=imagecreate(1,1);

  // Set the background colour
  $white=imagecolorallocate($im,255,255,255);

  // Allocate the background colour
  imagesetpixel($im,1,1,$white);

  // Set the image type
  header("content-type:image/jpg");

  // Create a JPEG file from the image
  imagejpeg($im);

  // Free memory associated with the image
  imagedestroy($im);
 $ip = $_SERVER['REMOTE_ADDR'];
  $useragent = $_SERVER['HTTP_USER_AGENT'];

    $sql1 = "INSERT INTO pixel (a, b, c)  
            VALUES ('$ip','$useragent','$c')";

    if ( $mysqli->query($sql1) ){

        //header("location: overview.php"); 
        echo "Registration successful!";
    }

    else {
        $_SESSION['message'] = 'Registration failed!';
        //header("location: error.php");
        echo "Registration failed";
    }    
?>

Add (for Allen):

My code so far.

<?php
    $content = file_get_contents('white_1x1.jpg');
    echo base64_encode($content);
    header("Content-Type: image/jpeg");
    echo base64_decode($content);
    //following PHPcode for Log to db
    require 'config.php';
    $ip = $_SERVER['REMOTE_ADDR'];
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    $sql1 = "INSERT INTO pixel (a, b, c) VALUES ('$ip','$useragent','$c')";
    if ( $mysqli->query($sql1) ){
        //header("location: overview.php"); 
        //echo "Registration successful!";
    }
    else {
        $_SESSION['message'] = 'Registration failed!';
        //header("location: error.php");
        //echo "Registration failed";
    }
?>
nucky
  • 348
  • 5
  • 15

1 Answers1

0

For your case:

  1. Prepare the 1 pixel image for PHP convert to a text
  2. Cache the image binary data into your PHP code

        <?php
            $content = file_get_contents('white_1x1.jpg');
            echo base64_encode($content);
        ?>
    
  3. Echo the binary data and log data into database

        <?php
            $content = 'a long string value from step 2';
            header("Content-Type: image/jpeg");
            echo base64_decode($content);
            //following PHPcode for Log to db
        ?>
    

Finally code:

<?php
    $content = '/9j/4AAQSkZJRgABAQEAwADAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9/KKKKAP/2Q==';
    header("Content-Type: image/jpeg");
    echo base64_decode($content);
    //following PHPcode for Log to db
    require 'config.php';
    $ip = $_SERVER['REMOTE_ADDR'];
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    $sql1 = "INSERT INTO pixel (a, b, c) VALUES ('$ip','$useragent','$c')";
    if ( $mysqli->query($sql1) ){
        //header("location: overview.php"); 
        echo "Registration successful!";
    }
    else {
        $_SESSION['message'] = 'Registration failed!';
        //header("location: error.php");
        echo "Registration failed";
    }
?>
Allen Chak
  • 1,802
  • 1
  • 10
  • 21
  • For better understanding please let me ask some questions. On step 1: by preparing you mean giving the file a name like 'white_1x1.jpg' in your reply and this by using: imagejpeg($im, 'white_1x1.jpg');, correct? Step 2 and 3 to insert between $imagejpeg and the $ip (and so on) till the database insert command? Sorry for asking so much, but try to understnad the logic and how to do. – nucky Jul 26 '17 at 12:16
  • One more thing: I found something out while trying. If I address the file directly on the server (writing src="dir/pixel.php" a.s.o. with no php) it works(!). But when addressing through the whole URL with http:// it doesn't work. – nucky Jul 26 '17 at 19:50
  • Aim, do NOT generate an image on the fly to scale down the server resource. For step 1, just use any software you liked, to create a JPEG image, and save it to `white_1x1.jpg`. Run the PHP, and get the result for next step. – Allen Chak Jul 27 '17 at 10:17
  • For Step 3, comment out step 2 code. Use step 2 result instead of on-the-fly generate the image. Append the db logging code after the `echo base64_decode($content);`. – Allen Chak Jul 27 '17 at 10:20
  • BTW, after this line `echo base64_decode($content);` , there are should not echo() / print() / header() – Allen Chak Jul 27 '17 at 10:30
  • A fantastic idea would be to echo out a transparent gif or png so that it doesn't interfere with the aesthetic of the page it appears on. – castis Jul 27 '17 at 17:04
  • 1. Allen, thanks for your help. I used to try your solution. I assume that I'd have made huge mistakes or I don't understand your solution. For the code I create a jpg-image (1px x 1px) with the file name above. The code I used is visible above. 2. I tried something else. Instead of an image-tag I tried an inframe-tag with source. This works. So I can/could use it, but am curious how to make the image-tag-tracking pixel work. – nucky Jul 27 '17 at 17:06
  • @castis this is possible. When trying this with iframe, it works well. However, I read some mail-providers may block it. Don't know if it is true. – nucky Jul 27 '17 at 20:18