NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page ridiculously easy problems, but they have me stumped (cookies)

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 11-04-2007, 04:50 PM THREAD STARTER               #1 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



[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:
????: NamePros.com http://www.namepros.com/programming/392179-ridiculously-easy-problems-but-they-have.html

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 by Albino; 11-12-2007 at 03:44 PM.
Albino is offline  
Old 11-04-2007, 05:07 PM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Remove '@' from the function call, see if you get any errors.
Eric is offline  
Old 11-05-2007, 02:34 AM THREAD STARTER               #3 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



sorry I should have said, I've already tried that, no errors
Albino is offline  
Old 11-05-2007, 05:49 AM   #4 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=392179

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 Code:
error_reporting(E_ALL); 
Although @ does indeed suppress errors, the errors still may not show depending on your servers configuration.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Last edited by peter@flexiwebhost; 11-05-2007 at 05:57 AM.
Peter is offline  
Old 11-05-2007, 07:43 AM   #5 (permalink)
Senior Member
Join Date: Aug 2007
Posts: 2,172
jido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond repute
 



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?
__________________
______________________________________
Help with crosswords http://lettercombination.com
Pitfalls of Java http://javasyntax.com
_______________ f o r . s a l e ______________
jido is offline  
Old 11-05-2007, 01:01 PM   #6 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
I tried some quick code and it worked fine for me.

PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=392179

  error_reporting
(E_ALL); 
  
$cookie "Wee"// Well it needs a value...
  
  
if(isset($_GET['del'])) { 
  
setcookie('siteLogin'""time() - 31104000);
  
setcookie("test"""time() - 36000);
????: NamePros.com http://www.namepros.com/showthread.php?t=392179
  echo 
"Deleted cookies. Refresh for it to work!";
  exit();
  }

    
setcookie('siteLogin'$cookietime() + 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'];
    
?>
Daniel is offline  
Old 11-07-2007, 04:39 AM   #7 (permalink)
New Member
Join Date: Nov 2005
Posts: 6
LovesYou is an unknown quantity at this point
 



Originally Posted by Danltn
I tried some quick code and it worked fine for me.

PHP Code:
<?php

  error_reporting
(E_ALL); 
  
$cookie "Wee"// Well it needs a value...
  
  
if(isset($_GET['del'])) { 
  
setcookie('siteLogin'""time() - 31104000);
  
setcookie("test"""time() - 36000);
????: NamePros.com http://www.namepros.com/showthread.php?t=392179
  echo 
"Deleted cookies. Refresh for it to work!";
  exit();
  }

    
setcookie('siteLogin'$cookietime() + 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.
LovesYou is offline  
Old 11-07-2007, 09:07 AM THREAD STARTER               #8 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



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
Albino is offline  
Old 11-07-2007, 09:16 AM   #9 (permalink)
Senior Member
Join Date: Aug 2007
Posts: 2,172
jido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond reputejido has a reputation beyond repute
 



Originally Posted by Albino
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.
__________________
______________________________________
Help with crosswords http://lettercombination.com
Pitfalls of Java http://javasyntax.com
_______________ f o r . s a l e ______________
jido is offline  
Old 11-07-2007, 09:26 AM THREAD STARTER               #10 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



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!
Albino is offline  
Old 11-07-2007, 09:43 AM   #11 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
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
Daniel is offline  
Old 11-07-2007, 09:53 AM THREAD STARTER               #12 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



ok, so looks like it might be that then.
Albino is offline  
Old 11-07-2007, 06:30 PM   #13 (permalink)
NamePros Member
Join Date: May 2006
Posts: 160
TwistMyArm is on a distinguished road
 



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.
TwistMyArm is offline  
Old 11-12-2007, 03:44 PM THREAD STARTER               #14 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



cheers TMA, it worked great
Albino is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:41 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger