Jump to content

PHP Article Counter


computermax2328

Recommended Posts

Hello Again,

 

In the database of the blog I am building I have a column for each article called "count." What I did on the article page is every time the id for the article is called to the page to select all of the content for that specific article the count will be selected from the database and updated plus one. The problem that I am having is that it is adding two to the count every time. The code looks something like this.

 

<?php
require("../includes/connection.php");
   $id = $_GET['id'];
   $query = "SELECT * FROM blog WHERE id=$id";
   $select = mysql_query($query);
   while ($row = mysql_fetch_array($select)) {
    $id = $row['id'];
    $count = $row['count'];
    $date = $row['Date'];
    $title = $row['Title'];
    $sub = $row['Subtitle'];
    $author = $row['Author'];
    $content = $row['Content'];
    $tags = $row['Tags'];
   }
$addcount = $count+1;
   $update = "UPDATE blog SET count=$addcount WHERE id=$id";
   $upcount = mysql_query($update);
?>

 

Any ideas?? :confused:

Link to comment
Share on other sites

First off, make sure you sanitize that $_GET['id'] input before you use it in a query with MySQL. Use regex for example to check if it's a number.

 

if(!preg_match('/[^0-9]/', $_GET['id'])){
// safe to use $_GET['id']
}

 

The second part is that you don't need to search for it first, you only need to update.

 

"UPDATE blog SET count=count+1 WHERE id=$id";

 

I guess you need to search anyways, because you need the content of the post.

 

Do this with all of your mysql_querys:

mysql_query($query) or die(mysql_error());

 

$upcount should in your code tell you if you if the MySQL update was successful, with either a TRUE/FALSE value. Don't mistake it for the actual counter.

Link to comment
Share on other sites

Your page is probably being requested twice, either by the browser (different browser do this for different reasons), by something you are doing in your code, by url rewriting, or even how your web host is sending http requests to your web server (assuming this is occurring on a live web site.)

 

Check your web server's access log to see if there are closely spaced http requests from the same ip address with the same id on the end of the url.

 

If you are getting multiple requests and they are not due to a coding error on your part, the ultimate solution is to NOT maintain a simple count, but to store a record in a database table that records, at a minimum, the article id, the ip address of the http request, and the datatime of the http request. You will then be able to use this information to filter out and ignore duplicate http requests to get a more accurate count.

 

Another method to detect and prevent duplicate requests is to set session variables with the article id and the datatime of the http request and if another request occurs for the same article id within a short time (~ 1 second), ignore it.

 

 

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.