Hi,

I want my cookie was changing like that:
1,2,3,4,1,2,3,4,1,....
after each time I refresh the page.

but it is changing somehow randomly.

if (!isset($_COOKIE["bbb"]) && empty($_COOKIE["bbb"])) setcookie("bbb", "1", time()+3600);
	else {

		switch ($_COOKIE['bbb']) 
		{
			case 1: 
					 setcookie("bbb", "2", time()+3600);
					 break;
			case 2: 
					 setcookie("bbb", "3", time()+3600);
					 break;	
			case 3: 
					 setcookie("bbb", "4", time()+3600);
					 break;	
			case 4: 
					 setcookie("bbb", "1", time()+3600);
					 break;	
			default: 
					 setcookie("bbb", "1", time()+3600);
	 
		
		}
	}

Recommended Answers

All 7 Replies

Before you try to change the value of the cookie, try setting the cookie to nothing, then setting the cookie.

something like this....

[B]setcookie("bbb", "", time()-3600);[/B]

switch ($_COOKIE['bbb']) 
{
        case 1:
                setcookie("bbb", "2", time()+3600);
                break;
        case 2:
                setcookie("bbb", "3", time()+3600);
                break;
        case 3:
                setcookie("bbb", "4", time()+3600);
                break;
        case 4:
                setcookie("bbb", "1", time()+3600);
                break;
        default: 
                setcookie("bbb", "1", time()+3600);  		
}

That is weird. Indeed my code works, but on the desperate page. When I am putting this code into the file which I include into another file (complex website), cookies go crazy. It looks like it executes code with cookies few times (at least twice) - I display the value and see after each time I refresh the page: 2,1,4,2,1,3
LOL

That is weird. Indeed my code works, but on the desperate page. When I am putting this code into the file which I include into another file (complex website), cookies go crazy. It looks like it executes code with cookies few times (at least twice) - I display the value and see after each time I refresh the page: 2,1,4,2,1,3
LOL

In my first post, I thought you only wanted one value in the cookie at a time. (sorry about that)

Could it be that your file to set the cookie is being called multiple times before the page finishes loading and you write the value out? Try writing out a value to the page from within your case statement. This way you can see just how many times your case statement is being called during the page load.

case 1:
                setcookie("bbb", "2", time()+3600);
[I]echo cookieValue[/I]
                break;
        case 2:
                setcookie("bbb", "3", time()+3600);
[I]echo cookieValue[/I]
                break;

I am going crazy :), I dont know what is going on. I tried to do similar thing with session and the result of a code bellow is: 51,54,57,60.
WTF?

// Start sessions before any output
session_start();

// Check if there is an active session; if not, default to 1
if( !isset($_SESSION['banner_rotate_num']) )
{
    // Set new (1)
    $_SESSION['banner_rotate_num'] = 1;
}

else
{
    // Otherwise set to next number
    $_SESSION['banner_rotate_num']++;
}

echo  $_SESSION['banner_rotate_num'];

definitely pages are somehow reloaded 3 times before there are displayed. I have no idea how come, no idea... :/

hmm... I think I see the problem. somehow Sessions are kind of Global, so it is changing anytime somebody is going to the website.

LOL. I am using mod_rewrite and it is probably a problem while I am redirecting my pages

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.