Here is how to use COOKIES in S-mart

greenspun.com : LUSENET : S-Mart Shopping Cart : One Thread

After a long debate on using REMOTE_HOST or REMOTE_ADDR, neither of which completely fixes the aol problems, I've implemented the use of cookies into the script. For all of you that are wondering, here's how you do it:

Near the top of the script, add the following line: require 'cookie.lib'; (I put this line right below my require smart.cfg line)

This tells the script to use the cookie libraries. The file cookie.lib is readily available on the internet. Search for it and you should be able to find it by file name, otherwise send me a message and I'll drop you a copy.

Next, I just added the following at the bottom of the SUB GET_HOST section:

# Here is the START of the cookie section # If there already is a count cookie, proceed without setting a new one. if (&GetCookies('userid')) { # set cookie content $cookiecontent = $Cookies{'userid'}; }

# Otherwise, if the use didn't already have a cookie, give them one! else { $useridnum = time() . "-" . $$; # Set a new cookie. &SetCookies('userid', $useridnum); $cookiecontent = $useridnum; }

$reffile = "$tmpdir$delim$storename-$cookiecontent";

# This is the END of the cookie section

Just cut and paste that into the script and you will be up and running with cookies - since the $reffile is set in the sub get_host section which runs before anything else runs, the entire script is running using that userid and never even needs to look at the remote_host or remote_addr. Took a few hours to figure everything out and learn how cookies work but it was well worth the time.

The userid was generated based on the same system how the script can automatically create an order id number.

Hope this helps some of you out who have been struggling with the AOL proxy server issue! By cutting and pasting the above text into your script you should be up and running with cookies in a matter of minutes!

Take care

-BP

-- BP (bppilot@aol.com), August 04, 1999

Answers

I have tried this in many different ways. I can not get it to work. I tested it by setting my browser to prompt for cookies. I never get a prompt. I do however see "Set-Cookie: userid=934643960-18983;" at the very bottom of the footer on the buy1 and buy2 pages. This number changes between the 2 pages and will also change if you refresh the page.

I'm assuming I have the code in the wrong area of the script. I placed it after the last } of the sub get_host routine and before the next sub-routine.

Just an FYI, the server the script is running on is Apache 1.3 and the secure server is Netscape Enterprise. I also have the REMOTE_HOST changed to REMOTE_ADDR and my header file uses the secure server to get the images.

Any help is a big welcome.

-- Dave Anderson (danders@speedex.net), August 14, 1999.


I have implemented cookies with my cart. If you are interested to know more, email me at patrickccf@hotmail.com.

--
God bless!

-- Patrick Chan (patrickccf@hotmail.com), March 20, 2000.


Hi BP,

I've been trying to implement cookies the way that you describe but to no avail. I fear that I must have manged to bodge the script in placing it. I have tried placing the cookie.lib in various different spots and and changing the permissions but no joy. All I get is the document contains no data message when viewed from netscape. Any idea where I'm going wrong?

Thanks for your help

Rufus

-- Rufus Biggs (rufus.biggs@m-audio.co.uk), June 06, 2000.


I'd just like to update my previous comment. I had got the layout a bit muddled and now the script runs again, but I am getting the same problems as Dave described. Has anyone come across this and solved what it is?

Rufus

-- Rufus Biggs (rufus.biggs@m-audio.co.uk), June 06, 2000.


I'd just like to say thanks to Patrick Chan who sent me indstuctions for modifying the code to work with cookies. If there is anyone else out there struggling to sort this out I'd recommend dropping himan email. Many thanks Patrick.

Rufus

-- Rufus Biggs (rufus.biggs@themcorporation.com), June 08, 2000.



You have to put it below the host and reffile definitions and then put the code above BETWEEN the reffile line and the closing bracket }.

If you do this, this thing works for ALL browsers that I've seen -- and it does install the session cookie.

Here's what I have.

# Get the hostname, for file reference sub get_host { $host = $ENV{'REMOTE_ADDR'}; $reffile = "$tmpdir$delim$storename-$host";

# Here is the START of the cookie section # If there already is a count cookie, proceed without setting a new one. if (&GetCookies('userid')) { # set cookie content $cookiecontent = $Cookies{'userid'}; } # Otherwise, if the use didn't already have a cookie, give them one! else { $useridnum = time() . "-" . $$; # Set a new cookie. &SetCookies('userid', $useridnum); $cookiecontent = $useridnum; } $reffile = "$tmpdir$delim$storename-$cookiecontent"; } # This is the END of the cookie section

And do NOT forget the final bracket. Also, you obviously don't have a final bracket below the line

$reffile = "$tmpdir$delim$storename-$host";

because the sub-routine continues (by putting in the cookie -- lol).

check it out and you'll see

Thanks BP, by the way. Helped a whole lot!

-- The "fix" works very well but (alfredo@people-link.net), July 13, 2000.


Am i bein' stoopid? For some reason, i can't get my script to work. I cut n' paste exactly what was listed but to no avail. Any help appreciated

-- Jennifer Devera (jdevera@pcmagic.net), August 24, 2000.

Hi, BP or Patrick Chan can you help me set up the "cookie.lib"

Thanks, Jim

-- James L. Farmer (jim@team-blankets.com), June 13, 2001.


BP,

Nicely done! This seems to work just fine.

-- Don Thompson (gogomagazine@aol.com), November 28, 2002.


Moderation questions? read the FAQ