SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookies do not seem to work

    I have made a page that sets a cookie that will be read by another page to determine what image should be displayed.

    On the page that sets to cookie, the following code is used to set a cookie.

    PHP Code:
    if ($TotalPages == $Page) {
      
    $CookieExpires time() + 3600;
      
    $CookieName "TopicID" $TopicID;
      
    setcookie("$CookieName""true"$CookieExpires);
      echo(
    "setcookie(\"$CookieName\", \"true\", $CookieExpires);");
      echo(
    "<br>");
      echo(
    "The value of the cookie TopicID1 is " $HTTP_COOKIE_VARS["TopicID1"]);

    The output of the above code is:

    setcookie("TopicID1", "true", 1004510708);
    The value of the cookie TopicID1 is true

    On the page that reads the cookie I have this code:

    PHP Code:
    echo($HTTP_COOKIE_VARS["TopicID1"]); 
    However this page gives me an error:

    Warning: Undefined index: TopicID1 in D:\MC PHP\temp.php on line 2


    What am I doing wrong?

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TopicID1 or TopicID? Check it.

  3. #3
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is TopicID1.

    PHP Code:
    $CookieName "TopicID" $TopicID
    The value of $TopicID is 1 (one), so the cookie is called TopicID1.

    Also, on the page where the cookie is actually set, it is read correctly and the correct information (which is "true") is displayed. It is only on the other page trying to display the cookie where it gives me an error.

  4. #4
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran your codes. It gave me true and no errors. Something is wrong in your second page. Post some codes above and below the "echo http..." line on the second page. In second page echo both lines below and tell me the output:
    echo "topic is". $TopicID1;
    echo ($HTTP_COOKIE_VARS["TopicID1"]);

    ----
    wow, you're off line
    Last edited by johnn; Oct 31, 2001 at 05:15.

  5. #5
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran your code and I got errors in that too.

    echo "topic is ". $TopicID1;
    gave me an undefined variable error

    echo($HTTP_COOKIE_VARS["TopicID1"]);
    gave me an undefined index error.

    I think the $TopicID1 does not work because I edited the php.ini file and turned off the register_globals
    (that's all I changed in the INI file)
    Last edited by Husain; Nov 1, 2001 at 03:12.

  6. #6
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it OK now?

  7. #7
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope! As I said, both the lines of code you asked me try out gave me errors.

  8. #8
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you wrote, maybe you can turn on register global, and try again. If that doesn't work, you can post your code and I may see something wrong with it.

  9. #9
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will post the code, then.

    But one thing I do not understand (I am a beginner - so please pardon my ignorance) is HTTP_*_VARS should work regardless of the settings of register_globals.

    Plus, when I read a variable from the URL using HTTP_GET_VARS, it works! I will try it with forms and see if it works.

    But then, if GET_VARS works, I wonder why COOKIES_VARS doesnt...

  10. #10
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have zipped the code and uploaded it here.

    Hope this is convinent for you..

  11. #11
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hussain,
    I created 2 small scripts: a.php and b.php

    a.php is:
    PHP Code:
    <?

      $CookieExpires 
    time() + 3600;
      
      
    $CookieName "TopicID" '1';
      
    setcookie("$CookieName""true"$CookieExpires);
      echo(
    "setcookie(\"$CookieName\", \"true\", $CookieExpires);");
      echo(
    "<br>");
      echo(
    "The value of the cookie TopicID1 is " $HTTP_COOKIE_VARS["TopicID1"]);
    ?>
    <a href="b.php">bbb</a>
    and b.php (or your temp.php) is
    PHP Code:
    <html>
    <head>
    <title>Testing</title>
    </head>
    <body><table border="1" width="100%">
    <tr>
        <td>
    <?
    $TopicID 
    1;
    $CookieName "TopicID" $TopicID;
    echo(
    $TopicID1);
    echo(
    $HTTP_COOKIE_VARS["$CookieName"]);
    echo(
    "<br>Hope this works!");
    ?>

        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    </table>
    </body>
    </html>
    <?
    //echo "dd". $TopicID1;
    //echo ($HTTP_COOKIE_VARS["TopicID1"]);
    ?>
    and here's the output:
    setcookie("TopicID1", "true", 1004780755);
    The value of the cookie TopicID1 is true bbb

    Click on bbb link and it gives this:
    truetrue
    Hope this works!

    Try the above scripts and see if you have the same result.

  12. #12
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is interesting... the code you asked me to try out works.

    However, the echo($TopicID1); statement does not work because I have disabled the global vars setting. I enabled the setting from the INI file and it worked too.

    So now, coming back to my original code, I went through it again and compared my code with yours: I can not find my mistake. Can you tell me what my mistake is? Thanks

  13. #13
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    I suggest you put echoes before you exit the first script and see what is missing or changing, remove codes in question and gradually add them until you get errors. That way you will improve your debugging skill.

  14. #14
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the tip. I will try it and get back to you.

  15. #15
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did just as you had adviced to debug my code. There was nothing wrong with my code because it ran without errors.

    However, the errors were occurring because the two files were stored in different locations. The file that set the cookie was located in

    localhost/new/forums/showthread.php

    and the file that read the cookie was located at

    localhost/new/temp.php

    When I moved temp.php to the forums directory, the code worked and the cookie was read perfectly.

    So now the question is: how do I store cookies so that they can be retrieved from pages stored in any location?

  16. #16
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In php.ini there is a directive named "include_path"
    Try to include both paths to it. It probably works I think.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •