Forum Moderators: coopster

Message Too Old, No Replies

PHP Include and Cookies

How to maintain cookie functionality on an included file

         

geckofuel

1:12 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



If you set up a PHP wrapper for an HTML file that uses cookies, how do you get the cookie to persist through the include without being sent to the server?

Here's the deal (this is purely hypothetical):

Say you've got a file called "wrapper.php" which includes a file called "isendacookie.html"

It is clear that isendacookie.html gets called from my webserver (from wrapper.php) rather than my workstation. So where does the cookie get sent?

I've been having trouble retaining full cookie functionality through this process. Any good ideas on how to handle the problem? Unfortunately, I'm not able to recode so that wrapper.php sends the cookie instead.

grahamstewart

2:11 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I entirely understand the problem. What do you mean by a PHP wrapper? Whats the basic structure of the file? How does the HTML file set a cookie (by Javascript maybe)?

Some (short) code snippets might help.

Fischerlaender

7:05 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



I think your solutions depends on the circumstances. Is your isendacookie.html accessed through an HTTP connection or via your file system? If it's on your (local) filesystem there is of course no cookie header sent anywhere by the HTML file. If it's on a remote computer and is accessed via HTTP, then it depends on how your HTML file creates the cookie. I assume it is created with JavaScript, then there should be no cookie set if the file is fetched with file(), because the cookie only gets set, if the user agent is executing the JavaScript.

DrDoc

7:26 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If a cookie is retrieved through JavaScript, and you later want to use that information in an included PHP file... I'm afraid that isn't possible. The PHP is executed before the page loads, whereas the JavaScript is not executed until the page is loading on the client side.

To achieve cookie functionality in the PHP script the cookie has to be read by the script. So, you could include some nifty code snippet first in your file to retreive the cookie and set the applicable variables. If you then want to use some of that info in a JavaScript as well, you just have to echo it to the JS variable.

var something = <?php echo $myvar;?>;