PDA

View Full Version : Can PHP extract information from a session cookie?


magicka
10-20-2002, 03:40 AM
I have been trying to develop a unique sort of shopping cart. The base code on the HTML pages is in JavaScript. The whole thing was originally written to later post to formmail.cgi. That's all fine and well except for a few things.

1. The entire output is going to be emailed the same recipient.

2. Parts of the output is going to be efaxed to different establishments depending on the contents being efaxed.

Because of these particular customizations and due to the fact that I am more familiar with PHP than I am with Perl, I have decided to write a PHP script to take all the information passed to it from the order page, compose the individual sets of information and then send them to the appropriate recipients.

Since the JavaScript uses session cookies to store the orders, I need to know if PHP can extract information from a session cookie. If not, I will need to scrap the shopping cart and start afresh.

If it can extract information from a session cookie, I need to know how I can take the row items and assign them to variables. I used to have a book on PHP, however, I moved two months ago and I lost the book. (It's buried in a box somewhere in the garage.) So, I can't look up the answer for myself or see any sample code.

I would appreciate any thoughts or suggestions. Thanks to you all in advance.

Bawy
10-20-2002, 08:00 AM
JavaScript does not write to session cookies, javascript writes to ordinary cookies residing on the client. PHP can write ordinary client cookies or session cookies residing on the server. Since you can't view files on client machines with PHP, I don't think you can read JavaScript cookies, perhaps someone else may know of a workaround, it is not written in stone.

A simpler solution is to have JavaScript and PHP transfer the info back and forth as needed. JavaScript can call a PHP page in the background with variables appended to the URL (or write hidden input tags with DHTML and wait for user to submit a form) to transfer data to PHP. PHP can simply write the JavaScript variables as needed at the head of the JavaScript, for example:

<script language="JavaScript"><!--
itemcount=<?
$itemcount=count($array_of_items);
echo $itemcount;
?>;
if(itemcount>0) {
document.write("You have "+itemcount+" items in your cart");
} else {
document.write("cart is empty");
}
//--></script>

magicka
10-20-2002, 02:41 PM
If that were true, then the JS shopping cart that I am trying to adapt for this purpose is an impossibility. In all the times I have tested its operation, never once did it save a permanent cookie to my computer. It uses cookies that exist only in the browser's memory; when the browser is closed, the cookie disappears. This, by definition, is a session cookie. Therefore, JavaScript can write to session cookies as this shopping cart is based solely on this operation.

So, my original question still stands: Can PHP extract information from a session cookie and if so, how?

Thanks again.

firepages
10-20-2002, 02:55 PM
cookies are cookies fullstop , how they are used and how session state is maintained is another matter, sessions require a server-side component to store session data in the case that the user has cookies disabled, then the session id is passed from page to page by appending the session ID via GET or POST.

So regardless of whether you are simulating sessions or not Bawy is right in that javascript (client-side at least) can not set 'session cookies'

But assuming you are setting javascript cookies ,then the cookie data is available from PHP and vice-versa so if you have set a cookie via javascript called say 'my_cookie' then PHP can access that cookie via

echo $_COOKIE['my_cookie'];

and if you setcookie() in PHP then javascript can read it (assuming the same domain & path etc)

magicka
10-20-2002, 03:13 PM
Thank you for your post firepages. At the very least, I now know that PHP can read cookie info.

Where can I get sample code that will enable me to assign variables for the contents of a cookie?

Here is the JS code that sets the cookie info... the cookie I would later want PHP to read and work with:

function buyItem(newItem, newPrice, newQuantity, newOption) {
if (newQuantity <= 0) {
rc = alert('Zero is not a quantity.');
return false;
}
if (newQuantity >= 0) {
index = document.cookie.indexOf("MyCart");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
top.frames[3].location = "basket.htm";
}
document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newItem+","+newPrice+"^"+newOption+"~"+newQuantity+"]";
}}

Thanks again for your help firepages.