Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Cookies

  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookies

    When I click on a number, I like it to switch from 0 to 1 or vice versa. Now both numbers gets switched. Also, when I reload the page, the numbers switch.

    I only like the number that I click on to switch. And reloading shouldn't affect the numbers. I think that looking at the code simply explains what I try to do.

    What am I doing wrong?

    The filename is test.php

    <html>
    <body>

    <?php function uSwitch($cid)
    {
    if (isset($_COOKIE[$cid]))
    if ($_COOKIE[$cid]=="0")
    setcookie($cid, "1", time()+7200);
    else
    setcookie($cid, "0", time()+7200);
    else
    setcookie($cid, "1", time()+7200);
    }
    ?>

    <?php function uPrint($cid)
    {
    if (isset($_COOKIE[$cid]))
    echo $_COOKIE[$cid];
    else
    echo '0';
    }
    ?>

    Welcome!<br>
    <br>
    user1: <a href="test.php" onClick="<?php uSwitch("u1"); ?>">
    <?php uPrint("u1");?></a><br>
    user2: <a href="test.php" onClick="<?php uSwitch("u2"); ?>">
    <?php uPrint("u2");?></a>

    </body>
    </html>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You're trying to call a php function from a clientside event (onclick). PHP doesn't work like that.

    How PHP works

    You could always just use Javascript to manipulate your cookies, if you have no other reason to use PHP.

    Just one of many Google results

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,466
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    All I can think of is using two files ...

    This file first checks for an existing cookie, or starts it out at zero.
    It then calls the next script that sets or resets. You need to have
    that second script otherwise the variable is retained and a refresh will
    change it back. This way, a refresh will not affect the values.

    This one I called "test2.php" ...
    Code:
    <?php
    session_start();
    function uPrint($cid){
    $id=$_SESSION[$cid];
    if(!$id){echo "0";}
    else{echo $_SESSION[$cid];}
    }
    ?>
    <html>
    <body>
    
    Welcome!<br>
    <br>
    user1: <a href="test3.php?cid=u1">
    <?php uPrint("u1");?></a><br>
    user2: <a href="test3.php?cid=u2">
    <?php uPrint("u2");?></a>
    
    
    </body>
    </html>
    This is the second file ... the one that does the switching ...

    This one I called "test3.php" ...
    Code:
    <?php
    session_start();  
    $cid = $HTTP_GET_VARS['cid'];
    
    if($_SESSION[$cid] == 0){
    $_SESSION[$cid]=1;
    }
    else{$_SESSION[$cid]=0;
    }
    header ("location: test2.php");
    ?>

  • #4
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mlseim, your files aren't working :/ but I had good use of your idea! ^^ thanks =)

    test.php
    Code:
    <?php function uPrint($cid)
    {
    if (isset($_COOKIE[$cid]))
      echo $_COOKIE[$cid];
    else
      echo '0';
    }
    ?>
    
    <html>
    <body>
    Welcome!<br>
    <br>
    user1: <a href="test2.php?cid=u1&num=<?php uPrint("u1");?>"><?php
    uPrint("u1");?></a><br>
    user2: <a href="test2.php?cid=u2&num=<?php uPrint("u2");?>"><?php
    uPrint("u2");?></a>
    
    </body>
    </html>
    test2.php
    Code:
    <?php
    $num=$_REQUEST["num"];
    $cid=$_REQUEST["cid"];
    if (isset($cid))
    {
      if($num=="1")
        setcookie($cid,"0",time()+7200);
      else
        setcookie($cid,"1",time()+7200);
    }
    header ("location: test.php");
    ?>
    Last edited by Mikael; 08-22-2006 at 07:12 PM.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,466
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Mikael ...

    Glad it gave you an idea. Perhaps the "session" is not
    configured the same for you as it is for me (with my webhost).

    The key is having two files.


  •  

    Posting Permissions

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