【发布时间】:2016-07-06 14:15:52
【问题描述】:
我在 php 中编写了一个 API,由运行 google tag manager 的脚本调用。
脚本的用途是为每个访问者提供网站上唯一的电话号码
在测试期间,我通过添加带有 php 会话 ID 的 cookie 来管理用户跟踪。这让我可以查询数据库,看看他们是否已经分配了一个数字,如果他们没有,那么我需要分配一个。
在测试中这工作得很好,但是在 wordpress 平台中实现时,cookie 没有设置。因此,每次调用脚本(在页面更改时)我都会收到一个新号码。
方法一 在客户端设置 cookie 并使用 $_Cookie['MyCookie']
检索方法二 每次调用脚本时创建一个会话并检查会话 ID。会话 ID 每次都在更改...失败
方法三 尝试使用 ga 访问者 id(cid) 来跟踪用户,这也每次都会生成不同的数字
我已经尝试了多种解决方案来解决这个问题,但目前我无法阻止这种行为。
脚本当前配置为使用 GA CID,如下所示
<?php
header("Access-Control-Allow-Origin: *");
error_reporting(-1);
ini_set('display_errors', 'On');
$servername = "";
$username = "";
$password = "";
$dbname = "";
$cookie_name = "TestCookie";
$numID;
$site_id = "";
$domain_name = $_POST['url'];
if(isset($domain_name)) {
$conn_siteID = new mysqli($servername, $username, $password, $dbname);
$sql_siteID = "Select * from SiteList where ReferrerURL = '" . $domain_name . "'";
$result = $conn_siteID->query($sql_siteID);
if ($conn_siteID->connect_error) {
die("Connection failed: " . $conn_siteID->connect_error);
}
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$site_id = $row["id"];
}
} else {
}
$conn_siteID->close();
}
$cid = gaParseCookie();
echo $cid;
if(!empty($cid)) {
//Cookie doesnt exist give them a new number
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "Select * from NumPool where cid='". $cid . "' order by lastshown asc limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//return current number
echo $row["number"];
$numID = $row["id"];
$conn4 = new mysqli($servername, $username, $password, $dbname);
$sql = "Update NumPool set lastshown='" . date("Y-m-d H:i:s") . "', cid='". $cid ."' where id='" . $numID . "'";
$result = $conn4->query($sql);
$conn4->close();
}
} else {
//return new number
$conn3 = new mysqli($servername, $username, $password, $dbname);
if ($conn3->connect_error) {
die("Connection failed: " . $conn3->connect_error);
}
$sql = "Select * from NumPool where siteid='". $site_id . "' order by lastshown asc limit 1";
$result = $conn3->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["number"];
$numID = $row["id"];
$conn4 = new mysqli($servername, $username, $password, $dbname);
$sql = "Update NumPool set lastshown='" . date("Y-m-d H:i:s") . "', cid='". $cid ."' where id='" . $numID . "'";
$result = $conn4->query($sql);
$conn4->close();
}
} else {
}
$conn3->close();
}
$conn->close();
}
// Handle the parsing of the _ga cookie or setting it to a unique identifier
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version,$domainDepth, $cid1, $cid2) = preg_split('[\.]', $_COOKIE["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
}
else $cid = gaGenUUID();
return $cid;
}
// Generate UUID v4 function - needed to generate a CID when one isn't available
function gaGenUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
?>
【问题讨论】:
-
如果会话 id 在每次请求时都发生了变化,那么要么是您的会话 cookie 设置错误(丢失会话 cookie -> 每次都获取新的 + 空会话),要么是其他地方在搞乱会话姓名/身份证
-
你的函数
gaParseCookie()看起来总是返回一些东西。因此,当您使用if(!empty($cid) ) {检查值时,始终返回 true。看起来(从你的评论中)你给了他们一个新号码。 -
感谢你们 cmets 伙计们,不幸的是,他们都没有帮助我解决这个问题。我正在更改机智并直接在页面上注入脚本,而不是在服务器端管理它。完成后我会发布我的解决方案
标签: php wordpress session cookies