Jump to content

Email in a cookie


Diegar

Recommended Posts

I am trying to use an email address as the user ID for a site. When i try to set the cookie as:

setcookie(email,$email,time()+3600);

When it is referred to the index.php, $_COOKIE['email'] is empty. I know the $email string isn't empty though, cause below the setcookie line, it does other things with it, successfully. Am i unable to set a cookie as an email address, perhaps because of the '@' or the '.', or should i look for something else in the code, instead?

Link to comment
Share on other sites

Maybe i should rephrase a bit. Is there something wrong with this block of code?

		setcookie(email,$email,time()+3600);		$deltime=time()+3500;		$log=time();		mysql_query("UPDATE members SET last_log='$log' WHERE email='$email'");		mysql_query("UPDATE members SET online='Yes' WHERE email='$email'");		mysql_query("UPDATE members SET delonline='$deltime' WHERE email='$email'");		echo "<script type=\"text/javascript\">window.location='../index.php';</script>";

...cause when it goes to the index.php, $_COOKIE['email'] is empty. I thought maybe it was the JS redirect, but i //'d it out and added a meta refresh tag to the bottom of the page, under the code, and it did the exact same thing, only took longer.

Link to comment
Share on other sites

Okay, i figured it out, sorta, but i am not satisfied with it. When you login, the Login.php with the form is in the main folder, with the index.php. When you submit your form, to actually login, i had it going to another folder, one that i could keep hidden, to do the actual logging in. Once it was done, it would refer you back to the main folder to check your login status on the index.php page. Problem was, the cookie was being set for that hidden folder, so when the user is referred back to the index.php, the cookie was empty. Hope that made sense.... Is there a way around this? A way to set the cookie for the main folder, and not the hidden one?

Link to comment
Share on other sites

Well, you definately need the cookie name in quotes, you're trying to use a constant right now. You might also need a domain name and path. Are you trying to do this on your local server? Some browsers have problems with cookies on localhost, or any domain without a TLD, the RFC for cookies says that cookies only apply to second-level domain names and down, localhost is only a top-level domain. You can try to set the domain for the cookie as 127.0.0.1, or according to the comments on php.net you can set false as the domain:

instead for localhost you should use false.to make your code work on both localhost and a proper domain, you can do this:<?php$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;setcookie('cookiename', 'data', time()+60*60*24*365, '/', $domain, false);?>
You can also try to modify your host file under Windows so that you can type a domain like www.mydomain.com and have it redirect to your computer, that way you can set cookies with a normal domain name.http://www.webmasterworld.com/forum47/142.htm
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...