1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Passing data values in cookie through link?

Discussion in 'PHP' started by SEOguy101, Jan 12, 2009.

  1. #1
    I wondered if someone could explain how to pass a value through a link using cookies to store the data. For instance, I have a quiz with four possible answers and each one has a value associated with it. The value associated with the answer that the user clicks needs to be stored in the cookie.
    SEMrush
    Here is an example:

    What is your favorite color in the rainbow?

    Yellow (value=1)
    Red (value=2)
    Orange (value=3)
    Blue (value=4)

    So if the user clicks orange the integer 3 needs to be stored in a cookie, and the quiz would progress to the next question.

    Any help would be appreciated. Thanks!
     
    SEOguy101, Jan 12, 2009 IP
    SEMrush
  2. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,069
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    130
    #2
    I think you need to create a script which would generate cookie after clicking on one of those links.
    <a href="cookie.php?value=1">Yellow</a>


    cookie.php - create cookie
     
    deriklogov, Jan 12, 2009 IP
  3. SEOguy101

    SEOguy101 Active Member

    Messages:
    640
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #3
    SEOguy101, Jan 12, 2009 IP
  4. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,069
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    130
    #4
    deriklogov, Jan 12, 2009 IP
  5. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I would store the values in a session instead. With cookies, with every page that the user requests all the values stored in the cookies will be passed to the server, which would be a waste of bandwidth (and therefore speed).
     
    phper, Jan 12, 2009 IP
  6. artflutter

    artflutter Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    using sessions is definitely the best option, heaps of info out there in googleland.
    Basically at the top of your php script you want to include the code
    session_start();

    and then on the 2nd page if you want to know the answer submitted from first page you would just need to use $_SESSION[rainbow] or whatever the input name of the form element was.
     
    artflutter, Jan 13, 2009 IP
  7. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #7
    you can use both session or cookie.

    you can do something like this:

    // define variable 
    if($_GET['color'] == 'Yellow') {
     $color = 1;
    } elseif ($_GET['color'] == 'Red') {
     $color = 2;
    } elseif ($_GET['color'] == 'Orange') {
     $color = 3;
    } elseif ($_GET['color'] == 'Blue') {
     $color = 4;
    }
    
    if(!empty($var)) {
     setcookie("chosen_color", $var, time()+60*60*24*7); // this will save the color value in cookie, it will be saved for 1 week
    }
    
    PHP:
    you can fill the value in a variable using $_GET or $_POST command.
    the above code will work if you send the link this way:
    you.com/page.php?color=Yellow

    to fetch the cookie value, do something like this.


    if(isset($_COOKIE['chosen_color'])) {
      echo "The cookie value is: ".$_COOKIE['chosen_color'];
    }
    
    PHP:
    i hope this helps.
     
    khan11, Jan 13, 2009 IP
    SEOguy101 likes this.
  8. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think your answer is available in your question. As others i can say that using session will be better way for storing value and then append the new page value into your array with previous values.


    at the end you will get all values in your array.

    ------------------------------------------------------------------
    Precious Interview News
    Download Free Ebooks
     
    linkexchange1984, Jan 13, 2009 IP
  9. SEOguy101

    SEOguy101 Active Member

    Messages:
    640
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Very helpful post...rep added! Thanks!
     
    SEOguy101, Jan 13, 2009 IP