[SOLVED] cookie set, cant retrieve them.

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
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[SOLVED] cookie set, cant retrieve them.

Post 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';
}
?>
Last edited by ol4pr0 on Mon Jun 21, 2004 2:15 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are aware that most cookies take a page refresh to become available to a page yes?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Yes.. i do however on refresh or when redirecting it to antoher page.. the var $_COOKIE['USERNAME'] is still undefined.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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]
Last edited by feyd on Mon Jun 21, 2004 2:34 am, edited 3 times in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

I will, could be some fun...

i let u know what happend..
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Coulnt set cookie in 5 times is the result.

EDIT:

actually
Couldn't set cookie in 5 attempts!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like your browser isn't accepting the cookies. You may need to fiddle with your settings some more..
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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>';
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.??
Last edited by ol4pr0 on Mon Jun 21, 2004 10:50 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try

Code: Select all

print_r($_COOKIE);
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

set a time frame for the cookie

setcookie("blah", $var, time() + 3600);
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
.....
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Guess the path was the boo boo

Code: Select all

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