Jump to content

Redirect to self - Page isn't redirecting properly


richever

Recommended Posts

I'm new to php and to web programming, to some extent, and I'm trying to do the following.

 

First, I am trying to set a number of values returned from a server using calls to setcookie().  From what I understand, for these cookies to be 'enabled' and be contained with $_COOKIE I need to reload the page. 

 

So, what does this exactly mean? 

1) Should I reload the page I'm on (redirect to self?) or can I just load another page with in the same session? 

2) If I'm correct about either of these then if I were to examine the cookies that my browser thinks it has, should I expect to see these same cookies there?

 

What I'm trying to do now is redirect to the page on which I loaded my cookies.  Below is some code but with all the cookie setting code removed.  It's just the redirect I'm concerned about now because it seems to be in an infinite loop. 

 

<?php
session_start();

if (!isset($_SERVER['cookies_set']))
{
   // set cookies here and then prepare to redirect...

   $_SERVER['cookies_set'] = 1;

   header("Location: ". $_SERVER['PHP_SELF']);
   die;
}
else
{
   $_SERVER['cookies_set'] = NULL;
   unset($_SERVER['cookies_set']);
}

?>
<html>
cookies_set: <? echo $_SERVER['cookies_set']; ?>
<br>
</html>    

 

Can anyone give me some help.  I'd really appreciate it!

 

 

 

Thanks for the reply discomatt, but I'm using $_SERVER to store a value and nothing else. I am familar with setcookie() and I understand how to use it.  I didn't include that code because it's not how to call this function that I don't understand, but rather what its behaviour is.  I've called setcookie() and I don't see the cookies I've set being set.  My second problem is trying to redirect to the same page without getting a infinite loop in the browser. 

 

Again, thanks for the reply though.

 

Rich

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.