Reputation: 125
On Page Variable =
$lgc_code = '1,2,3,15,23,30';
Cookie = 'lgc_cntl'
My cookie is housing logic codes for each user to determine what banners or icons should show based on previous web pages visited. I have written the following function:
$lgc=$lgc_code;
// check for variable for multiple codes
$expEncArr = explode(",", $lgc);
$results = count($expEncArr);
if(isset($_COOKIE['lgc_cntl'])) {
// read cookie
$cookie_codes = $_COOKIE['lgc_cntl'];
// Build Array of cookie codes
$expEncArr2 = explode(",", $cookie_codes);
foreach($expEncArr as $l_code) {
if(in_array($l_code, $expEncArr2)) {
$lgc_codes=$_COOKIE['lgc_cntl'];
} else {
$lgc_codes=$_COOKIE['lgc_cntl'].','.$l_code;
} // end array search statement
// add campaign to cookie
setcookie('lgc_cntl',$lgc_codes,time() + (86400 * 365), "/", ".oru.edu"); // 86400 = 1 day
} // end foreach statement
} else {
$lgc_codes = $lgc;
// add campaign to cookie
setcookie('lgc_cntl',$lgc_codes,time() + (86400 * 365), "/", ".oru.edu"); // 86400 = 1 day
} // end isset(cookie) if / else statement
If there is no cookie set, the function works perfectly but if there is a cookie found it only adds the last variable in the array versus any codes not found in the cookie already.
The function should work as follows:
It is working all the way through but when the cookie is set, it is only adding the last variable in the string even if none of the others are not located in the cookie array. What am I doing wrong or how can I solve this?
Upvotes: 0
Views: 29
Reputation: 97718
This is a common mistake - $_COOKIE
is not affected by calling setcookie
. This is because $_COOKIE
is created at the beginning of the PHP script execution based on data sent from the browser, and setcookie
sends data to the browser.
Consequently, each time you run this line:
$lgc_codes=$_COOKIE['lgc_cntl'].','.$l_code;
Your value of $lgc_codes
is the cookie string originally sent by the browser plus exactly one extra code.
The solution is to instead use a local variable, such as $new_cookie_value
, to build up the full string, and then call setcookie
once:
$new_cookie_value = $_COOKIE['lgc_cntl'];
foreach($expEncArr as $l_code) {
// if statement removed to keep example brief
// Add extra code to list
$new_cookie_value = $new_cookie_value .','.$l_code;
}
// Now call setcookie once with the desired value
setcookie('lgc_cntl',$new_cookie_value,time() + (86400 * 365), "/", ".oru.edu");
[Aside: Do not underestimate the value of good variable names. Your code would be much clearer if $expEncArr
and $expEncArr2
had longer names which described what the difference was between them. Similarly, $lgc
, $lgc_code
, $lgc_codes
, etc.]
Upvotes: 1
Reputation: 24943
Your logic is very muddled. This is what should be in the first portion of your if
block
// read cookie
$cookie_codes = $_COOKIE['lgc_cntl'];
// Build Array of cookie codes
$expEncArr2 = explode(",", $cookie_codes);
$lgc_codes = $_COOKIE['lgc_cntl'];
foreach ($expEncArr as $l_code)
{
if (!in_array($l_code, $expEncArr2))
{
$lgc_codes .= ',' . $l_code;
}
}
setcookie('lgc_cntl', $lgc_codes, time() + (86400 * 365), "/", ".oru.edu");
Upvotes: 0