Solved

PHP Cookies Wen Post to New Form

Posted on 2004-10-06
9
203 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, the exact same code.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

by:Diablo84
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

7 Experts available now in Live!

Get 1:1 Help Now