1. This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
    For information on cookies and how you can disable them visit our Cookie Usage page.
    Dismiss Notice

PHP [PHP] Letting your visitors know that some pages have changed ...

Discussion in 'Web Development' started by Gonzoide, Jul 29, 2014.

  1. Gonzoide

    Gonzoide New Member

    Hi all,

    Sometimes, it can be useful to let your visitors know that something has changed on one page or the other of your website. Since I am not a big fan of cookies, I'll propose a small PHP/MySQL implementation that simply assumes that your visitor is identified via a session variable "user_id", you can of course adapt this to your need.

    Code is split in two parts:
    1 - monitoring a page that is subject to changes
    2 - getting this information to present in a menu

    1 - monitoring a page that is subject to changes

    In each page you want to track, import the corresponding lib and provide a unique ID, as a variable $trackId. For instance in my page storing the latest news, in PHP I declare :

    $trackId = "news";
    @require_once("track.php");

    Signal the system that user has just visited current page :

    handleTrack($trackId);

    Whenever you decide that your page has changed and should show up accordingly in menu, just use :

    updateTrack($trackId);

    2 - getting this information to present in a menu

    In the menu page where you want to monitor which pages contain new stuff, use, add part in red in the menu item corresponding to the tracked page :

    <li><a <?= newStyle("news") ?> href="/news.php">News</a></li>


    Method "newStyle", present in track.php, will detect if your user has already seen the latest version of this page, and display something accordingly.

    In this example, I force the style to change to "new_menu_item", but you're of course encouraged to use your style instead :)

    3- that's it ! Now a little big on the technical part ...

    Track.php assumes that you have a MySQL table with the following format :

    user_id : int(4)
    page : text
    time : timestamp


    The track.php file follows (for clarity I'm using methods "executeSQL" and "getData", you may replace them with whatever code you access your database with) :

    <?php

    function trackUser($trackId, $userId) {
    executeSQL("delete from track where user_id=".$userId." and page='".$trackId."'");
    executeSQL("insert into track (user_id, page, time) values (".$userId.",'".$trackId."', now())");
    }

    function updateTrack($trackId) {
    trackUser($trackId, -1);
    }

    function handleTrack($trackId) {
    if (($trackId != null) && isset($_SESSION['user_id'])) {
    // user is visiting a tracked page

    trackUser($trackId, $_SESSION['user_id']);
    }
    }

    function isNew($trackId) {
    $result = 0;
    $update = null;
    $last = null;

    if (isset($_SESSION['user_id'])) {
    // last change timestamp

    $rs = executeSQL("select * from track where page='".$trackId."' and user_id=-1");

    if ($data = getData($rs)) {
    $update =$data["time"];
    }

    // last visit timestamp

    $rs = executeSQL("select * from track where page='".$trackId."' and user_id=".$_SESSION['user_id']);

    if ($data = getData($rs)) {
    $last =$data["time"];
    }

    // result

    if (($update != null) && ($last == null) || ($update > $last)) {
    $result = 1;
    }
    }

    return $result;
    }

    function newStyle($trackId) {
    $result = "";

    if (isNew($trackId)) {
    $result = "class='new_menu_item'";
    }

    return $result;
    }
    ?>
     
    Ruthe and Dawn like this.