Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set cookie and redirect

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
How can I, like perl, set a cookie and redirect at the same time?

Thanks / Henrik
 
setcookie('name: value');
header('Location: '');

You cannot set an cookie and in the same statement redirect. mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
I tried the above using two files. It seems like it redirects without setting the cookie first. Have you tried the above with two files? Please try it, it doesn't work for me!

First file:
name= test.php

<?
setcookie (&quot;cookie&quot;, &quot;test&quot;,time()+3600, &quot;/&quot;, &quot;127.0.0.1&quot;);
header('Location: test2.php');
?>

Second file:
name= test2.php
<?
echo $cookie;
?>

Thanks Henrik
 
Yes i have tried the above. It is working if you write test.php as follows:

<?php
setcookie (&quot;cookie&quot;, &quot;test&quot;);
header('Location: test2.php');
?>

Where you go wrong is in defining the domain as localhost.
The next will set an cookie for the next hour for the domain you are currently on.
<?
setcookie (&quot;cookie&quot;, &quot;test&quot;,time()+3600, &quot;/&quot;);
header('Location: test2.php');
?>
To delete the cookie, set the time in the past: (Use the same parameters as you used to set the cookie)
<?
setcookie (&quot;cookie&quot;, &quot;test&quot;,time()-3600, &quot;/&quot;);
header('Location: test2.php');
?>
Good luck mcvdmvs
-- &quot;It never hurts to help&quot; -- Eek the Cat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top