views:

44

answers:

1

Greeting Overflowers,

I am asked to code an email tracker using PHP. Email clients request my PHP script thinking it is an image (BODY's background). However, the client (namely Outlook 2007) hangs then (showing nothing) until my PHP script reaches its timeout.

<?php
define("DB_FILE", "sqlite:C:\wamp\www\database.sdb");
define("QUERY", "INSERT INTO Receipt (counter_id, reader_id, start_time, end_time) VALUES (%s, \"%s\", %d, %d)");
define("TIME_OUT", "10");

function track() {
    global $counter_id;
    global $reader_id;
    global $start_time;

    $end_time = time();

    $db = new PDO(DB_FILE);
    $db->exec(sprintf(QUERY, $counter_id, $reader_id, $start_time, $end_time));
}

$counter_id = $_GET["counter_id"];
$reader_id  = $_SERVER["REMOTE_ADDR"];
$start_time = time();

set_time_limit(TIME_OUT);
register_shutdown_function("track");

while(!connection_aborted()) {
    echo "\n";
    ob_flush();
}
?>

It is the loop which should keep the HTTP connection alive for the period of client's reading the tracked email. Any thoughts on how to solve this ?

Regards

+1  A: 

I'm afraid there's no sensible way of telling how long somebody has spent looking at an email. Mail clients simply aren't designed with that kind of data gathering in mind, and trying to force a network connection to stay open will merely cause the kind of problems you've run into.

Gordon