Re: Re: Cookie Problems on Localhost

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I tried your example script for setting a cookie. You are absolutely right, you can not set a cookie to localhost that way. That said, it can be done!! Just leave the domain parameter out of the setcookie call. Try this sample:

Begin sample:

<?php
setcookie("vegatable","artichoke") ;
?>
<html>
<body>
<SCRIPT language=javascript>
if (document.cookie) {
    document.write(cookie+'<br>\n')
}
else {
    document.write("No cookie found.<br>\n");
}
</SCRIPT>
<?php
print_r($_COOKIE);
?>
</body>
</html>

End sample

You can still set the expire, path. If you need the cookie to be secure, specify an empty string for the domain. This works. If you set the domain to either 'localhost' or '127.0.0.1', the cookie will not be set. The advantage of setting the domain value is that the browser will return the same cookie value to multiple hosts within the same domain.

Let's say you have three servers within the same domain to split up the workload, with these names www1.mydomain.com, www2.mydomain.com, www3.mydomain.com. Your application needs to monitor your session (or cookie) data no matter which server they end up on. In this case, you could set the domain to '.mydomain.com'. This will cause the browser to return the cookie for any of the three above hosts. The domain needs to be a legitimate domain name including a top level domain (.com, .net, etc.). So, localhost doesn't work!

If you are exploring how cookies work, or developing an application that will run on a single web server, leaving the domain blank will take care of you. Your browser will figure out where to send it.

If you are developing an application that will run on multiple servers, you have a more complicate problem. In that case I would recommend connecting using a private IP address (example: 192.168.0.0-255). It would also be helpful to run a DNS name server for the LAN.

Randy Clamons
Systems Programming
Novaspace.com


> ------------Original Message------------
> From: "tony yau" <tony.yau@xxxxxxxxxxxx>
> To: php-windows@xxxxxxxxxxxxx
> Date: Fri, Feb-25-2005 12:41 PM
> Subject: Re:  Re: Cookie Problems on Localhost
>
> is your browser blocking the cookie?
> 
> "Joseph L. Mueller" <unclejoe@xxxxxxxxxxx> wrote in message
> news:20050224141828.88529.qmail@xxxxxxxxxxxxxxxx
> > Luis Moreira wrote:
> >
> > > You can always try to set a cookie, but wether the browser allows 
> it
> > > or not, it's a whole different ball game.
> > > I believe your problem lies here.
> > >
> > > Anyway, the two scripts I attach, I have just tried, and the cookie
> > > was set by the first, WITH A MESSAGE FROM THE BROWSER ASKING IF I
> > > ALLOWED IT.
> > > The second script read the cookie, no problem.
> > >
> > >
> > > Hope this helps.
> > >
> > > Luis
> > >
> > > Mikey wrote:
> > >
> > >>> That still leaves the problem of how to test cookies and sessions 
> on
> > >>> a local system.
> > >>>
> > >>
> > >>
> > >> Don't know if this will work, but have you tried setting an entry 
> in
> > >> your
> > >> hosts file (C:\WINDOWS or C:\WINNT\system32\drivers\etc) for your
> > >> machine
> > >> name that points to 127.0.0.1?
> > >>
> > >> Eg:
> > >> 127.0.0.1    mymachine
> > >>
> > >> HTH,
> > >>
> > >> Mikey
> > >>
> > >>
> > >>
> > Luis,
> > Thanks for sending the script to my e-mail account. I tried them but 
> no
> > cookie was set. All I got rom the r_print was "Array {}".
> >
> > -- 
> > Joseph L. Mueller
> 
> -- 
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux