Solved

PHP Cookies Wen Post to New Form

Posted on 2004-10-06
9
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 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…

710 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