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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: joe at kybert dot com
New email:
PHP Version: OS:

 

 [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: Thu Apr 25 09:01:29 2024 UTC