I normally don't do it, but I am completely hopeless here.
I am trying to create a simple system to track leads from ads to a landing page. For this I want to save a cookie of the pages the client visited, but for some reason I can't set a cookie, even though the setcookie() function returns true.
I am pretty sure I am setting the cookie before sending any headers, and I also checked the problem on firefox, chrome and explorer and the results are the same.
Thanks in advance for anyone who will try to help.
<?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;
?>
您应该使用$ _COOKIE ['cid']而不是$ _cookie ['cid']。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.