Jump to content

[SOLVED] hit counter with url?


delphi123

Recommended Posts

Hi there,

 

I'm trying to make a hit counter that I can then use to show the most popular urls on a site.

 

I've got the simple code from the phpfreaks tutorials section, could anyone give me pointers as to whether it's possible to extend so it somehow tallys based on url?

 

I'm guessing it'd need some way to extract the current url, send to sql table, check if it already exists, if so, add one to that row, if not add a new row.. ???

 

session_start();
include 'includes/dbconnect.inc.php';
if (!session_is_registered("counted")){
    mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id=1");
session_register("counted");
} 

Link to comment
Share on other sites

The tutorial simply counts the hits to the page on which the script runs. If it is on every page of the site, it counts every page hit. What you'll want to do is alter your table to store the current page's URL along with the hit count. That way, as the pages are hit, it updated distinctly. Your table would need to be something like this:

CREATE TABLE url_count (
  id integer unsigned auto_increment primary key,
  url text,
  count integer unsigned not null default 0
);

 

Then, your script would do something like this to see whether or not the current page exists. If it does, increment the count; if not, insert a new record. This way, as you add pages to your site, your count will automatically update itself:

 

<?php
$url = $_SERVER['REQUEST_URI'];
$sql = mysql_query("SELECT id FROM url_count WHERE url = '$url'");
if (mysql_num_rows($sql) == 0)
{
  // Doesn't exist, so create it
  mysql_query("INSERT INTO url_count (url, count) VALUES ('$url', 1)");
}
else
{
  // Exists, so update it
  mysql_query("UPDATE count SET count = (count + 1) WHERE url = '$url'");
}
?>

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.