Solved

PHP Cookies Wen Post to New Form

Posted on 2004-10-06
9
214 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array_values - reorder after unset? 5 21
How to count in a table in php 22 35
tutorial for ebay api 3 34
Php recording post 4 40
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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