Jump to content

Unique PHP Hit Counter


phpBeginner06

Recommended Posts

I am trying to create a unique hit counter with the script below (that I found on another website). I can get the script to set the cookie, but it will not send mysql_query UPDATE to the "ur_hits" data table; if the cookie does not exist (like the script should be doing).


[u]Data Table[/u]

[code]
CREATE TABLE ur_hits (
  page VARCHAR(30) NOT NULL,
  ref VARCHAR(40) NOT NULL,
  user_id INT UNSIGNED,
  counter INT UNSIGNED,
  PRIMARY KEY (page, user_id)
  );
[/code]



[u]PHP Hit Counter & HTML Code[/u]

[code]
<?
$db=mysql_connect("localhost","username","password");

mysql_select_db("Statistics",$db);

if (!isset($user_id)) {
  $user_id=get_new_user();
  setcookie ("user_id", $user_id, time()+315360000, "", "http://www.mydomain.com, 0);
  }
record_hit($user_id, $PHP_SELF);

mysql_query("UPDATE ur_hits SET counter=counter+1 WHERE page = '$PHP_SELF' AND user_id=\"$user_id\" ",$db);
?>
<html>
<head>
       
<title>Untitled</title>

</head>
<body>


</body>
</html>
[/code]


Does any one know what I am doing wrong?
If so, how can I fix it and/or get this unique hit counter to work?
Link to comment
Share on other sites

The script you provide did not give any type of results; it was just a blank page. Which PHP Configuration file do I need to open too make sure register_globals set to "on"? If I set it to "on" will my script work?

PS: I have MySQL 4.1.14 ruuning on my server; will the script I am using work with this version? Do I need a updated version of MySQL for my script to work; could that be the problem?
Link to comment
Share on other sites

Look at www.php.net/mysql for info on how to return error codes.

You could to then,

$query = mysql_query("UPDATE ur_hits SET counter=counter+1 WHERE page = '$PHP_SELF' AND user_id=\"$user_id\" ",$db);

if (!$query)
{
    // return the mysql error
}

Also, just put echo "Testing"; just above the mysql_query line, that will let you know that the script is making it to that section...

Cheers,
Dave
Link to comment
Share on other sites

It must not be making it to that section; because it will not echo anything before mysql_query. Why would it not be making it to this section; does anyone know?

As a reference to this script that I found; here is the link below. Maybe I just am not coding it right; like the script shows how too, but it seems like I am (I don't know).

[url=http://trkwebhosting.com/modules/free-php-hit-counter-script.php]http://trkwebhosting.com/modules/free-php-hit-counter-script.php[/url]
Link to comment
Share on other sites

When I do MySQL queries I always have them in the form:

[code]
<?php

$query = mysql_query($strqry) or die("MySQL Error: <br />{$strqry}<br /> ". msyql_error());

?>
[/code]

That way if I have a mysql error, the query will die, it will spit out my actually query so I can see what it looks like and then the [code=php:0]mysql_error()[/code] tells me what is wrong with it.
Link to comment
Share on other sites

Well.....The functions get_new_user() and record_hit() are not PHP specific functions but are user-defined functions, and these two functions are not included anywhere in your code - therefore you update query is failing.


try putting this:

[code]
echo $user_id;
exit;
[/code]

after this:
[code]
$user_id=get_new_user();
[/code]

....and see what it outputs (I think you will find it will print nothing at all).

Looking a bit closer at you code I would assume the get_new_user() function (if it was present in your code) is meant to look up a database table and obtain the id for the user.....

You may want to check out the site you got the code from in the first place.....

:)
Link to comment
Share on other sites

matto,

i figured it out, but i went a different route - but [u][b][color=red]!!! THANK YOU !!![/color][/b][/u] for all your help.

Below is the link to the final working script:

[url=http://www.phpfreaks.com/forums/index.php/topic,123428.0.html#msg511224]http://www.phpfreaks.com/forums/index.php/topic,123428.0.html#msg511224[/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.