I’m an experienced programmer (since 1974!), who’s fairly new to working with advanced web techniques (I know my way around HTML
, basic PHP
, and MYSQL
). I’ve never worked with cookies before, and I’m stumped as it seems I’m having problems reading cookies I’ve set.
In this case, this page is recursively called to process and display a form. The first time the user displays the page, I look for a cookie (and there shouldn't be one). User fills out the form and submits it. The form action calls the page again, and I look for a cookie. Still isn't one, but there’s $_POST
data, so I set the cookie for the user (this part works). If the user returns to the page after submitting a form, there should be a cookie, and I use the cookie value to look up the user and populate identifying information in the form.
Writing the cookie works. However, when I access this page again, the cookie is blank.
I checked the content settings in the browser (Chrome); the cookie is there, and has the required information (and, hasn’t expired). Using the console function in the browser for the page, I see all my cookies from this domain, but I can’t access the specific one for this program. I’ve deleted the cookie, gone through the routine a few times to make sure that the cookie is rewritten, and still no cookie on the next access. I’ve deleted the cookie, flushed cache in the browser, and tried again without success. I've closed the browser, restarted it, and still don't receive the cookie. I’ve checked the php.ini file to make sure that the variables_order flag is set properly (explicit EGPCS
). I searched through the previous questions and didn’t see anything that looks like what’s going on here (most are JS
related, or have to do with setting the path in the cookie).
I added some echo statements after the <html>
tag to show me the state of cookie processing, and it’s always indicating that $_COOKIE
is blank (or non-existent).
So, there’s something I’ve overlooked in the code. Maybe someone can see what I’m not.
Here’s the code (I used straight PHP
since my JS-fu
is weak):
<?php
//make sure I display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//cookie routine
//initialize variables
$cookie_name="SHS74"; //self-explanatory
$cookie_set=FALSE; //flag to help debugging
$returning=FALSE; //FALSE = no cookie, TRUE=cookie has been read
$returned_user=""; //value of the cookie
//actual routine
if(!isset($_COOKIE['$cookie_name'])) { //user has never submitted anything
if (isset($_POST['email_from'])) { //but has submitted a form this time – required field
setcookie ($cookie_name, $_POST['email_from'], time()+3600*24*30, '/'); //works
$returning=FALSE; //so I don’t look for database records for this user
$cookie_set=TRUE; //flag to help debugging
} // end of if $_POST[ email_from] - note: if not isset $_POST[‘email_from’], I can safely fall through since flags are already initialized to FALSE
} //if not isset
else { //the user has a cookie
$returned_user = $_COOKIE['$cookie_name']; //who the user is
$returning=TRUE; //user has been here before, submitted a form, and has a database record
} //end else
//database initialization
session_start();
include("connection.php");
?>
<!DOCTYPE HTML>
<html>
<head>
[…snip…]
(yeah, really old school)
So, what am I doing wrong? Is it a configuration error I’ve missed, or did I screw up the handling of $_COOKIE
in the beginning?
Thanks!
10-Aug-2018 11:00edt - as suggested, I removed the single quotes from around $cookie_name in the test of 'isset' and everywhere else I referenced $_COOKIE. It worked! Thanks. Yes, this is all new to me--my prior web building experience has been very basic - no array handling, no cookies, so I'm on a steep learning curve. Thanks everyone who offered help!