The following code works with register_globals=On. What do I have to change to make it work with register_globals=Off. Of course I would like the code to be independent of registers_globals On or Off. I have PHP V4.2 on the testing server? Any assistance would be appreciated by this 70 year old man attempting to learn PHP.
Chas...........
<?php
if(!isset($COOKIE_SET))
{
setcookie("whichPage","$page");
header ("Location: $PHP_SELF?COOKIE_SET=1");
exit;
};
?>
<html>
<head>
<title>CookieWorks with RG On</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Testpage for index.php</p>
<p><a href="cookieworks.php?page=one">Link 1</a></p>
<p><a href="cookieworks.php?page=two">Link 2</a></p>
<p><a href="cookieworks.php?page=three">Link 3</a></p>
<p>page =<?php print "$whichPage"; ?></p>
<p><?php
if(isset($_COOKIE['whichPage']))
{
include "$whichPage.htm";
exit;
};
?></p>
</body>
</html>

    When you use the $_COOKIE superglobal array, the code will work whether or not register_globals is set to On or Off.

      You'll also want to use $GET['COOKIE_SET'] instead of $COOKIE_SET; and $SERVER['PHP_SELF'] instead of $PHP_SELF.

        Now I get this:
        Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/chas/public_html/Cookie_Test/cookieone.php on line 5
        When line 5 is now this:
        header ("Location: $_SERVER['PHP_SELF']?COOKIE_SET=1");

        I'll admit that I am new to PHP but I have read a lot of the documentation. I think my problem, other than age (70)', is that I come from many years as a 'Fortran' programmer background. 26 years working on the SR-71 Elint collection processing and have no experience with other programming languages. Being retired I started taking a WEB class at the local Jr college. After hearing and reading about PHP I am attempting to include some PHP code in to learn more about cookies. I have spent many hours trying to learn PHP from the docs but I find most of my knowledge comes from disecting examples. I apologize if I have offended anyone with what to some are basic questions.

        Chas...........

          There are several ways:

           header ("Location: {$_SERVER['PHP_SELF']}?COOKIE_SET=1");
           header ("Location: $_SERVER[PHP_SELF]?COOKIE_SET=1");  
          header ("Location:".$_SERVER['PHP_SELF']."?COOKIE_SET=1");

          Use curly braces, don't use single quotes or concatenate the strings.

          Thomas

          P.S.: I don't think that anyone is offended by your posts ... so .. I'm rather impressed)

            I spent another 2 days trying to sort it out but couldn't get it to work. All I am attempting to do is pass a cookie to the originating page with register_globals Off. I have shortened the code and implemented the suggestions but to no avail. Any help is appreciated. reg_globals=On it works, reg_globals-Off doesn't work. I have no more hair to fall out...........Anyone out there that can make it work?

            <?php
            if(!isset($GET['COOKIE_SET'] ))
            {
            setcookie("whichPage","$page");
            header ("Location:".$
            SERVER['PHP_SELF']."?COOKIE_SET=1");

            }
            ?>
            <html>
            <head>
            <title>CookieWorks with RG Off</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            </head>

            <body>
            <p>Testpage for index.php r_g = Off</p>
            <p><a href="cookieone.php?page=one">Link 1</a></p>
            <p>Cookies = <?php print_r ($_COOKIE); ?></p>
            <p>whichPage = <?php print ($page); ?></p>

            </body>
            </html>

              You should use $_GET['page'] instead of $page.

                I've tried $_GET['page'] instead of $page but it still doesn't work. Is anyone out thee running with register_globals=Off that is able to pass cookies successfully?
                chas............

                  The following test script worked on my server with register_globals=Off:

                  <?php
                    // get register_globals, PHP_SELF and page stored in cookie
                    $blnGlobals = ((bool)ini_get("register_globals")) ? "On" : "Off";
                    $strSelf    = $_SERVER['PHP_SELF'];
                    $strPage    = isset($_COOKIE['whichPage']) ? $_COOKIE['whichPage'] : "none";
                  
                    // delete cookie
                    if (isset($_GET['delcookie']) && $_GET['delcookie'] === 'true') {
                      setcookie("whichPage","",time()-3600);
                      header("Location: {$_SERVER['PHP_SELF']}");    
                  } // change cookie if GET parameter page exists if (isset($_GET['page'])) { setcookie("whichPage",$_GET['page']); header("Location: {$_SERVER['PHP_SELF']}"); } ?> <html> <head> <title>CookieWorks with RG Off</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <p>Testpage for index.php r_g = <?PHP echo $blnGlobals; ?></p> <p><a href="<?PHP echo $strSelf; ?>?page=one">Link one</a></p> <p><a href="<?PHP echo $strSelf; ?>?page=two">Link two</a></p> <p><a href="<?PHP echo $strSelf; ?>?page=three">Link three</a></p> <p><a href="<?PHP echo $strSelf; ?>?delcookie=true">delete cookie</a></p> <pre>Cookies = <?php print_r ($_COOKIE); ?></pre> <p>whichPage = <?php echo $strPage; ?></p> <?PHP // display link if cookie exists if (isset($_COOKIE['whichPage'])) { echo "<p>link with actual cookie: <p><a href=\"$strSelf?page=$strPage\">Link $strPage</a></p>\n"; } ?> </body> </html>

                  EDIT: I added a "delete cookie" link.

                  Thomas

                    Write a Reply...