$_COOKIE variable is always empty

Hi,

When I run the following in PHP then two cookies are set.

ini_set('session.name', 'session-1');
session_start();
setcookie('session-2', '');


Both cookies are being sent to the client, and then to the server (I am
sure), but they are not available in $_COOKIE (this array is empty).

Why and how can the $_COOKIE variable be empty - this is getting
frustrating. Am I missing something? All other global variables are
available normally, only $_COOKIE is always empty.

(ps. I am viewing the page through plesk site preview over https, and
have tried bot secure and normal)


kind regards, Seansan


Please find ini settings through phpinfo below.

Directive Local Value Master Value
allow_call_time_pass_reference Off Off
allow_url_fopen On On
always_populate_raw_post_data Off Off
arg_separator.input & &
arg_separator.output & &
asp_tags Off Off
auto_append_file no value no value
auto_prepend_file no value no value
browscap no value no value
default_charset no value no value
default_mimetype text/html text/html
define_syslog_variables Off Off
disable_classes no value no value
disable_functions no value no value
display_errors Off Off
display_startup_errors Off Off
doc_root no value no value
docref_ext no value no value
docref_root no value no value
enable_dl On On
error_append_string no value no value
error_log backend/logs/errors no value
error_prepend_string no value no value
error_reporting 2047 2047
expose_php On On
extension_dir /usr/lib/php4 /usr/lib/php4
file_uploads On On
gpc_order GPC GPC
highlight.bg #FFFFFF #FFFFFF
highlight.comment #FF8000 #FF8000
highlight.default #0000BB #0000BB
highlight.html #000000 #000000
highlight.keyword #007700 #007700
highlight.string #DD0000 #DD0000
html_errors On On
ignore_repeated_errors Off Off
ignore_repeated_source Off Off
ignore_user_abort Off Off
implicit_flush Off Off
include_path .: .:
log_errors On On
log_errors_max_len 1024 1024
magic_quotes_gpc Off Off
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
max_execution_time 30 30
max_input_time 60 60
memory_limit 32M 32M
open_basedir /var/www/vhosts/domain.nl/httpdocs:/tmp no value
output_buffering no value no value
output_handler no value no value
post_max_size 8M 8M
precision 14 14
register_argc_argv On On
register_globals On Off
report_memleaks On On
safe_mode On Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
sendmail_from no value no value
sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
serialize_precision 100 100
short_open_tag On On
SMTP localhost localhost
smtp_port 25 25
sql.safe_mode Off Off
track_errors Off Off
unserialize_callback_func no value no value
upload_max_filesize 2M 2M
upload_tmp_dir no value no value
user_dir no value no value
variables_order EGPCS EGPCS
xmlrpc_error_number 0 0
xmlrpc_errors Off Off
y2k_compliance On On
Seansan [ Sa, 03 März 2007 22:59 ] [ ID #1646569 ]

Re: $_COOKIE variable is always empty

Seansan wrote:
> Hi,
>
> When I run the following in PHP then two cookies are set.
>
> ini_set('session.name', 'session-1');
> session_start();
> setcookie('session-2', '');
>
>
> Both cookies are being sent to the client, and then to the server (I am
> sure), but they are not available in $_COOKIE (this array is empty).
>
> Why and how can the $_COOKIE variable be empty - this is getting
> frustrating. Am I missing something? All other global variables are
> available normally, only $_COOKIE is always empty.
>
> (ps. I am viewing the page through plesk site preview over https, and
> have tried bot secure and normal)
>
>
> kind regards, Seansan
>
>
> Please find ini settings through phpinfo below.
>
> Directive Local Value Master Value
> allow_call_time_pass_reference Off Off
> allow_url_fopen On On
> always_populate_raw_post_data Off Off
> arg_separator.input & &
> arg_separator.output & &
> asp_tags Off Off
> auto_append_file no value no value
> auto_prepend_file no value no value
> browscap no value no value
> default_charset no value no value
> default_mimetype text/html text/html
> define_syslog_variables Off Off
> disable_classes no value no value
> disable_functions no value no value
> display_errors Off Off
> display_startup_errors Off Off
> doc_root no value no value
> docref_ext no value no value
> docref_root no value no value
> enable_dl On On
> error_append_string no value no value
> error_log backend/logs/errors no value
> error_prepend_string no value no value
> error_reporting 2047 2047
> expose_php On On
> extension_dir /usr/lib/php4 /usr/lib/php4
> file_uploads On On
> gpc_order GPC GPC
> highlight.bg #FFFFFF #FFFFFF
> highlight.comment #FF8000 #FF8000
> highlight.default #0000BB #0000BB
> highlight.html #000000 #000000
> highlight.keyword #007700 #007700
> highlight.string #DD0000 #DD0000
> html_errors On On
> ignore_repeated_errors Off Off
> ignore_repeated_source Off Off
> ignore_user_abort Off Off
> implicit_flush Off Off
> include_path .: .:
> log_errors On On
> log_errors_max_len 1024 1024
> magic_quotes_gpc Off Off
> magic_quotes_runtime Off Off
> magic_quotes_sybase Off Off
> max_execution_time 30 30
> max_input_time 60 60
> memory_limit 32M 32M
> open_basedir /var/www/vhosts/domain.nl/httpdocs:/tmp no value
> output_buffering no value no value
> output_handler no value no value
> post_max_size 8M 8M
> precision 14 14
> register_argc_argv On On
> register_globals On Off
> report_memleaks On On
> safe_mode On Off
> safe_mode_exec_dir no value no value
> safe_mode_gid Off Off
> safe_mode_include_dir no value no value
> sendmail_from no value no value
> sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
> serialize_precision 100 100
> short_open_tag On On
> SMTP localhost localhost
> smtp_port 25 25
> sql.safe_mode Off Off
> track_errors Off Off
> unserialize_callback_func no value no value
> upload_max_filesize 2M 2M
> upload_tmp_dir no value no value
> user_dir no value no value
> variables_order EGPCS EGPCS
> xmlrpc_error_number 0 0
> xmlrpc_errors Off Off
> y2k_compliance On On

I always see "This is your first visit"

<?php
header();

if ($_COOKIE['test'] != "hi") {
setcookie('test', 'hi');
echo "This is your first visit";
}

elseif ($_COOKIE['test'] == "hi") {
echo "we found the cookie! You have been here before";
}

?>
Seansan [ Sa, 03 März 2007 23:15 ] [ ID #1646570 ]

Re: $_COOKIE variable is always empty

Seansan wrote:
> Hi,
>
> When I run the following in PHP then two cookies are set.
>
> ini_set('session.name', 'session-1');
> session_start();
> setcookie('session-2', '');
>
>
> Both cookies are being sent to the client, and then to the server (I am
> sure), but they are not available in $_COOKIE (this array is empty).
>
> Why and how can the $_COOKIE variable be empty - this is getting
> frustrating. Am I missing something? All other global variables are
> available normally, only $_COOKIE is always empty.

Even if sessions many times are cookies, they don't use the $_COOKIE
array, but $_SESSION, as you want to be able to access a session the
same way no matter if it's embedded into url or as a cookie.

I suggest you take a look at

http://www.php.net/manual/en/ref.session.php
http://www.php.net/manual/en/function.session-start.php

to see how session works


setcookie() may require you to specify all the arguments, seems to be a
long lived msie bug that makes it to not save a cookie and also keep in
mind that you can't access a cookie value until the script after the one
where you created (the same applies to value changes).


--

//Aho
Shion [ So, 04 März 2007 00:04 ] [ ID #1646571 ]

Re: $_COOKIE variable is always empty

J.O. Aho wrote:
> Seansan wrote:
>> Hi,
>>
>> When I run the following in PHP then two cookies are set.
>>
>> ini_set('session.name', 'session-1');
>> session_start();
>> setcookie('session-2', '');
>>
>>
>> Both cookies are being sent to the client, and then to the server (I am
>> sure), but they are not available in $_COOKIE (this array is empty).
>>
>> Why and how can the $_COOKIE variable be empty - this is getting
>> frustrating. Am I missing something? All other global variables are
>> available normally, only $_COOKIE is always empty.
>
> Even if sessions many times are cookies, they don't use the $_COOKIE
> array, but $_SESSION, as you want to be able to access a session the
> same way no matter if it's embedded into url or as a cookie.
>
> I suggest you take a look at
>
> http://www.php.net/manual/en/ref.session.php
> http://www.php.net/manual/en/function.session-start.php
>
> to see how session works
>
>
> setcookie() may require you to specify all the arguments, seems to be a
> long lived msie bug that makes it to not save a cookie and also keep in
> mind that you can't access a cookie value until the script after the one
> where you created (the same applies to value changes).
>
>

Thank you for your answer. setcookie() has been called correctly and
with all the arguments. (this was just a simple example).

The cookies are stored on the client, and also sent to the server. I
have tested with firefox and IE. There is something happening serverside
that either changes the COOKIE value, doesnt allow it to be set, of has
another hash where it is stored.

Any more ideas?
Seansan [ So, 04 März 2007 00:23 ] [ ID #1646572 ]

Re: $_COOKIE variable is always empty

Seansan <sean115=3Dat [at] =3Dreeve.nl> wrote:
>>> When I run the following in PHP then two cookies are set.
>>>
>>> ini_set('session.name', 'session-1');
>>> session_start();
>>> setcookie('session-2', '');
>>>
>>> Both cookies are being sent to the client, and then to the server (I=
am
>>> sure), but they are not available in $_COOKIE (this array is empty).=

>Thank you for your answer. setcookie() has been called correctly and =

> with all the arguments. (this was just a simple example).
>
> The cookies are stored on the client, and also sent to the server. I =

> have tested with firefox and IE. There is something happening serversi=
de =

> that either changes the COOKIE value, doesnt allow it to be set, of ha=
s =

> another hash where it is stored.

First of all, just to make sure, you do know that it will only be in the=
=

$_COOKIE array on a second or later request?

Secondly, I'd like to know how you are so sure the cookie is sent back. =
=

How did you check that? (FF->LiveHTTPHeaders, MSIE->Fiddler ?)

On Apache, you can check what request headers make it to the script with=
=

apache_request_headers(), is the cookie header there?
-- =

Rik Wasmus
Rik [ So, 04 März 2007 00:39 ] [ ID #1647140 ]

Re: $_COOKIE variable is always empty

Rik wrote:
> Seansan <sean115=at [at] =reeve.nl> wrote:
>>>> When I run the following in PHP then two cookies are set.
>>>>
>>>> ini_set('session.name', 'session-1');
>>>> session_start();
>>>> setcookie('session-2', '');
>>>>
>>>> Both cookies are being sent to the client, and then to the server (I am
>>>> sure), but they are not available in $_COOKIE (this array is empty).
>> Thank you for your answer. setcookie() has been called correctly and
>> with all the arguments. (this was just a simple example).
>>
>> The cookies are stored on the client, and also sent to the server. I
>> have tested with firefox and IE. There is something happening
>> serverside that either changes the COOKIE value, doesnt allow it to be
>> set, of has another hash where it is stored.
>
> First of all, just to make sure, you do know that it will only be in the
> $_COOKIE array on a second or later request?
>
> Secondly, I'd like to know how you are so sure the cookie is sent back.
> How did you check that? (FF->LiveHTTPHeaders, MSIE->Fiddler ?)
>
> On Apache, you can check what request headers make it to the script with
> apache_request_headers(), is the cookie header there?


> First of all, just to make sure, you do know that it will only be in
the $_COOKIE array on a second or later request?

Yes

> Secondly, I'd like to know how you are so sure the cookie is sent
back. How did you check that? (FF->LiveHTTPHeaders, MSIE->Fiddler ?)

FF->LiveHTTPHeaders (I see request headers)

>
> On Apache, you can check what request headers make it to the script
with apache_request_headers(), is the cookie header there?

apache_request_headers() cookies dont make it to the script.

Rik, I must say that I have found out some more information and it seems
that the PLESK controlpanel is blocking COOKIES. I am using the
sitepreview (php program) and I have just read that globalvars are not
passed. This probably has something to do with it.

http://forums.theplanet.com/index.php?showtopic=54222

Now wish how I can circumvent either through PHP or call the IP address
directly

2 cookies
Name sessauth
Value c41187720babfefe285d3a34e84a7dc2cecbb3b3
Host 85.17.55.35
Path /sitepreview/http/domain.nl/mail/
Secure Yes
Expires At End Of Session

Name sessid
Value 03d181a76be98a1bc8906de63006a608
Host 85.17.55.35
Path /
Secure Yes
Expires At End Of Session
Seansan [ So, 04 März 2007 01:09 ] [ ID #1647141 ]

Re: $_COOKIE variable is always empty

Why are you calling header(); with no params? Try to remove it. try change
} elseif ($_COOKIE['test'] == "hi") { to simple } else {.

Seansan wrote:

> Seansan wrote:
>> Hi,
>>
>> When I run the following in PHP then two cookies are set.
>>
>> ini_set('session.name', 'session-1');
>> session_start();
>> setcookie('session-2', '');
>>
>>
>> Both cookies are being sent to the client, and then to the server (I am
>> sure), but they are not available in $_COOKIE (this array is empty).
>>
>> Why and how can the $_COOKIE variable be empty - this is getting
>> frustrating. Am I missing something? All other global variables are
>> available normally, only $_COOKIE is always empty.
>>
>> (ps. I am viewing the page through plesk site preview over https, and
>> have tried bot secure and normal)
>>
>>
>> kind regards, Seansan
>>
>>
>> Please find ini settings through phpinfo below.
>>
>> Directive Local Value Master Value
>> allow_call_time_pass_reference Off Off
>> allow_url_fopen On On
>> always_populate_raw_post_data Off Off
>> arg_separator.input & &
>> arg_separator.output & &
>> asp_tags Off Off
>> auto_append_file no value no value
>> auto_prepend_file no value no value
>> browscap no value no value
>> default_charset no value no value
>> default_mimetype text/html text/html
>> define_syslog_variables Off Off
>> disable_classes no value no value
>> disable_functions no value no value
>> display_errors Off Off
>> display_startup_errors Off Off
>> doc_root no value no value
>> docref_ext no value no value
>> docref_root no value no value
>> enable_dl On On
>> error_append_string no value no value
>> error_log backend/logs/errors no value
>> error_prepend_string no value no value
>> error_reporting 2047 2047
>> expose_php On On
>> extension_dir /usr/lib/php4 /usr/lib/php4
>> file_uploads On On
>> gpc_order GPC GPC
>> highlight.bg #FFFFFF #FFFFFF
>> highlight.comment #FF8000 #FF8000
>> highlight.default #0000BB #0000BB
>> highlight.html #000000 #000000
>> highlight.keyword #007700 #007700
>> highlight.string #DD0000 #DD0000
>> html_errors On On
>> ignore_repeated_errors Off Off
>> ignore_repeated_source Off Off
>> ignore_user_abort Off Off
>> implicit_flush Off Off
>> include_path .: .:
>> log_errors On On
>> log_errors_max_len 1024 1024
>> magic_quotes_gpc Off Off
>> magic_quotes_runtime Off Off
>> magic_quotes_sybase Off Off
>> max_execution_time 30 30
>> max_input_time 60 60
>> memory_limit 32M 32M
>> open_basedir /var/www/vhosts/domain.nl/httpdocs:/tmp no value
>> output_buffering no value no value
>> output_handler no value no value
>> post_max_size 8M 8M
>> precision 14 14
>> register_argc_argv On On
>> register_globals On Off
>> report_memleaks On On
>> safe_mode On Off
>> safe_mode_exec_dir no value no value
>> safe_mode_gid Off Off
>> safe_mode_include_dir no value no value
>> sendmail_from no value no value
>> sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
>> serialize_precision 100 100
>> short_open_tag On On
>> SMTP localhost localhost
>> smtp_port 25 25
>> sql.safe_mode Off Off
>> track_errors Off Off
>> unserialize_callback_func no value no value
>> upload_max_filesize 2M 2M
>> upload_tmp_dir no value no value
>> user_dir no value no value
>> variables_order EGPCS EGPCS
>> xmlrpc_error_number 0 0
>> xmlrpc_errors Off Off
>> y2k_compliance On On
>
> I always see "This is your first visit"
>
> <?php
> header();
>
> if ($_COOKIE['test'] != "hi") {
> setcookie('test', 'hi');
> echo "This is your first visit";
> }
>
> elseif ($_COOKIE['test'] == "hi") {
> echo "we found the cookie! You have been here before";
> }
>
> ?>
Gootes [ So, 04 März 2007 16:29 ] [ ID #1647146 ]
PHP » alt.php » $_COOKIE variable is always empty

Vorheriges Thema: Using Post with Multiple Checkboxes ?
Nächstes Thema: Q: how can i cache data that took from a txt file before?