Jump to content

if php cookie set, show code...


adrianle

Recommended Posts

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!?!!?

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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   

}

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..  ::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.