Re: Code to create a cookie in PHP [message #169832 is a reply to message #169830] |
Mon, 27 September 2010 22:34 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
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
==================
|
|
|