Page 1 of 1

[SOLVED] cookie set, cant retrieve them.

Posted: Mon Jun 21, 2004 2:08 am
by ol4pr0
With the following the cookie is not being set.. I dont have a clue

have the i-net security of browser on the most lowest as possible..
still echo returned, Cant set cookie.

Code: Select all

<?
setcookie ("USERNAME", $_POST['username']);
setcookie ("PASSWORD", $_POST['password']); 
if(isset($_COOKIE['USERNAME']))
{
include_once ("auth.php");
include_once ("authconfig.php");
 
$username =  $_POST['username'];
$password =  $_POST['password'];

$Auth = new auth();
$detail = $Auth->authenticate($username, $password);

if ($detail==0)
{
?><HEAD>
<SCRIPT language="JavaScript1.1">
	<!--
		location.replace("<? echo $failure; ?>");
	//-->
</SCRIPT>
</HEAD>
<?
}
elseif ($detail==1) {
?>
	<HEAD>
		<SCRIPT language="JavaScript1.1">
		<!--
			location.replace("<? echo $success; ?>");
		//-->
		</SCRIPT>
	  </HEAD>
	<?
	  }
}
else
{echo 'couldnt set cookie';
}
?>

Posted: Mon Jun 21, 2004 2:11 am
by feyd
you are aware that most cookies take a page refresh to become available to a page yes?

Posted: Mon Jun 21, 2004 2:15 am
by ol4pr0
Yes.. i do however on refresh or when redirecting it to antoher page.. the var $_COOKIE['USERNAME'] is still undefined.

Posted: Mon Jun 21, 2004 2:21 am
by ol4pr0
Thats why i put in that if(isset

That after many tries and differant refreshes, so i thought maby something wrong with the code

Posted: Mon Jun 21, 2004 2:28 am
by feyd
for fun.. try this in it's own page:

Code: Select all

<?php

if(!isset($_GET['refresh']))
{
  header('Location: '.$_SERVER['SCRIPT_NAME'].'?refresh=0&USERNAME='.(isset($_REQUEST['USERNAME'])?$_REQUEST['USERNAME']:'george'));
}
elseif(($ref = $_GET['refresh']) < 5)
{
  header('Set-Cookie: USERNAME='.$_REQUEST['USERNAME'].'; Version = 1;');
  header('Location: '.$_SERVER['SCRIPT_NAME'].'?refresh='.($_GET['refresh']+1).'&USERNAME='.$_REQUEST['USERNAME']);
  echo 'refresh '.$_GET['refresh'];
  flush();
}
else
{
  die('Couldn''t set cookie in 5 attempts!');
}

?>
[edit]oops.. made a tiny boo-boo in the die.. fixed.[/edit]

Posted: Mon Jun 21, 2004 2:29 am
by ol4pr0
I will, could be some fun...

i let u know what happend..

Posted: Mon Jun 21, 2004 2:39 am
by ol4pr0
Coulnt set cookie in 5 times is the result.

EDIT:

actually
Couldn't set cookie in 5 attempts!

Posted: Mon Jun 21, 2004 2:42 am
by feyd
sounds like your browser isn't accepting the cookies. You may need to fiddle with your settings some more..

Posted: Mon Jun 21, 2004 3:39 am
by ol4pr0
WEll i tried something very simple

Code: Select all

$cookiename='name';
$value='some';
setcookie($cookiename,$value);
echo $_COOKIE['name'];
#Did work on refresh.
What u had up there.. didnt work.

What i have i will show u, i have a couple pages.

index where the login is located, after the login it goes to the authenticate.php which has the code above.

the auth.php has this.

Code: Select all

<?
class auth{
	
	var $HOST = "localhost";
	var $USERNAME = "root";	
	var $PASSWORD = "";	
	var $DBNAME = "woning";


function authenticate($username, $password) {
		$connection = mysql_connect($this->HOST, $this->USERNAME, $this->PASSWORD);
		$SelectedDB = mysql_select_db($this->DBNAME);
		$sql = mysql_query("SELECT * FROM users WHERE login='$username'", $connection); 
		$data = mysql_fetch_array($sql); 
		$encrypted_password = $data['password']; 
		$password = crypt($_POST['password'], $encrypted_password); 
		if ($password == $encrypted_password)  { 
		return 1;
					
			}else{ 
			   return 0;
				
			} 
     $UpdateRecords = "UPDATE users SET lastlogin= NOW(), logincount=logincount + 1 WHERE login='$username'";
	 $Update = mysql_query($UpdateRecords);
}
The index part

Code: Select all

echo '<form action="'.$resultpage.'" method="post" target="1">';
		      echo '<b>Usario:</b><br><input maxLength=40 size=15 name=username><br><b>Clave:</b><br><input type=password maxLength=25 size=15 name=password><br>';
		      echo "<button name="submit" type="submit" style="font-family: v; font-size: 10pt; color: #5E6A7B; border: 1px solid #5E6A7B; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1">Login</button>";
			  echo '</form>';

Posted: Mon Jun 21, 2004 10:33 am
by ol4pr0
installed Firefox, now the cookies are being set, however for some dumb reason i just cant retrieve them.

Is there something, i might have forgotten.??

Posted: Mon Jun 21, 2004 10:35 am
by feyd
try

Code: Select all

print_r($_COOKIE);

Posted: Mon Jun 21, 2004 12:26 pm
by ol4pr0
I am clueless

output
Array ( [PHPSESSID] => b9bc4db24acfcad40c476154e834d5c4 )
While the cookie manager in Firefox clearly states that
localhost USERNAME
localhost PASSWORD
is set.
The popup box tells me the same.

refresh as many as you want but the output is the same.

Posted: Mon Jun 21, 2004 12:31 pm
by tim
set a time frame for the cookie

setcookie("blah", $var, time() + 3600);

Posted: Mon Jun 21, 2004 2:01 pm
by ol4pr0
i will try that..

however i tried the following

on my very index page.

Code: Select all

setcookie("USERNAME","SOMEUSER");
return
Array ( [USERNAME] => SOMEUSERNAME [PHPSESSID] => a51050a9f76c64dc27046d5770d4fb78 )
Even tho that the firefox did gave the popup to modify the cookie with the username logged in.

name: Username
content: ik
path :/inc/
secure server: no
.....

Posted: Mon Jun 21, 2004 2:14 pm
by ol4pr0
Guess the path was the boo boo

Code: Select all

setcookie("USERNAME", $_POST['username'],time()+3600,"/");
That fixed it..