PHP session for tracking unique page views

前端 未结 2 1953
萌比男神i
萌比男神i 2020-12-07 05:07

I want to use sessions to track unique page views. Not a very robust method, I know, but sufficient for what I want to do.

On the first page load the session variabl

相关标签:
2条回答
  • 2020-12-07 06:00

    Just tested. This also works.

    session_start();

    // Get page name
    $page_url = $_SERVER['REQUEST_URI'];

    // Create the session ID. Will act as visitor username
    if(!isset($_SESSION['id'])){

    $_SESSION['id'] = session_id();

    // For the visit to the first page
    $visit_id = $_SESSION['id'];

    }else{

    // For subsequent visits to any page
    $visit_id = $_SESSION['id'];

    }

    /******************
    Query DB. Insert only one visit per page per session. It means we need a count for each page to get its total visits. Or a count for all records to get site total visits.
    ************************************/
    $query_visits_table = mysqli_query($db_connect, "SELECT * FROM tblcount WHERE (visit_id='$visit_id') AND (page_name='$page_url')");

    if(mysqli_num_rows($query_visits_table) > 0){

    // Do nothing if this page has been visited during this session

    }else{

    mysqli_query($db_connect, "INSERT INTO tblcount (visit_id, page_name, visit) VALUES('$visit_id', '$page_url', '1')");

    }

    // Get site total visits
    $query_site_visits = mysqli_query($db_connect, "SELECT * FROM tblcount");

    // For a specific page
    $query_specific_page_visit = mysqli_query($db_connect, "SELECT * FROM tblcount WHERE page_name='$page_url'");

    if(isset($query_site_visits) && isset($query_specific_page_visit)){

    $site_total_visits = mysqli_num_rows($query_site_visits);
    $specific_page_visit = mysqli_num_rows($query_specific_page_visit);

    echo 'Site total visits is '. $site_total_visits . '<br />';

    echo 'Total visits for ' . $page_url . ' is ' . $specific_page_visit;

    exit();

    }

    0 讨论(0)
  • 2020-12-07 06:07

    Problems:

    1. You are updating in tblCount EACH time, because your session is closed each time your script finishes. SO: Put the session_start()call as the FIRST LINE in code.
    2. It's not permitted to set an integer as $_SESSION variable. So if you set $_SESSION[$pagenumber] = 'something', then you gain the following notice:

    ( ! ) Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

    Quite... not understandable. For details see this answer.

    Solution:

    Add your $pagenumber as index in an array (here pagenumbers) and that array inside the $_SESSION variable. No notice anymore.

    session_start();
    
    $pagenumber = 1;
    
    if (!isset($_SESSION['pagenumbers'])) {
        $_SESSION['pagenumbers'] = array();
    }
    
    if (!isset($_SESSION['pagenumbers'][$pagenumber])) {
        updateViews($pagenumber);
        $_SESSION['pagenumbers'][$pagenumber] = $pagenumber;
    }
    
    echo 'Page number: ' . $_SESSION['pagenumbers'][$pagenumber] . '<br/>';
    $views = getViews($pagenumber);
    echo '<pre>Viewed ' . print_r($views, true) . ' times</pre>';
    

    Note: I used my functions to test. They just replace your db-processing code.

    0 讨论(0)
提交回复
热议问题