SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 46

Thread: PHP Cookies

  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm Afraid I'll have to trouble you guys here at the PHP/mySQL forum. I trying to develop a method which would show the latest events since a user last visited the site. This would mean like chnage the font colour to red or soemthing which would catch the user's attention.

    Now, a simple method which I reckon would be is to use PHP Cookies.

    What has to be done is setting a cookie whenever a user visits the main page, and save the date/time within the cookie.

    When the user revisits, the value from the cookie will be compared with the value stated in:

    $moddate = filemtime("filename.dat");

    If the data in the user's cookie is older then that specified by the $moddate, then the variable:

    $color = '#A80000';
    else, it'll be $color = '#000000';

    I am not well versed in PHP, am learning as time goes by. Hence, I'll need the assistance of the experts here.

    Thank you for your time.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're on the right track, lynlimz! All things being equal, the solution you propose is the same one I would use in your place. Where are you getting stuck?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Thanks.

    Currently, I already have the built in system for getting the modified date, the $moddate variable in short.

    I have no clue to creating a cookie, adding the value and saving it. And also retrieving it.

    I did some reading, about...retrieving a varibale via HTTP_COOKIE_VAR or something...yup..

    Any ideas?

    That reminds me of somehting..you know this bulletin? it records your last visited and shows which post you haven't read right? yep....exactly what I need on my site, just very much on the smaller scale.

    Thanks for your help Kevin
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies are fairly easy tos et and retrieve with php.
    The setcookie function create a new cookie
    like so...
    setcookie("lastvisit", time(), time()+258000, "", "http://www.yourdomain.com", 0);

    and it works like this...you can probabaly see the relationship.
    setcookie(name, value, expire, path, domain, secure)

    The associative array HTTP_COOKIE_VARS is the way to retriev the cookies for the current page. So
    $HTTP_COOKIE_VARS[lastvisit]
    would contain the timestamp of last visit.

    For more info refer to the cookie section of the php manual, under features if memory serves me.

    Goodluck
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    COOL! Thanks

    How then do I set the format of the date to be saved on the user's computer?

    And how then do I compare that date and the date form $moddate and verify which is the latest?

    thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need help with the basics of setting cookies, deleting cookies, and reading from them, you can checkout this tutorial I just posted (yes, shameless self-promotion, but I think it might help):

    MyCoding.com: Using Cookies In PHP

    Good luck!

  7. #7
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks TWTCommish!

    I'll read it...and see if I can make do with it..LOL
    cool...no rpboem...
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  8. #8
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks..I've created a cookie, and am able to retrieve the data.
    How now do I compare the dates and see which is newer? thanks

    Here's my code for my PHP file.

    Code:
    <?php
    function adj_timezone($meth, $hour, $moddate) {
    $hour = ($hour * 3600);
    if ($meth == "+") { $newtime = $moddate + $hour; }
    elseif($meth == "-") { $newtime = $moddate - $hour; }
    else { $time = ""; }
    $adj_time = DATE("d/m/y | g:i:s a", $newtime);
    $time = explode("|", $adj_time);
    return $time;
    }
    
    $moddate = filemtime("$SCRIPT_FILENAME");
    $time = adj_timezone("+", 13, $moddate);
    $date = "$time[0]";
    setcookie ("date", $date, time()+31536000); /* Expires in a year */
    echo "$date";
    ?>
    The date format is in the form: dd/mm/yy

    Thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if this will make a difference for you but if you read TWTcommish's artcile on PHP Cookies (which is great) then you would know that the setcookie function should be the first thing on the page.

    I haven't tested cookies out yet, but it might make a difference.

    Can anyone back me and TWTCommish up?

    Pete

  10. #10
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    petesmc..
    Yes..I noted his explanation.
    But it only stated: output

    I wasn't outputing any text before the cookie code. It was a mere function to get the modified date. ( which freddydoesphp has so greatly helped me with some time ago )

    there are no echo, print commands or whatsoeveer before.

    I tested it...and the cookie was created..so maybe ther ain't any problem?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  11. #11
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do whatever as long as the setcookie() function is the first thing sent to the browser.
    It is like a header in this way. In fact it is a header .

    If it is not you will get some error like
    Failed setting cookie on line 1234, output started by file.php at line 4321.
    Or something along those lines.

    Nice article by the way commish!. :-)
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  12. #12
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - that means a lot coming from an onion. Didn't take too long to write, but I figured it would be very helpful. Cookies are kind of "scary" to learn for some reason.

  13. #13
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies are something that few sites (technical authors) seem to cover. Why, because they are such a bee eye sea tee hache!
    The bad rap issue that you touch on is a very valid point, i know my school has disabled cookies (because they are ignorant to actual facts regards IT). I hear of alot of users doing the same, unless you understand (and they are hard to understand), the thought that a piece of data is able to be placed on your computer, without warning, is a scary one.

    that means a lot coming from an onion
    You were lucky it was the first thing i did after getting home from first day back at school, most things would have made me irrate .. LOL
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  14. #14
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, they get a very bad rap - I was incredibly tempted to simply blow that officer away with technical jargon, but at the time I didn't know as much about cookies as I do now, so I was positive about the things I might have said. Today, I wouldn't let it slide.

    If you liked that article, look for another one in the future, which will essentially be a followup to create a small user management system using cookies.

  15. #15
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    More shameless self promotion
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  16. #16
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah..TWTCommish or anyone..you know how to check which dates are newer?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  17. #17
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is why we would use the time() to set the time in the cookie.
    It is possible with other date formats but the unix timestamp would make it much easier.

    If you were to grab the time from the cookie, then the current time you could do it like this.

    $vtime=$HTTP_COOKIE_VARS[lastvisit];
    $ctime=whatever; //the last update timestamp
    if($ctime>$vtime){
    //the user has not seen the most recent info
    }else{
    //the user has seen the info
    }
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  18. #18
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Sorry about this..but the dates used throughout the entire site is in:

    dd/mm/yy

    If I were to change it, that would mean making a change throughout the entire site. Definitely not an option for just a simple addition to the main page.

    Is there anywway the date can be compared?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  19. #19
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is a little off-topic, (forgive my ignorance) but :

    if a user has cookies disabled in his/her browser, does that mean that i can't use sessions either to do things?
    . . . chris

  20. #20
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    atomicmunky: yep
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  21. #21
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lynlimz, that is not correct, if a user has cookies turned off, you can still pass the session id through the url. All a cookie holds for sessions is the value of the session id. So whether it gets passed to the next page depeneds on the cookie being set or the session id being passed trhough the url. Alternately you can compile PHP with --enable-trans-sid which will automatically append the session id to all links in the site where session_start() is called.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  22. #22
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Back to lyn. original question ...

    You could use mktime to convert your dd/mm/yy time to a timestamp.
    int mktime (int hour, int minute, int second, int month, int day, int year [, int is_dst])
    There the excert from the php manual...fairly self explanatory.

    I dont have time to explain cause i have school soon but someone can im sure.
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  23. #23
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    lynlimz, that is not correct, if a user has cookies turned off, you can still pass the session id through the url. All a cookie holds for sessions is the value of the session id. So whether it gets passed to the next page depeneds on the cookie being set or the session id being passed trhough the url. Alternately you can compile PHP with --enable-trans-sid which will automatically append the session id to all links in the site where session_start() is called.
    There are only two problems that I can see with sticking the sessionID into every link:
    If you do it by hand, that is quite a bit of work for you as the developer and PHP is all about saving time.
    If you rely on PHP to do it for you with --enable-trans-sid, you incur a substantial performance hit if you recieve any sort of traffic as PHP must look at each page served for relative links and then add the ID to it.

    The best/easiest way I've seen it done is with mod_rewrite in apache (http://www.bobross.com/session_id/index.php) and DNS tricks (such as http://session_id.bobross.com/index.php). Both methods are explained in detail in the book "Web Application Development with PHP 4.0", which I recommend if someone is bored and wants to read about PHP.

    Of course, by far the easiest way is to compile with --enable-trans-sid -- but only for sites that doesn’t get "too much" traffic. I'm not sure what "too much" traffic is, but if you turn it on and watch performance drop, then you know you have "too much" traffic.

  24. #24
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgive me if im worng but wouldnt the main problem with parsing the session_id in the url (which i have done before) be having people tamper with the url and lose their session...visitors might get annoyed with that.

    Anyway i take what freddy says to be correct (most of the time ), so i will wait for his input on this before i go mouthing off anymore :-)...
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  25. #25
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Matt none of these links seem to work. I can't speak for performance I never built a site like amazon where I get millions of hits everyday, but for sites I have built --enable-trans-sid has worked great for the non-cookie users. I will have to take a look at the mod_rewrite for apache and see, but from my experience the only way to insure that the session id gets passed, is to pass it in the url, that is why PHPLIB does that way too.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •