Ridiculously easy problems, but they have me stumped (Cookies)

SpaceshipSpaceship
Watch

Albino

Munky DesignsEstablished Member
Impact
17
[resolved] ridiculously easy problems, but they have me stumped (cookies)

ok, im an idiot, simple things stump me.

for instance, this beauty!

1. I can't display/access cookies. I can make them, see them if I browse to the file, but when I use:

Code:
        echo $_COOKIE['siteLogin'];
	if (isset($_COOKIE['test'])){
		echo "worky";
	}else{
		echo "no cookie here";
	}

I get nothing, no output (names of cookies are correct). to make them I use:

Code:
@setcookie('siteLogin', $cookie, time() + 31104000);
			@setcookie("test", "lol", time() + 36000);


any ideas, its driving me crazy
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Remove '@' from the function call, see if you get any errors.
 
1
•••
sorry I should have said, I've already tried that, no errors :(
 
0
•••
Are you setting the cookie in the same page call as when you are checking its existence. A cookie is not available until a page after you created it.

Ie if you create the cookie on login.php and then immediately try to check its existence it will not work. The cookie will not be available until they go to another page. The best way to get around this if it is the case if use a header redirect once the cookie is set.

Also when you are testing have the following at the top of your scripts:-

PHP:
error_reporting(E_ALL);

Although @ does indeed suppress errors, the errors still may not show depending on your servers configuration.
 
Last edited:
1
•••
Check in your browser (Security/Show cookies or Cookie Manager).

Which domain is it set for? What is the expiry time? What is the time on your web server and on your computer?
 
1
•••
I tried some quick code and it worked fine for me.

PHP:
<?php

  error_reporting(E_ALL); 
  $cookie = "Wee"; // Well it needs a value...
  
  if(isset($_GET['del'])) { 
  setcookie('siteLogin', "", time() - 31104000);
  setcookie("test", "", time() - 36000);
  echo "Deleted cookies. Refresh for it to work!";
  exit();
  }

	setcookie('siteLogin', $cookie, time() + 31104000);
  
	if (isset($_COOKIE['test']))
	{
			echo "Working<br />";
	}
	else
	{
		if(setcookie("test", "lol", time() + 36000)) echo "I think we made one...<br />";
		echo "Not working<br />"; // Echo after to ensure you don't get Header problems
		echo "<br />Try refreshing your browser, browsers normally need a refresh to get it to work";
	}
	
	if(isset($_COOKIE['siteLogin'])) echo $_COOKIE['siteLogin'];
	
?>
 
1
•••
Danltn said:
I tried some quick code and it worked fine for me.

PHP:
<?php

  error_reporting(E_ALL); 
  $cookie = "Wee"; // Well it needs a value...
  
  if(isset($_GET['del'])) { 
  setcookie('siteLogin', "", time() - 31104000);
  setcookie("test", "", time() - 36000);
  echo "Deleted cookies. Refresh for it to work!";
  exit();
  }

	setcookie('siteLogin', $cookie, time() + 31104000);
  
	if (isset($_COOKIE['test']))
	{
			echo "Working<br />";
	}
	else
	{
		if(setcookie("test", "lol", time() + 36000)) echo "I think we made one...<br />";
		echo "Not working<br />"; // Echo after to ensure you don't get Header problems
		echo "<br />Try refreshing your browser, browsers normally need a refresh to get it to work";
	}
	
	if(isset($_COOKIE['siteLogin'])) echo $_COOKIE['siteLogin'];
	
?>

Great Bit of code.. will help me with a problem I've been having :) Thanks.
 
0
•••
ok, thanks everyone for the help so far! (ive repped you all).

Danltn, that code works fine for me but when I intergrate it, it still does't work. Does where you declare the cookie matter?

like say I make the cookie from scripts/php/login.php, but then wanted to check it from root, would that matter?

I don;t see why it should, but its the only thing I can think of :(
 
0
•••
Albino said:
like say I make the cookie from scripts/php/login.php, but then wanted to check it from root, would that matter?
It would not matter as long as they are the same domain (eg. uploads.mydomain.com)

Did you check the cookie in the browser as I advised? The cookies will not be set if there is some output before the cookie code, see the setcookie documentation. I think even a blank line can prevent the cookie from being set.
 
0
•••
yea, I checked the cookie, via the tools->options->privacy->show cookies method in firefox. They show up and I can see the value is what I want them to be.

this really has me stumped!
 
0
•••
It sometimes complains if the cookie is accessed from a different directory to the one it was set in.

E.G. I might not be able to access my cookie "EG" from /danswork/ when I set it in /danscode/.

Dan
 
0
•••
ok, so looks like it might be that then. :(
 
0
•••
As per http://ca3.php.net/setcookie , the default path for the cookie is the current directory. You want to override that with a '/'. ie. if you give a 4th parameter to your setcookie function call and that 4th parameter is '/', you shouldn't have any problems with that.
 
1
•••
cheers TMA, it worked great :)
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer

We're social

Spaceship
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back