adrianle Posted January 9, 2010 Share Posted January 9, 2010 Hi all.. I need to figure out this little snippet right quick.. seems like it should be easy enough to do, but I'm flailing here... I need to have a blurb that checks to see if a cookie of a specific name is set, and if it is - then show a certain line of code (basic href link).. but if the cookie is NOT set, to show a different line of link code. HELP!?!!? Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/ Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 if (isset($_COOKIE['name'])) { // code } else { // other code } Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991415 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 Believe it or not, I actually have that part! What I don't have is how to add in regular HTML code in the spaces in between the statements, marked in your comment by "// code" Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991434 Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 Two ways.... <?php if (isset($_COOKIE['name'])) { echo '<a href="foo.php">foo</a>'; } else { echo '<a href="foo.php">foo</a>'; } ?> or <?php if (isset($_COOKIE['name'])) { ?> <a href="foo.php">foo</a> <?php } else { ?> <a href="foo.php">foo</a> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991438 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 ok, I tried that too and all I see is the 'else' chunk of the code.. and I think I know why.. the script HTML I have in the first chunk has single ticks in it.. and it's interfering with the PHP perhaps... Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991444 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 I put this in and I ONLY see the chunk after the 'else' even if the cookie is set: <?PHP if (isset($_COOKIE["April09"])){ ?> <a href="p20090419show.php"><img src="archive.gif" width="35" height="48" border="0"></a> <?PHP } else { ?> Cookie not set <?PHP } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991445 Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 Then the cookie is not being set. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991451 Share on other sites More sharing options...
premiso Posted January 9, 2010 Share Posted January 9, 2010 Post the code you are setting the cookie with. Note that if you set the cookie on the same page it will not be available until a page reload. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991525 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 This sets the cookie: <?php setcookie ('April09', 'confirmed', time() + (60*60*48)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991535 Share on other sites More sharing options...
clay1 Posted January 9, 2010 Share Posted January 9, 2010 I put this in and I ONLY see the chunk after the 'else' even if the cookie is set: <?PHP if (isset($_COOKIE["April09"])){ ?> <a href="p20090419show.php"><img src="archive.gif" width="35" height="48" border="0"></a> <?PHP } else { ?> Cookie not set <?PHP } ?> I could be wrong but won't escaping out of PHP each time like that cause ALL the html to be processed? If you do print_r($_COOKIE); what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991544 Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 I could be wrong but won't escaping out of PHP each time like that cause ALL the html to be processed? You would be wrong. It is still only conditionally sent. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991550 Share on other sites More sharing options...
clay1 Posted January 9, 2010 Share Posted January 9, 2010 I could be wrong but won't escaping out of PHP each time like that cause ALL the html to be processed? You would be wrong. It is still only conditionally sent. Good to know Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991555 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 So.. any final thoughts on why this is happening for me? Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991791 Share on other sites More sharing options...
oni-kun Posted January 9, 2010 Share Posted January 9, 2010 So.. any final thoughts on why this is happening for me? Why not check the documentation for setcookie and read common problems associated, and check out the extra parameters. Also , physically checking for the cookie's existence would help to check function. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991798 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 hi oni-kun... I was hoping Thorpe could continue his previous thread of help.. thanks though Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991829 Share on other sites More sharing options...
oni-kun Posted January 9, 2010 Share Posted January 9, 2010 hi oni-kun... I was hoping Thorpe could continue his previous thread of help.. thanks though There's absolutely nothing you've given us to help you with, that's why he started going off-topic. Your code is completely correct and should work (under almost any circumstance) unless it is a problem with your server, such as, your cookie is being invalidated because of paths as mentioned by the other parameters you could set with the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991839 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 well, that's actually more than I've gotten from others on this topic, so thanks.. I appreciate the confirmation and suggestion.. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991851 Share on other sites More sharing options...
crabfinger Posted January 9, 2010 Share Posted January 9, 2010 <?php var_dump($_COOKIE); ?> Post what that tells you. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991854 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 It gives me this (which looks right to me?) array(1) { ["April09"]=> string(9) "confirmed" } Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991883 Share on other sites More sharing options...
crabfinger Posted January 9, 2010 Share Posted January 9, 2010 Well if that is what is being ouput then the only thing that I can think of is that it is a problem with isset() and that can't be right. But you could try it without escaping from php <?php var_dump($_COOKIE["April09"]); echo '<br / >'; if(isset($_COOKIE["April09"])) { echo '<a href="p20090419show.php"><img src="archive.gif" width="35" height="48" border="0"></a>'; } else { echo 'Cookie not set'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991957 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 This all actually works beautifully if it's in a php page all by itself. For some reason, when I implement it on the page where it's meant to show, it fails and only shows the code after "else"... any thoughts on what might prevent the isset portion to be functional? Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991976 Share on other sites More sharing options...
adrianle Posted January 9, 2010 Author Share Posted January 9, 2010 For any interested.. I figured it out. Probably an elementary mistake really.. anyways, the test files that always worked were in the same directory as the file setting the cookie, and the page calling/checking the cookie was in a different directory. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-991989 Share on other sites More sharing options...
oni-kun Posted January 10, 2010 Share Posted January 10, 2010 For any interested.. I figured it out. Probably an elementary mistake really.. anyways, the test files that always worked were in the same directory as the file setting the cookie, and the page calling/checking the cookie was in a different directory. If you would have looked at the documentation as mentioned more than one time.. Quote Link to comment https://forums.phpfreaks.com/topic/187781-if-php-cookie-set-show-code/#findComment-992068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.