我通常不会这样做,但我在这里完全没有希望。
我正在尝试创建一个简单的系统来跟踪从广告到目标网页的潜在客户。为此,我想保存客户端访问过的页面的cookie,但由于某种原因,即使setcookie()函数返回true,我也无法设置cookie。
我很确定我在发送任何标头之前设置了cookie,而且我还检查了firefox,chrome和explorer上的问题,结果是一样的。
提前感谢任何想要提供帮助的人。
<?php
//save the received user information to local variables:
$cid = $_GET['campaignId'];
$redirectURL = $_GET['url'];
//save the click in a database:
$servername = *********
$username = ******
$password = ******
$dbname = *******
//check by cookie if its the first visit, if it is set cookie to the current cid (campaign id).
$cookieCounter=0;
$first=true;
if(isset($_cookie['cid']))
{
foreach($_cookie['cid'] as $cookieCid)
{
if($cookieCid == $cid)
{
$first=false;//returning visitor
$cookieCounter++;//used for cookie array reference later on.
break;
}
}
}
if($first)
{
/*if its a first time visit to the current campaign:
create a new cookie log
update DB*/
//cookie update:
if(setcookie("cid[$cookieCounter]",$cid,time()+60*60*24*30*2))
//set expiration date for 2 months
//database UPDATE
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//update user visit:
$sql = "UPDATE campaign SET uniqueVisitors = uniqueVisitors + 1 WHERE cid = $cid";
$result = $conn->query($sql);
if ($result)
{
echo "<script>window.alert(\"Success! Update Unique Visitors\")</script>";
}
else
{
echo "<script>alert(\"Sorry, something went wrong :( could not update unique visitors ".mysqli_connect_error()."end of report\")</script>";
}
$conn->close();
}
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//update user visit:
$sql = "UPDATE campaign SET visitors = visitors + 1 WHERE cid = $cid";
$result = $conn->query($sql);
if ($result)
{
//echo "<script>window.alert(\"Success!\")</script>";
}
else
{
echo "<script>alert(\"Sorry, something went wrong :( ".mysqli_connect_error()."end of report\")</script>";
}
$conn->close();
echo $cid." ".$redirectURL;
?>
答案 0 :(得分:0)
你应该使用$ _COOKIE ['cid']而不是$ _cookie ['cid']。