Results 1 to 19 of 19

Thread: Storing URLS in a cookie

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Question Storing URLS in a cookie

    Hi

    Id like a cookie to store a customers recent items viewed.

    Im doing this to set the cookie:

    PHP Code:
    setcookie("viewed"""time()+3600000); 
    And then in my list Ive got this:

    PHP Code:
    if (isset($_COOKIE["viewed"])) {
    foreach (
    $_COOKIE["viewed"] as $name => $value)
        {
        echo 
    "$name : $value <br />";
        }
    } else {
        echo 
    "no cookies";


    If a user clicks on an item it is meant to add the URL of the item to the cookie array list (after making sure its not already there). Im doing this:

    PHP Code:
    if (isset($_COOKIE["viewed"]))
    setcookie("viewed[".$mid."]","$mid");


    Unfortunately I'm getting the echo statement 'no cookies' so I reckon the setCookie function is wrong, but am I going about this the right way? Ive searched online and not found anything to specifically explain using cookies and then pushing items to the array list.

    Can you help?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Storing URLS in a cookie

    I've not tested anything myself, but you might have trouble setting the cookie as an array because you're not setting the cookie as an array initially (though, I'm not even sure that matters). you could try just setting your cookie using:
    PHP Code:
    setcookie("viewed[$mid]"$mid); 
    the only other thing that I could see might give you problems is if $mid is storing a full URL, you might characters that need to be escaped. you could use a function for this, but if the name of the key for the cookie doesn't matter to you, you could simply hash the array's key, like below:
    PHP Code:
    $hash md5($mid);
    setcookie("viewed[$hash]"$mid"); 
    if neither solution works, then make sure you're not trying to use setcookie() after you've sent output to the browser.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Storing URLS in a cookie

    First thing to do is set error reporting to its most verbose level. As kows said, you need to ensure that cookies are set before any other output as they are sent in a SetCookie header.

    Do this by inserting the following line at the top of your script:
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    ?>
    If you haven't already got error reporting set to E_ALL I suggest you change it in your php.ini because it will help during development with debugging.

    I have never used arrays within cookies before and would not recommend it either. However, I did a quick test in Firefox and IE and both don't seem to mind saving Cookies with [] in their names. What is the typical value of your $mid variable?

    My advice however would be to use a session. The session will send a single cookie to the browser containing a session ID and allow you to store as much information relating to that session on the server side as you like (http://www.php.net/session).

    PHP Code:
    session_start(); // like cookies, needs to be called before output

    $_SESSION['myarray'] = array(166772); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    $mid is just an integer value, 1, 2, 3.

    The reason I'm using cookies instead of sessions is that I want the list to remain even if the user visits the site a day or week later.

    Would it be a better idea to simply store the data into one cookie and then use explode() to output separate items? " " could be the delimiter?

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Storing URLS in a cookie

    The array notation "should" work. But a comma separated list of integer product ids would be better. You will then require only one cookie, so it will reduce the amount of data being sent between the server and client.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    Actually yes, that was exactly the idea - to store the $mid rather than entire url!

    Ill test it all again and use error reporting too - thanks!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    How do I set the cookie properly? By this I mean, I want to take $mid and then search through the cookie to ensure it's not already there, if it isn't I want to add it to the cookie and separate all $mid's with the ,comma as you've advised.

    How do I do that properly?

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Storing URLS in a cookie

    That's three questions - I'll have to start charging
    1. To set the cookie, use setcoookie as you were doing before.

    2. The explode() function to get the id's into an array.

    3. Then use the in_array() function to search for the ID you need.

    4. If it is not there append it to the array and use the implode() function to get change the list. (if you are making no other changes to the array you could just append the id to the end of the original string).

    5. Finally set the cookie with the new string.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    Thanks for that VisualAd! I'll be testing it now; and as for charging well I'm going to rate you!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    PHP Code:
    setcookie("viewed""$id"time()+3600000); 
    Does the above line of code actually set a brand new cookie called 'viewed' or does it add to an existing cookie called 'viewed'?

    Each time I change $id will it overwrite the current cookie or make a new one eg. viewed.$id?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    Ok it's fine - this is how I did it, thanks to your code:

    PHP Code:
    /* FIND OUT IF THE COOKIE EXISTS */
    if (isset($_COOKIE["viewed"])) { 
    $viewed=$_COOKIE["viewed"];
    // BREAK IT UP AND SEARCH FOR ID
    $views explode(","$viewed);
    if (
    in_array($id$views)) {
    // DO NOTHING AS IT EXISTS IN LISTS
    } else {
    // ADD IT TO VIEWS ARRAY
    array_push($views$id);
    // JOIN UP ARRAY READY FOR COOKIE LIST
    $viewed implode(","$views);
    // SET IT AS NEW COOKIE VIEWED
    setcookie("viewed"$viewedtime()+3600000);
    }
    } else {
    // CREATE NEW RECENTLY-VIEWED COOKIE
    setcookie("viewed"$idtime()+3600000);

    Cheers!

  12. #12
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Storing URLS in a cookie

    it would be better to check if the in_array() returns false (since this is what you want), rather than checking if it returns true and then doing nothing.
    PHP Code:
    if(!in_array($id$views)){
      
    //do your stuff.


  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Storing URLS in a cookie

    Quote Originally Posted by wwwfilmfilercom
    Ok it's fine - this is how I did it, thanks to your code:

    PHP Code:
    /* FIND OUT IF THE COOKIE EXISTS */
    if (isset($_COOKIE["viewed"])) { 
    $viewed=$_COOKIE["viewed"];
    // BREAK IT UP AND SEARCH FOR ID
    $views explode(","$viewed);
    if (
    in_array($id$views)) {
    // DO NOTHING AS IT EXISTS IN LISTS
    } else {
    // ADD IT TO VIEWS ARRAY
    array_push($views$id);
    // JOIN UP ARRAY READY FOR COOKIE LIST
    $viewed implode(","$views);
    // SET IT AS NEW COOKIE VIEWED
    setcookie("viewed"$viewedtime()+3600000);
    }
    } else {
    // CREATE NEW RECENTLY-VIEWED COOKIE
    setcookie("viewed"$idtime()+3600000);

    Cheers!
    Glad you got it working, but you should be indenting your code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Storing URLS in a cookie

    Quote Originally Posted by kows
    it would be better to check if the in_array() returns false (since this is what you want), rather than checking if it returns true and then doing nothing.
    Why?

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Storing URLS in a cookie

    Good question - why?

  16. #16
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Storing URLS in a cookie

    why would you check for a clause that you don't care about?

    #1
    PHP Code:
    if(in_array($id$views)){
      
    //do nothing
    } else {
      
    $x 1;

    vs.

    #2
    PHP Code:
    if(!in_array($id$views)){
      
    $x 1;

    now, if you actually had to DO something if in_array() returned true, then the first method would make sense -- otherwise, at least to me, it doesn't.

    am I the only one who sees this?!

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Storing URLS in a cookie

    Apparently so... The two constructs are identical.

    Sometimes the logic of doing nothing in a particular case, rather than doing something in the opposite case, is simpler to follow.

  18. #18
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Storing URLS in a cookie

    yes, they do the same thing (not like I was debating that), but it's more efficient to write the way that I posted (though I don't even think it would be more efficient while executing). I can agree that it may be simpler to follow the other way, though, for sure.. but I've never had any problems following it, so I still prefer excluding the "doing nothing."

  19. #19
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Storing URLS in a cookie

    When the code is parsed by the interpreter, redundant statements such as that are simply removed. The only way in which that is less efficient is the extra typing required
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width