这是您需要做的:
1)在 MySQL 中创建一个包含这些字段的事务表:
TRANSACTIONS
############
id - INT, AUTO-INCREMENT, UNSIGNED, NOT NULL
transaction_id - VARCHAR(8)
created_on - TIMESTAMP, DEFAULT CURRENT_TIMESTAMP()
2) 在 MySQL 中创建一个包含这些字段的转换表:
CONVERSIONS
###########
id - INT, AUTO-INCREMENT, UNSIGNED, NOT NULL
transaction_id - VARCHAR(8)
created_on - TIMESTAMP, DEFAULT CURRENT_TIMESTAMP()
3) 在您的代码中,在将它们发送到 URL 之前,您需要生成并附加一个唯一的交易 ID(也称为子 ID):
/**
* Generates [$length] character transaction ID's. Verifies transaction ID is not
* currently in use. If it is, function recursively attempts to generate
* a unique one. After 10 attempts, it fails and returns false.
*
* @param PDO Database connection via dependency injection
* @param Int Length of string to generate
*/
function generateTransactionId($db, $length = 8)
{
// Don't let this function get stuck in endless recursion.
static $recursion_count = 0;
$recursion_count++;
if($recursion_count > 10)
{
$recursion_count = 0;
return false;
}
// Create transaction ID
$transaction_id = substr(md5(uniqid(mt_rand(), true)), 0, $length);
// Check to make sure it is unique
$query = "SELECT id FROM myDb.transactions WHERE transaction_id = ?";
$stmt = $db->prepare($query);
$stmt->execute(array($transaction_id));
$row = $stmt->fetch(PDO::FETCH_OBJ);
$stmt = null;
// If nothing found, we have a unique ID. Otherwise recursively find a unique one.
if(!is_object($row) || $row == false)
{
// Reset recursion counter
$recursion_count = 0;
// Found unique ID
return $transaction_id;
}
else
{
// Recursively generate unique transaction ID
return generateTransactionId($db);
}
}
$url = "http://www.google.com/";
$transaction_id = generateTransactionId($myPDOConnection);
// Add to URL
$url = $url . "?c3=" . $transaction_id;
// Do redirect to URL
header('Location: ' . $url);
4) 在另一个用于接收回发的脚本中,您需要通过 GET(或 POST,如果您愿意的话)获取 Sub ID/Transaction ID 并标记转换:
$transaction_id = isset($_GET['c3']) ? trim($_GET['c3']) : false;
if($transaction_id)
{
// Received conversion, add this transaction ID to conversions table
}
就实时比较而言,您可以访问的唯一指标是重定向之前的时间点,以及您收到回发的时间点。您可以通过检查事务表中的 COUNT() 来查看已完成的重定向次数。您可以通过查看转换表中的 COUNT() 来查看收到的回发/转换次数。