hey,
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 cant 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 exploere and the results are the same.
thanks in advance for anyone who will try to help.
[code] <?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 “”;
}
else
{
echo "”;
}
$conn->close();
echo $cid." ".$redirectURL;
?>
[/code]