php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25929 cannot read cookies within included file
Submitted: 2003-10-20 19:59 UTC Modified: 2003-10-22 01:55 UTC
From: joe at kybert dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.2 OS: XP (debug server)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
44 + 38 = ?
Subscribe to this entry?

 
 [2003-10-20 19:59 UTC] joe at kybert dot com
Description:
------------
Cookie is not readable across multiple files.

I have 2 files, file 1 sets a cookie (refresh file after 1st exe so cookie is sent)

it then includes file 2, which reads and displays the cookie.

Both files are in the same folder on the server, which is
<servername>/test/<filenames>
.

File 2 fails to read the cookie!

Reproduce code:
---------------
file1.php contains:

<?php
setcookie("test", "set_by_file_1", time()+3600, "/", $_SERVER['HTTP_HOST']);
echo "file 1 cookies: ";
print_r($_COOKIE);
include("http://$SERVER_NAME/test/file2.php");
?>

file2.php contains:

<?php
echo "<br>file 2 cookies: ";
print_r($_COOKIE);
?>


Expected result:
----------------
file 1 cookies: Array ( [LastUser] => ujoe [test] => set_by_file_1 ) 
file 2 cookies: Array ( [LastUser] => ujoe [test] => set_by_file_1 )

Actual result:
--------------
file 1 cookies: Array ( [LastUser] => ujoe [test] => set_by_file_1 ) 
file 2 cookies: Array ( ) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-21 15:17 UTC] sniper@php.net
If you include REMOTE file, of course the cookie is not passed to it.

 [2003-10-21 16:05 UTC] joe at kybert dot com
Why not? The remote file is on the same server, and the cookie was set to be accessable to all files from that domain.

If you dont include it this way, you cannot pass arguments to the included file, this doesnt work:

include("{$_SERVER['DOCUMENT_ROOT']}/test/file2.php?myparam=hello");

joe
#
 [2003-10-21 16:38 UTC] jay@php.net
I assumed from your first post that when you were 
including the file through http, you weren't parsing on 
the remote server. (I've seen similar set ups before. Not 
recommended, though, or necessary in virtually all cases.) 
 
You can't see the cookie in $_COOKIE from the second file 
because it's not being requested from the machine you see 
the cookie on, but rather the script that does the 
include. If you requested the file as source rather than 
parsed so that it would be parsed via the first file, it 
would probably work. 
 
Your second example of passing arguments to the second 
file won't work either because you can't pass arguments to 
a script that way. Just make some variables before 
including the file, the second file will see them. 
 
J 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC