For Programmers: Free Programming Magazines  


Home > Archive > PHP on Windows > February 2005 > RE: [PHP-WIN] Re: Cookie Problems on Localhost









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: [PHP-WIN] Re: Cookie Problems on Localhost
Mikey

2005-02-23, 3:56 pm

> 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
Mikey

2005-02-23, 3:56 pm

> 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


Whoops! Forgot to say that you should then access your site using
http://mymachine

Mikey
Luis Moreira

2005-02-23, 3:56 pm

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:

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


Joseph L. Mueller

2005-02-25, 3:57 pm

Luis Moreira wrote:
[color=darkred]
> 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:
>
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
Tony Yau

2005-02-25, 3:57 pm

is your browser blocking the cookie?

"Joseph L. Mueller" <unclejoe@prodigy.net> wrote in message
news:20050224141828.88529.qmail@lists.php.net...
> Luis Moreira wrote:
>
> 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

Randy Clamons

2005-02-25, 8:57 pm

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 sampl=
e:

Begin sample:

<?php
setcookie("vegatable","artichoke") ;
?>
<html>
<body>
<SCRIPT language=3Djavascript>
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, sp=
ecify 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 advantag=
e of setting the domain value is that the browser will return the same cook=
ie value to multiple hosts within the same domain.

Let's say you have three servers within the same domain to split up the wor=
kload, with these names www1.mydomain.com, www2.mydomain.com, www3.mydomain=
..com. Your application needs to monitor your session (or cookie) data no ma=
tter 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 i=
ncluding a top level domain (.com, .net, etc.). So, localhost doesn't work!

If you are exploring how cookies work, or developing an application that wi=
ll run on a single web server, leaving the domain blank will take care of y=
ou. 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 hel=
pful to run a DNS name server for the LAN.

Randy Clamons
Systems Programming
Novaspace.com


> ------------Original Message------------
> From: "tony yau" <tony.yau@emigen.co.uk>
> To: php-windows@lists.php.net
> Date: Fri, Feb-25-2005 12:41 PM
> Subject: Re: [PHP-WIN] Re: Cookie Problems on Localhost
>
> is your browser blocking the cookie?
> =


> "Joseph L. Mueller" <unclejoe@prodigy.net> wrote in message
> news:20050224141828.88529.qmail@lists.php.net...
[color=darkred]
> it
[color=darkred]
> on
[color=darkred]
> in
[color=darkred]
> no
[color=darkred]
> =


> -- =


> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> =


> =

Joseph L. Mueller

2005-02-26, 3:56 pm

Randy Clamons wrote:

>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, localho
st 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.[color=darkred]
>
>Randy Clamons
>Systems Programming
>Novaspace.com
>
>
>
>
I played around with Mikey's suggestion and also did some "googling"
hit upon using
"my.local.com", which works.

The hosts entry is:
127.0.0.1 my.local.com localhost

Apparently you need a third level entry.

Thanks to all who responded.

--
Joseph L. Mueller
Joseph L. Mueller

2005-02-26, 3:56 pm

Randy Clamons wrote:

>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, localho
st 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.[color=darkred]
>
>Randy Clamons
>Systems Programming
>Novaspace.com
>
>
>
>
I played around with Mikey's suggestion and also did some "googling"
hit upon using
"my.local.com", which works.

The hosts entry is:
127.0.0.1 my.local.com localhost

Apparently you need a third level entry.

Thanks to all who responded.

--
Joseph L. Mueller
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com