Dynamic cookie names?

I can’t, but then I have no knowledge of the modal pop-up / JS / JQ side of things. I only tested the cookies part - set the cookie with a suitable name using setcookie() in one file, redirected to another file and read the cookies using a line similar to your first one. It was really the naming side of it that I was looking at.

I don’t think you can make the cookie that way. I think @droopsnoot had it correct the first time. You cannot dynamically create a cookie nor a session that way. The cookie name has to be concatenated properly or you’ll get an error. I don’t recall which one, but I assume it’s the Unexpected error.

I did think that, but it seems that you can, it’s just the syntax is a little different between using setcookie() and accessing it from $_COOKIE. There’s an example (example #3, I don’t seem to be able to link directly to it) on the setcookie doc page, which is where I got the syntax in post #39 from.

I had two very small code snippets, the first was basically

$id = 1;
setcookie('special_offer[' . $id . ']', "Test" );
header("location: cookie2.php");

and the second was

$id = 1;
if (!isset($_COOKIE['special_offer'][$id])) { 
  $cset = "No";
}
else {
	$cset = "Yes";
}
echo $cset;

and I get “Yes”.

@droopsnoot & @spaceshiptrooper. It is working the following way:

<?php if (!isset($_COOKIE['special_offer'.$offerid]) && $promotion > 0):  
include_once "modals/offer_modal.php";?>
<script>
	$(window).scroll(function () { 
		if($(document).scrollTop() > $(document).height()/2){
			$('#offerModal').modal('show');
		} 
	});
</script>
<?php    
setcookie('special_offer'.$offerid, 'special_offer'.$offerid, time() + (86400 * 30), "/");
endif; ?>

Hmm, that’s very odd. I need to test this when I have time. But why is this required? I don’t think the OP should be getting into these sophisticated stuff. Sticking to traditional simple things should suffice.

Good to hear.

I started looking at arrays after I’d finally read back earlier in the thread (post #6) where there was talk of being able iterate dynamically through them, will you still be able to do that, or do you have a list of possible values that you can use to check instead?

I’m not sure i’m seeing the purpose of $offerid at all, given that offerModal is a singular ID, and offer_modal.php can only be included once, and the javascript uses the fixed ID?

What happens if your database returns multiple offer ID’s?

Why not handle all of this at the database/query end?

1 Like

Ok, I see what you are after now. But still, these kinds of methods are a bit over kill for a simple “if someone read this, then do this” kind of problem. I would think efficiency and timeliness matters.

@droopsnoot. There is always only one offer available:

  SELECT *
    FROM `offers`
   WHERE `isActive` = 1
ORDER BY `offer_id` DESC
   LIMIT 1

Then in the Controller:

$offer  	= $this->page->get_offer();
$offerid    = $offer['offer_id'];

I must admit I only skim-read the beginning of the thread, I was just looking at the code, to see what I could learn about cookies as much as anything else.

Danger Will Robinson! Danger!

Consider the example:
There are two active offers in the database, id 1 and 2.
User has seen Offer 1.
The user will NEVER see Offer 2, because your query will ONLY return offer 1 ever.

@m_hutley I just tried it first I dismissed the first offer after that I added a new offer to the database. I went to another page and the new offer was there! What I could do though is setting the active status of the former offer to 0 or even delete the entire offer

Sorry, got my numbers mixed up because you’ve got DESC in there.

New user. Blank your cookies.
Leave both offers in the DB.

You will never be served offer 1. Ever.

Your right about that, allthough most offers will be nearly the same something like:

Book now for 7, 8 or 9 days and get one day for FREE

But if you see another way to handle this, please let me know

17 posts were split to a new topic: Alternative to Dynamic Cookie Names