Still something wrong with the cookies

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

Still something wrong with the cookies

Post 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'];
}
Last edited by ol4pr0 on Wed Mar 31, 2004 9:49 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You have to refresh the page before the cookie data is accessible.

Mac
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

I did the cookie just didnt get set.. ( is it me or the code? )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just so I'm sure, the code you posted involves a refresh after each cookie is set?

Mac
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

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

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

Post 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;

}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

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

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

Post 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 ?
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

You do know you didnt close your PHP tag..don't you?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try doing a

Code: Select all

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