Jump to content

Cookie Not Settings


morrisjohnny

Recommended Posts

After using a log-in script for a while i've started to get to grips to it, however after trying to set a cookie (so i can track the number of people online and other reasons. the cookie isn't setting which i don't understand. I'm obiously doing something silly but i don't understand what. it's like the cookie isn't being set. because when i echo it out it doesn't show anything.Here's My code

<?phpinclude("__connect__.php");session_start();$logged_in = false;$msg = "";if ($_SESSION['online'])	{	 if(isset($_COOKIE['active']))	 {	 	if($_COOKIE['active']>=time())		{			//update users last active			$currenttime=time()+800;			setcookie("active", $currenttime, time()+3200);			//updating table			mysql_query("UPDATE $tbl_users SET LA='time()' WHERE username='$_SESSION[username]' AND ID='$_SESSION[iD]'");		}		else		{			$logged_in=false;		}	 }	 else	 {	 	echo"error";	 	$logged_in=false;	 }	}else if (isset($_POST['username'])){  $db=mysql_connect($hostname, $username, $password);  mysql_select_db($database, $db);  $username = htmlentities($_POST['username']);  $password = htmlentities($_POST['password']);  $username = mysql_real_escape_string($username);  $password = mysql_real_escape_string($password);  $query = mysql_query("SELECT ID, Username, Password FROM $tbl_users WHERE username = '$username' AND password = '$password'");  if(mysql_num_rows($query) == 1)  {	$result = mysql_query("SELECT * FROM $tbl_users WHERE username='$username' AND Password='$password'");	$getheader = mysql_fetch_array($result);	  $currenttime=time()+800;	  $_SESSION['username']=$getheader['Username'];	  $_SESSION['ID']=$getheader['ID'];	  $_SESSION['City']=$getheader['City'];	  $_SESSION['online'] = true;	  setcookie("active", $currenttime, time()+3200);	  mysql_query("UPDATE $tbl_users SET LA='time()' WHERE username='$_SESSION[username]' AND ID='$_SESSION[iD]'");	  //End Cookie	$logged_in = true;  }  else    $msg .= "The username and password did not match.<br /><br />";mysql_free_result($query);mysql_close($db); /*Also this Line*/}?>

Link to comment
Share on other sites

A couple things to keep in mind:Cookies aren't available on the same page when you set them, you need to refresh the page for the cookies to be sent to the server.Cookies have poor support on localhost or any domain without a TLD, so it's always best to test on a live server.You can use Firebug to see if the server is telling the browser to set the cookie. If you open up Firebug and open your page, go to the Net tab, then click All, and check the response headers. If the server is telling the browser to set a cookie then there will be a cookie header with the information in it.

Link to comment
Share on other sites

Cookies aren't available on the same page when you set them, you need to refresh the page for the cookies to be sent to the server.
Well that kind of ruins my plan then guess i'll have to come up with something else. Cheers i never knew this. Think i'll stick to my POST & Get methods as they seem to do the job.
Cookies have poor support on localhost or any domain without a TLD, so it's always best to test on a live server.
Hmmm, thinking about it if you don't have a 'domain' set i surpose it might get a bit confussed (the cookie i mean)anyway thanks once again i'm thinking about using a different method now (checking the last active time from the database and working from that. Thanks once again :)
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...