FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Code to create a cookie in PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Code to create a cookie in PHP [message #169825] Mon, 27 September 2010 19:18 Go to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Now that I can delete cookies, I'd like to create cookies, but this code
fails to create a cookie. Can anyone help me by telling me what is
wrong, please?

-MikeB

<?php
$name = "headercookie";
$value = "MikeB set this in the header";
$expire = time() * 24 * 60 * 60;
$path = '/';
$domain = "localhost";
setcookie($name, $value, $expire);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Set cookies</title>
</head>
<body>
<?php
if (isset($_COOKIE)) {
echo '<br />First $_COOKIE<pre>';
print_r($_COOKIE);
echo "</pre>";
} else {
echo "<br />No cookies here!";
}
Echo "<br />Now setting my own cookie!";
$name = "headercookie";
$value = "MikeB set this in the body";
$expire = time() * 24 * 60 * 60;
$path = '/';
$domain = "localhost";
setcookie($name, $value, $expire);
if (isset($_COOKIE)) {
echo '<br />Second $_COOKIE<pre>';
print_r($_COOKIE);
echo "</pre>";
}
?>
</body>
</html>
Re: Code to create a cookie in PHP [message #169826 is a reply to message #169825] Mon, 27 September 2010 19:36 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Sep 27, 8:18 pm, MikeB <mpbr...@gmail.com> wrote:
> Now that I can delete cookies, I'd like to create cookies, but this code
> fails to create a cookie. Can anyone help me by telling me what is
> wrong, please?
>
> -MikeB
>
> <?php
>      $name = "headercookie";
>      $value = "MikeB set this in the header";
>      $expire = time() * 24 * 60 * 60;
>      $path = '/';
>      $domain = "localhost";
>      setcookie($name, $value, $expire);
> ?>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
>      <head>
>          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
>          <title> Set cookies</title>
>      </head>
>      <body>
>          <?php
>   if (isset($_COOKIE)) {
>              echo '<br />First $_COOKIE<pre>';
>              print_r($_COOKIE);
>              echo "</pre>";
>          } else {
>              echo "<br />No cookies here!";
>          }
>          Echo "<br />Now setting my own cookie!";
>          $name = "headercookie";
>          $value = "MikeB set this in the body";
>          $expire = time() * 24 * 60 * 60;
>          $path = '/';
>          $domain = "localhost";
>          setcookie($name, $value, $expire);
>          if (isset($_COOKIE)) {
>              echo '<br />Second $_COOKIE<pre>';
>              print_r($_COOKIE);
>              echo "</pre>";
>          }
>          ?>
>      </body>
> </html>

Mike, it's nice that you're keen, but if you slow down a bit and start
from the examples in the manual, changing them step by step, you'll
get on a lot better that just throwing some code down and asking here
when it doesn't work.

Try the Example #1 from

http://uk.php.net/manual/en/function.setcookie.php

and then note that you are multiplying the time() value rather than
adding to it.
Re: Code to create a cookie in PHP [message #169828 is a reply to message #169825] Mon, 27 September 2010 19:55 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/27/2010 3:18 PM, MikeB wrote:
> Now that I can delete cookies, I'd like to create cookies, but this code
> fails to create a cookie. Can anyone help me by telling me what is
> wrong, please?
>
> -MikeB
>
> <?php
> $name = "headercookie";
> $value = "MikeB set this in the header";
> $expire = time() * 24 * 60 * 60;
> $path = '/';
> $domain = "localhost";
> setcookie($name, $value, $expire);
> ?>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
> <title> Set cookies</title>
> </head>
> <body>
> <?php
> if (isset($_COOKIE)) {
> echo '<br />First $_COOKIE<pre>';
> print_r($_COOKIE);
> echo "</pre>";
> } else {
> echo "<br />No cookies here!";
> }
> Echo "<br />Now setting my own cookie!";
> $name = "headercookie";
> $value = "MikeB set this in the body";
> $expire = time() * 24 * 60 * 60;
> $path = '/';
> $domain = "localhost";
> setcookie($name, $value, $expire);
> if (isset($_COOKIE)) {
> echo '<br />Second $_COOKIE<pre>';
> print_r($_COOKIE);
> echo "</pre>";
> }
> ?>
> </body>
> </html>

First of all, a cookie won't be available in the $_COOKIES array until
the browser fetches the next page. It is not available on the page you
set it (unless you already had a cookie with that name stored).

And your second setcookie() call will always fail. You cannot set a
cookie after headers are sent, and ANY output to the browser causes the
headers to be sent. You will see this error if your php.ini file has:

error_reporting = E_ALL // or E_ALL | E_STRICT
display_errors = on

As a side, you are never using the $domain value. The cookie will
belong to the domain you are requesting the page from - if that happens
to be 'localhost', it will belong to localhost.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Code to create a cookie in PHP [message #169829 is a reply to message #169826] Mon, 27 September 2010 20:37 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Captain Paralytic wrote:

> Mike, it's nice that you're keen, but if you slow down a bit and start
> from the examples in the manual, changing them step by step, you'll
> get on a lot better that just throwing some code down and asking here
> when it doesn't work.
>
> Try the Example #1 from
>
> http://uk.php.net/manual/en/function.setcookie.php
>
> and then note that you are multiplying the time() value rather than
> adding to it.

I am *so* sorry for such a dumb mistake. I checked my code when you
pointed this out and realized the expiry time has wrapped as a result of
that mistake. Apart from that it works fine.

COnsider myself chastised and I'll spend more time self-checking my code
before bothering the folks here.

In my defense, may I just say that this whole learning curve thing is
mega-steep. It's not only PHP that I need to understand, it's also HTML,
the whole protocol thing (like sending data in the headers before the
web page is sent), MySQL, Apache, and a host of other things. Sometimes
I just sit here for a couple of days trying to figure it all out. :(
Re: Code to create a cookie in PHP [message #169830 is a reply to message #169828] Mon, 27 September 2010 20:56 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Jerry Stuckle wrote:
> On 9/27/2010 3:18 PM, MikeB wrote:
>> Now that I can delete cookies, I'd like to create cookies, but this code
>> fails to create a cookie. Can anyone help me by telling me what is
>> wrong, please?
>>

> First of all, a cookie won't be available in the $_COOKIES array until
> the browser fetches the next page. It is not available on the page you
> set it (unless you already had a cookie with that name stored).

That's what I understood, I was just making sure. :)

>
> And your second setcookie() call will always fail. You cannot set a
> cookie after headers are sent, and ANY output to the browser causes the
> headers to be sent. You will see this error if your php.ini file has:

Well, this is puzzling. Both the setcookie commands work. I have
verified that both the cookies are created with the firebug/firecookie
extensions in Firefox as well with a little program I've written that
just dumps out the $_COOKIE variable.


>
> error_reporting = E_ALL // or E_ALL | E_STRICT
> display_errors = on

I have both these options as you recommend.

error_reporting = E_ALL | E_STRICT

>
> As a side, you are never using the $domain value. The cookie will belong
> to the domain you are requesting the page from - if that happens to be
> 'localhost', it will belong to localhost.

This is my next challenge. I can't get the cookies set if I include the
"path" and "domain" arguments in the setcookie call. But I'll wrestle
with it for a while on my own - it builds character and doesn't annoy
the group as much... :)

Oh, and I did correct the expiration by adding instead of multiplying as
Capt. P. pointed out.
Re: Code to create a cookie in PHP [message #169832 is a reply to message #169830] Mon, 27 September 2010 22:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/27/2010 4:56 PM, MikeB wrote:
> Jerry Stuckle wrote:
>> On 9/27/2010 3:18 PM, MikeB wrote:
>>> Now that I can delete cookies, I'd like to create cookies, but this code
>>> fails to create a cookie. Can anyone help me by telling me what is
>>> wrong, please?
>>>
>
>> First of all, a cookie won't be available in the $_COOKIES array until
>> the browser fetches the next page. It is not available on the page you
>> set it (unless you already had a cookie with that name stored).
>
> That's what I understood, I was just making sure. :)
>
>>
>> And your second setcookie() call will always fail. You cannot set a
>> cookie after headers are sent, and ANY output to the browser causes the
>> headers to be sent. You will see this error if your php.ini file has:
>
> Well, this is puzzling. Both the setcookie commands work. I have
> verified that both the cookies are created with the firebug/firecookie
> extensions in Firefox as well with a little program I've written that
> just dumps out the $_COOKIE variable.
>
>

Then you are buffering your output, which can be good on a production
system (but is not always). You shouldn't be buffering on a development
system, however, - it hides errors such as this (which have a habit of
showing up at the worst possible time).

I suggest you comment out the output_buffering (or set it to off) in
your development system.

>>
>> error_reporting = E_ALL // or E_ALL | E_STRICT
>> display_errors = on
>
> I have both these options as you recommend.
>
> error_reporting = E_ALL | E_STRICT
>
>>
>> As a side, you are never using the $domain value. The cookie will belong
>> to the domain you are requesting the page from - if that happens to be
>> 'localhost', it will belong to localhost.
>
> This is my next challenge. I can't get the cookies set if I include the
> "path" and "domain" arguments in the setcookie call. But I'll wrestle
> with it for a while on my own - it builds character and doesn't annoy
> the group as much... :)
>
> Oh, and I did correct the expiration by adding instead of multiplying as
> Capt. P. pointed out.
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Code to create a cookie in PHP [message #169839 is a reply to message #169832] Tue, 28 September 2010 00:38 Go to previous message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Jerry Stuckle wrote:
> On 9/27/2010 4:56 PM, MikeB wrote:

>> Well, this is puzzling. Both the setcookie commands work. I have
>> verified that both the cookies are created with the firebug/firecookie
>> extensions in Firefox as well with a little program I've written that
>> just dumps out the $_COOKIE variable.
>>
>>
>
> Then you are buffering your output, which can be good on a production
> system (but is not always). You shouldn't be buffering on a development
> system, however, - it hides errors such as this (which have a habit of
> showing up at the worst possible time).
>
> I suggest you comment out the output_buffering (or set it to off) in
> your development system.
>

Whaddaya know....

; Turning on this setting and managing its maximum buffer size can yield
some
; interesting side-effects depending on your application and web server.
; You may be able to send headers and cookies after you've already sent
output
; through print or echo.
output_buffering = 4096

Who'd a thunk it...

Thanks, I'll check it out ....


That was why I was writing the two different cookie statements, I was
expecting one to fail, just wasn't sure.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: mysql_fetch_array
Next Topic: Route/Link to an action works with http, but NOT with httpS (SSL!)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Oct 20 05:31:05 GMT 2024

Total time taken to generate the page: 0.03263 seconds