Page 1 of 1

Still something wrong with the cookies

Posted: Tue Mar 30, 2004 8:31 am
by ol4pr0
Question: how to get the part if == user1 to set a cookie ?


login = user1
it will automaticly set the cookie 'mail' i tried a number of differant ways.. but withouth succes. ( am able to set cookies tho ) with a simple code it worked just fine

Code: Select all

setcookie ("username", $_POST['username']);

echo $username; // does return the username!

if ($HTTP_COOKIE_VARS['username'] == 'user1') {
	setcookie ("mail", "user1@yahoo.com",time()-1200);
}

if ($HTTP_COOKIE_VARS['username'] == 'user2') {
	setcookie ("mail", "user2@yahoo.com",time()-1200);
}

if (isset($_COOKIE['mail'])){

echo $_COOKIE['mail'];
}

Posted: Tue Mar 30, 2004 8:32 am
by twigletmac
You have to refresh the page before the cookie data is accessible.

Mac

Posted: Tue Mar 30, 2004 8:41 am
by ol4pr0
I did the cookie just didnt get set.. ( is it me or the code? )

Posted: Tue Mar 30, 2004 8:43 am
by twigletmac
Just so I'm sure, the code you posted involves a refresh after each cookie is set?

Mac

Posted: Tue Mar 30, 2004 9:20 am
by Wayne

Code: Select all

echo $username; // does return the username!
this line will kill it, setcookie infomation is sent with the headers, this must be put before any header() calls or any output to the browser!

you also realise that time() -1200 will set the cookie in the past ie. delete the cookie?!

Posted: Tue Mar 30, 2004 10:30 am
by ol4pr0
twiglemac: nah did a manual refresh ( F5 )

Wayne: Thanks

Code: Select all

echo $username
i added that one to make sure username was there on the next page...

Also for the ()- history hint ;-)

Posted: Wed Mar 31, 2004 9:47 am
by ol4pr0
Something still not quit right...

cookie error
undefined username
cant modify header..

( java part is not importent its just that on this machine i only debug and test the pages.. , server does have it. )

Notice: Undefined index: username in d:\apache\htdocs\upload.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at d:\apache\htdocs\upload.php:2) in d:\apache\htdocs\upload.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at d:\apache\htdocs\upload.php:2) in d:\apache\htdocs\upload.php on line 16


please advise ;-) , my code ( parts of it)

login.php

Code: Select all

<form valign=middle align=center name="Sample" method="post" action="debug.php">
  <table width="40%" border="0" cellpadding="0" cellspacing="0" bordercolor="#000000">
    <tr> 
      <td colspan="2" background="../images/cellpic1.gif" valign="middle"> 
        <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>USR - PWD</b></font></div>
    </td>
  </tr>
    <tr> <!-- bg=6*c -->
      <td width="32%" bgcolor="#ffffff" valign="middle"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">&nbsp;Username</font></b></td>
      <td width="68%" valign="middle"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
        &nbsp;
<input type="text" name="username" size="15" maxlength="15">
        </font></b></td>
  </tr>
    <tr> <!-- bg=6*c -->

  </tr> <!-- bg =6*c -->
    <tr valign="middle" bgcolor="#ffffff"> 
      <td colspan="2"> 
        <div align="center">
          <input type="image" src="images/login.gif" value="Login">
          <input type="image" src="images/clear.gif" value="Clear">
        </div>
      </td>
  </tr>
</table>
</form>

debug.php ( upload.php on the server )

Code: Select all

<?
setcookie ("username", $_POST['username']);

if ($HTTP_COOKIE_VARS['username'] == 'c1') 
	{
	setcookie ("mail", "c1@some.com",time()+1200);
	}

if ($HTTP_COOKIE_VARS['username'] == 'c2') 
	{
	setcookie ("mail", "c2@some.com",time()+1200);
	}

if ($HTTP_COOKIE_VARS['username'] == 'c3') 
	{
	setcookie ("mail", "c3@some.com",time()+1200);
	}

if ($HTTP_COOKIE_VARS['username'] == 'c4') 
	{
	setcookie ("mail", "c4@some.com",time()+1200);
	}

if ($HTTP_COOKIE_VARS['username'] == 'c5') 
	{
	setcookie ("mail", "c5@some.com",time()+1200);
	}

?>
<html>
<head>
<SCRIPT language="JavaScript1.2">
function poponload()
{
popwindow= window.open ("readme.html", "popup",
    "location=1,status=1,scrollbars=auto,width=400,height=400");
popwindow.moveTo(100,150);
}

function process()
{
popwindow= window.open ("mailit.php", "popwindow",
    "location=1,status=1,scrollbars=auto,width=600,height=500");
popwindow.moveTo(100,150);
}
</SCRIPT>

// lots of other things which work and dont have anything to do with the cookie 


<script language=javascript>
file = "<?php echo $uploaded; ?>";
alert("Validar Archivo: " +file);
</script>

<?
$txt = new Java('txtFile', "{$absolute_path}/{$uploaded}");
$answer = $txt->validar(True);
?>



<script language=javascript>
respuesta = "<?php echo $answer; ?>";
alert("Respuesta: " +respuesta);
</script>

<?

if (isset($_COOKIE['mail'])){
?>
<body onload="javascript: process()">
<?
}

break;

}
?>

Posted: Wed Mar 31, 2004 11:44 am
by twigletmac
In debug.php you are setting a cookie and then immediately trying to access it - you cannot do that, there has to be a page refresh before the cookie will be available.

Mac

Posted: Wed Mar 31, 2004 1:27 pm
by ol4pr0
Thanks twigletmac,

however do not quiet understand the folllowing,

Code: Select all

setcookie ("username", $_POST['username']);


The above code above works on one page, but on the upload / debug.php it will return $username undefined

It works on the page where i only set cookies

Any reason why ?

Posted: Wed Mar 31, 2004 1:28 pm
by ol4pr0
ol4pr0 wrote:Thanks twigletmac,

however do not quiet understand the folllowing,

Code: Select all

setcookie ("username", $_POST['username']);


The above code above works on one page, but on the upload / debug.php it will return $username undefined
Notice: Undefined index: username in d:\apache\htdocs\upload.php on line 2
It works on the page where i only set cookies

Any reason why ?

Posted: Wed Mar 31, 2004 6:41 pm
by Pozor
hello,

post please the content of that cookie that is set with your page on your pc.
you dont define the the host, if you have subdomain, it will only be accepted with this subdomain -> domain.com instead of not define may solve your problem...


greez Pozor

Posted: Wed Mar 31, 2004 7:03 pm
by d3ad1ysp0rk
You do know you didnt close your PHP tag..don't you?

Posted: Thu Apr 01, 2004 2:45 am
by Wayne
try doing a

Code: Select all

var_dump($_POST)
and make sure the username value is there.