delphi123 Posted November 28, 2007 Share Posted November 28, 2007 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"); } Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 28, 2007 Share Posted November 28, 2007 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'"); } ?> Quote Link to comment Share on other sites More sharing options...
delphi123 Posted November 28, 2007 Author Share Posted November 28, 2007 amazingly helpful! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.