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
Solved

PHP Cookies Wen Post to New Form

Posted on 2004-10-06
9
220 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 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