dqqvravff05370501 2018-08-10 12:45
浏览 27

网页无法识别cookie

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答