Solved

PHP Cookies Wen Post to New Form

Posted on 2004-10-06
9
212 Views
Last Modified: 2006-11-17
HI. I have the following code in each of to forms, one of which posts to the other:

<?php
if(!isset($_COOKIE['ej_saveabuck_CartID'])) {
 $CartID = md5(uniqid(rand()));
  setcookie("ej_saveabuck_CartID", $CartID, time() + 31536000);
} else {
   $CartID = $_COOKIE['ej_saveabuck_CartID'];
}
?>

When I stick to the first page, doing posts to itself to calculate totals and such, it's fine. But when I post to the second page, it has a whole different cookie value which has nothing in the database.

Help!!!!
0
Comment
Question by:ejoan
  • 5
9 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12239457
Code looks ok, so browser may be rejecting cookies, check your security settings in your browser.

also, add this code for debugging to check whether it is set or not on the next page.

echo (isset($_COOKIE['ej_saveabuck_CartID'])) ? "cookie is set" : "cookie is NOT set";
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12239510
Just tested to double check and the code is fine

if(!isset($_COOKIE['ej_saveabuck_CartID'])) {
 $CartID = md5(uniqid(rand()));
  setcookie("ej_saveabuck_CartID", $CartID, time() + 31536000);
} else {
   $CartID = $_COOKIE['ej_saveabuck_CartID'];
}

outputting the contents with:

echo $_COOKIE['ej_saveabuck_CartID'];

returns the same cookie value for both pages.


You do have that full code on both pages right? otherwise you might be accidently overwriting the value is the isset check is not present second time around.
0
 

Author Comment

by:ejoan
ID: 12239659
Yes, the exact same code.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Diablo84
ID: 12239685
did you check your browser security level?

if it it set to a higher level it may be blocking your cookie so on the second form when the conditional statement is parsed it will regard the cookie as not having been set and so will generate a new cart id and will try to set a new cookie.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 12239713
You could instead use sessions to store the ID, although if its a cookie issue we might come back to the same problem

session_start(); //this must be at top of script

if (!isset($_SESSION['cartID'])) {
 $CartID = md5(uniqid(rand()));
 $_SESSION['cartID'] = $CartID;
} else {
   $CartID = $_SESSION['cartID'];
}

Access the ID using $_SESSION['cartID']
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12239793
If your error reporting level has been turned down you can also add the following to the top of your script for debugging purposes so you can see any problematic errors:

error_reporting(E_ALL ^ E_NOTICE);
0
 

Assisted Solution

by:NYstuff
NYstuff earned 250 total points
ID: 12242168
Is the second page in the same directory and path as the the first page,

for example

http://test.com/program.php  posts to itself then you try to get the cookie at http://www.test.com/program.php

this may not work properly since the cookie was originally set under http://test.com/program.php (no www)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now