Jump to content

Count the number of times a page has been viewed


ababba2

Recommended Posts

Hi guys.

I need to make a view counting script for my site. Using mysql.

 

I tried with the following code. But I don't know why it doesn't work.

     <?php
      $refreshed = $_SERVER['HTTP_CACHE_CONTROL'];
if ($refreshed == 'max-age=0'){
      $db                 = JFactory::getDBO();
      $query      = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id=" . $details->id;
      $db->setQuery($query);
      $db->query();
      }?>

Using this code, views doesn't grow up.

And if possible, I would like to set +1 in the database at every refresh of the page.

 

Could someone give me help? Thank you

 

EDIT: of course __hdflv_upload already exist in the database

Edited by ababba2
Link to comment
Share on other sites

My phpinfo does not show an element called HTTP_CACHE_CONTROL  - does yours?

 

I realize that you are trying to avoid repeated F5 hits here but is it really necessary.  Why not just trigger "new" hits on a page with a snippet of code and a function that logs the data?  I woule just check if there existed a submit value that comes from the current page; if it doesn't then it is a new visit to the page and  I would log it.  If not, I wouldn't.

Link to comment
Share on other sites

At the top of the page I'd just get the number of counts currently sorted in the db then add one.

 

$count = $row['count'];

 

$new_count = $count + 1;

$sql= "UPDATE table SET count = '$new_count' WHERE id='$details'";

$result = mysqli_query($cxn,$sql);

 

There is no need to run a SELECT query to get the current count in order to increment it by one before running the UPDATE query. You can do it all within an UPDATE query

UPDATE table SET field=field+1 WHERE id=$id
Edited by Psycho
  • Like 2
Link to comment
Share on other sites

I cannot use other tables...

I need to use __hdflv_upload and times_viewe for many reason...

 

No one is suggesting to use another table - those were examples. Did you even read ginerjm's response? How do you know that there is a HTTP_CACHE_CONTROL value? You need to add code for debugging purposes.

 

<?php
 
if(!isset($_SERVER['HTTP_CACHE_CONTROL']))
{
    echo "HTTP_CACHE_CONTROL doesn't exist";
}
elseif($_SERVER['HTTP_CACHE_CONTROL'] != 'max-age=0')
{
    echo "HTTP_CACHE_CONTROL doesn't equal 'max-age=0'. It is '{$_SERVER['HTTP_CACHE_CONTROL']}'.";
}
else
{
    $db = JFactory::getDBO();
    if(!$db)
    {
        echo "Unable to create DB reference";
    }
    elseif(!$details->id)
    {
        echo "Details id does not exist.";
    }
    else
    {
        $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}";
        $db->setQuery($query);
        if(!$db->query())
        {
            echo "Error executing query. Follow Joomla documentation to output the error.";
        }
        else
        {
            echo "Success";
        }
    }
}
?>
Link to comment
Share on other sites

Had this for someone else, can use in any way need to with tracking clicks or views, Iframe,include,ajax. With a js onclick event or simple href link.

 

Could change it around to add views or clicks each users name or user id, matter of editing it.

 

http://forums.phpfreaks.com/topic/296442-unique-click-tracking-using-php/?p=1512504

Link to comment
Share on other sites

Thank you, now the script works.

Anyway I'm facing another problem.

If I set the joomla cache of my site Off, then at every refresh I get +1.

Anyway, If I turn On the cache plugin, then I get +1 the first time I load the page and then it stop counting. I need to clear cache for get another +1 in the view count of that page.

From admin panel I see that every other page reload doesn't get count by the script.

How can I fix this?

I thought maybe I could disable caching for this little script. There are many plugin for this in Joomla, but I have no idea how to do this in php.

Edited by ababba2
Link to comment
Share on other sites

I don't know how you are doing this that makes the cache affect you.  I do this all the time.  Every time my script executes it checks if it has already been run and is processing a response, or if it is the "first" time it has been called.  If it is the "first" then I increment my counter.  Obviously this won't tally up refreshes, but why would you want to do that?  It doesn't tell you anything about usage.

 

So - show us your code for keeping your hit counts so we can see why the cache is affecting you.

Link to comment
Share on other sites

I used Psyco's script... I didn't changed anything in his script you see above... I just removed this

if(!isset($_SERVER['HTTP_CACHE_CONTROL']))
{
    echo "HTTP_CACHE_CONTROL doesn't exist";
}
elseif($_SERVER['HTTP_CACHE_CONTROL'] != 'max-age=0')
{
    echo "HTTP_CACHE_CONTROL doesn't equal 'max-age=0'. It is '{$_SERVER['HTTP_CACHE_CONTROL']}'.";
}
else
{

Anyway, I'm getting this cache problem

Link to comment
Share on other sites

<?php   
    $db = JFactory::getDBO();
    if(!$db)
    {
        echo "";
    }
    elseif(!$details->id)
    {
        echo "";
    }
    else
    {
        $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}";
        $db->setQuery($query);
        if(!$db->query())
        {
            echo "";
        }
        else
        {
            echo "";
        }
    }

