Problem with setting a cookie

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]

This doesn’t look quite right, just use $cookieCounter

then

foreach($_cookie['cid'] as $cookieCid)

Looks a bit odd as you will only have one cookie named ‘cid’, I would think that the foreach would fail as you are feeding it a string, or is it really an array??

Just check if $_cookie[‘cid’] == $cid

Also put this at the top of the page for testing,

echo'<pre>'; print_r($_COOKIE); echo '</pre>';

This will tell you what cookie values you have.

thank you so much for your reply.
i used the cid[$cookieCounter] because i really plan to have an array that will contain various campaigns that all lead to the same landing page.
but maybe the syntax of creating a cookie array is wrong.

i did add the echo for the variables at the top, but i read somewhere that cookies must be set before sending any output to the page.

try this syntax.

setcookie ('order_id', $order_id, time()+2678400, "/");

Do this on the first page then next page can get that value

If you need this on the first page, use $_SESSION[‘order_id’] = $order_id
Please use session_start(); at the very beginning of the script

Top line of code possibly a problem?

 <?php

space before the opening PHP header, or is that just on the forum?

no, the space is only in the forums example, but thanks anyway :slight_smile:

i just wanted to update you guys, that i fixed it, turns out it was a upper lower case issue- instead of writing $_cookie(…) i should have used $_COOKIE(…)

thanks for those that tried to help :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.