COOKIE results in 'undefined index' Solved.. kinda.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

COOKIE results in 'undefined index' Solved.. kinda.

Post by markusn00b »

In the process of making a user registration page, and if a username and or email address is already in use i set a cookie then use header( ... to redirect the user back to the page with the form.
On the page with the form, i check to see if there is reason to echo the error (if isset $_GET['i']; .. )
And then echo the cookie out.
Hooooowever, it seems the cookie isn't being recognised or i'm just silly silly silly!

Code: Select all

 
// setting the cookie
setcookie('Signup_Error', "Both username and email address have been taken! Please try again",  time() + 3600);
header("Location: http://localhost/blog.mahcuz.com/?i");
 

Code: Select all

 
// retrieving the cookie
<?php
if(isset($_GET['i']))
{
 $_error = $_COOKIE['Signup_Error'];
 ?>
 <div id="error_signup">
  <?php
  echo $_error;
  ?>
 </div>
<?php
}
?>
 
All this does is throw an error: undefined index: Signup_Error

If i am to cancel the header redirect and just echo the cookie out as soon as it is set, it works fine

Code: Select all

 
// setting the cookie
setcookie('Signup_Error', "Both username and email address have been taken! Please try again",  time() + 3600);
echo $_COOKIE['Signup_Error'];
// header("Location: http://localhost/blog.mahcuz.com/?i");
 
Living up to my name.. markusn00b
;)
Last edited by markusn00b on Sun Feb 17, 2008 9:25 am, edited 1 time in total.
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Re: COOKIE results in 'undefined index'

Post by dreamscape »

what browser are you using by chance?

I've come across this numerous times on OS X... not all browsers support setting cookies for "localhost"

You can avoid this by using 127.0.0.1 or by setting up a local domain to alias localhost (domain.local for example)
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: COOKIE results in 'undefined index' Solved.. kinda.

Post by markusn00b »

Thanks for your 2cents.

I just changed from using cookies to using sessions instead..
Post Reply