?>

This is working.

Edited by ababba2
Link to comment
Share on other sites

^Is working fine, but as I said there is cache problem.

 

I found this old quote on another forumt that could be useful

 

 

Problem is that Joomla counts and update the article hits when the page is built, but if the caching plugin is enabled and the page has been cached to disk, the page is loaded directly from the cache directory, without updating the number of views.

but still no solution

Edited by ababba2
Link to comment
Share on other sites

That code is pretty weak looking.  You test two things to see if they failed, but when they do you don't output anything to tell you they failed.  Echo-ing out an empty string as you do is not very helpful when something didn't work. And if it didn't work you continue on with the rest of the script anyway.  Why?

 

To top it all off - you then do your update query and once again you test for failure but don't put out a message whether it actually updated or not. 

 

As for caching - how does a php script get cached on the browser since it only exists on the server?

Link to comment
Share on other sites

Of course before removing the text inside the echo I tried this code too

<?php
 
    $db = JFactory::getDBO();
    if(!$db)
    {
        echo "Unable to create DB reference";
    }
    elseif(!$details->id)
    {
        echo "Details id does not exist.";
    }
    else
    {
        $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}";
        $db->setQuery($query);
        if(!$db->query())
        {
            echo "Error executing query. Follow Joomla documentation to output the error.";
        }
        else
        {
            echo "Success";
        }
    }
}
?>

With cache off, as result of the script I get "Success". And it's true.
With cache on, as result I get "success". But from Admin panel I see that isn't true.

 

For caching... well. Browser caching is disabled. I don't mean that kind of cache.

I mean this kind of cache: https://docs.joomla.org/Cache

Edited by ababba2
Link to comment
Share on other sites

Well, I don't think I will get any solution there.

Usually they don't offer this kind of support...

 

It's not the first time I ask them support for advanced problems and I never had answer...

 

 

Anyway I found this article on google: http://www.inmotionhosting.com/support/edu/joomla-3/increase-hits-cache

This explain how the fix view counter on articles when cache enabled. But my script is not based on article views. It's a different kind of view counter. But maybe could be useful for understand how to fix even my problem... They say to use jquery... do you think thi can be useful?

Edited by ababba2
Link to comment
Share on other sites

We need to do some definitions clearing up here.

 

Your article talks about "article" hits.  Can you define for me what pages you are attempting to count?  If you are displaying a requested document (not a script) and want to have it counted every time it is requested, that is definitely not what we(?) have been giving you.  Of course I can see why joomla as well as a browser would give you the cached copy instead of a new one.

 

So - when the user clicks on whatever you have on your webpage in order to see a new document, what actually happens?  Is there a form submit to a php script?  Or at least an anchor href pointing to a php script?

Link to comment
Share on other sites

http://test995.altervista.org/

This is a testing site I use for test.

 

As you can see in that site, there is a video gallery. What I'm trying to do is to count video view.

For "count video view" I don't mean how many times someone click play on the video.

I mean how many time a page that cointains a video is opened by someone

Edited by ababba2
Link to comment
Share on other sites

I'm not native speaker, so it's hard for me explain in detail. Please try to understand.

 

 

 

Your article talks about "article" hits.  Can you define for me what pages you are attempting to count?

 

I want to count how many time a page that cointains a video is opened by someone

 

 

If you are displaying a requested document (not a script) and want to have it counted every time it is requested, that is definitely not what we(?) have been giving you.

 

Sorry, I don't understand what you mean with document. If you mean excel files, word files, zip files... Then no, it's not a document.

 

 

So - when the user clicks on whatever you have on your webpage in order to see a new document, what actually happens?  Is there a form submit to a php script?  Or at least an anchor href pointing to a php script?

 

 

In homepage there is a list of videos. When you click on the video you want to see, happens that a php script runs and show a page containing that videos. I would like to count this kind of view...

Link to comment
Share on other sites

Inside the php script there is a call to a flash file. This flash file handles many thing. For example, the player of the videos is based on flash. And also handles and update the counter view.

But since flash is an outdated plugin, now I'm working for remove flash completely from my site.

I replaced flash player with html5 player.

Html5 player works fine, but now the problem is that the view counter doesn't work anymore since I removed the flash call. So I would like to make another script (not based on flash) that could update the view counter.

 

But this shouldn't be affected by the joomla cache...

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.