Jump to content

A simple website,pages,posts views counter using files


QuickOldCar

Recommended Posts

I made this for my site and decided to share with the community.

 

Depending on how you do the code and where, this can keep count of the views in different ways.

 

Please read the somewhat directions have in the code, make a folder called counters, and be sure to include this below function file. I named the function file index.php and belongs in the counters folder you make.

 

<?php
/*
name this file index.php and place it into a folder called counters

the function will create a unique text file for each unique item, good for posts or pages and store all the files in the counters folder

For the total website views,add this to your header file or top of your page
<?php
include('counters/index.php');
$website_view_url = "http://".$_SERVER['HTTP_HOST'];
}
$website_views = getViews("$website_view_url");
echo "<br />".$website_views."<br />";
?>

For the total pages or scripts views,add this to your header file or top of your page
<?php
include('counters/index.php');
$total_page_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
}
$total_page_views = getViews("$total_page_view_url");
echo "<br />".$total_page_views."<br />";
?>

For entire urls including queries,add this to your header file or top of your page
<?php
include('counters/index.php');
$page_queries_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"])) {
$page_queries_view_url .= "?".$_SERVER['QUERY_STRING'];
}
$page_queries_views = getViews("$page_queries_view_url");
echo "<br />".$page_views."<br />";
?>

for usage in posts like by id:
if do not want the pages counted and did not do the above code,include the below somewhere near the beginning of your page

<?php include('counters/index.php');?>

Then in the posts loop, you can associate your $row['id']; or some other unique value

<?php
$post_views = $row['id'];
$post_views = getViews("$post_views");
echo "<br />Post Views ".$post_views."<br />";
?>

if would like to combine any the above or use them all, just be sure to just include include('counters/index.php'); only one time
*/


function getViews($views_count_value)
{
    $views_count_value = md5("$views_count_value");
    $views_count_file_name = "counters/$views_count_value.txt";
if (!file_exists($views_count_file_name)) {
    $views_count =0;
    $file_handle = fopen($views_count_file_name, 'w') or die("can't open file");
    fwrite($file_handle, "$views_count");
    fclose($file_handle);

}

    $file_handle = fopen($views_count_file_name, "r");
    $views_count = fread($file_handle, filesize("$views_count_file_name"));
    fclose($file_handle);
    if ($views_count <= 0){
    $views_count =1;
    } else {
    ++$views_count;
    }
    $file_handle = fopen($views_count_file_name, "w+");
    fwrite($file_handle, "$views_count");
    fclose($file_handle);
    return("$views_count");

}
?>

Link to comment
Share on other sites

No am just being nice and helping someone who may need this.

 

I was editing the code, but then couldn't edit it, so please use the code below.

 

<?php
/*
name this file index.php and place it into a folder called counters

the function will create a unique text file for each unique item, good for posts or pages and store all the files in the counters folder

For the total website views,add this to your header file or top of your page
<?php
include('counters/index.php');
$website_view_url = "http://".$_SERVER['HTTP_HOST'];



}
$website_views = getViews("$website_view_url");
echo "<br />".$website_views."<br />";
?>

For the total pages or scripts views,add this to your header file or top of your page
<?php
include('counters/index.php');
$total_page_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];



}
$total_page_views = getViews("$total_page_view_url");
echo "<br />".$total_page_views."<br />";
?>

For entire urls including queries,add this to your header file or top of your page
<?php
include('counters/index.php');
$page_queries_view_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"])) {



$page_queries_view_url .= "?".$_SERVER['QUERY_STRING'];



}
$page_queries_views = getViews("$page_queries_view_url");
echo "<br />".$page_queries_views."<br />";
?>

for usage in posts like by id:
if do not want the pages counted and did not do the above code,include the below somewhere near the beginning of your page

<?php include('counters/index.php');?>

Then in the posts loop, you can associate your $row['id']; or some other unique value

<?php
$post_views = $row['id'];
$post_views = getViews("$post_views");
echo "<br />Post Views ".$post_views."<br />";
?>

if would like to combine any the above or use them all, just be sure to just include include('counters/index.php'); only one time
*/


function getViews($views_count_value)
{
    $views_count_value = md5("$views_count_value");
    $views_count_file_name = "counters/$views_count_value.txt";
if (!file_exists($views_count_file_name)) {
    $views_count =0;
    $file_handle = fopen($views_count_file_name, 'w') or die("can't open file");
    fwrite($file_handle, "$views_count");
    fclose($file_handle);

}

    $file_handle = fopen($views_count_file_name, "r");
    $views_count = fread($file_handle, filesize("$views_count_file_name"));
    fclose($file_handle);
    if ($views_count <= 0){
    $views_count =1;
    } else {
    ++$views_count;
    }
    $file_handle = fopen($views_count_file_name, "w+");
    fwrite($file_handle, "$views_count");
    fclose($file_handle);
    return("$views_count");

}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.