-
September 7th, 2004, 09:15 PM
#1
Senior Member
php question
guys i need to make a hit counter...
and the hit count to appear on a single html....
plz help
ty
-
September 7th, 2004, 09:29 PM
#2
What server do you have access to? Do you have access to a database, and what kind? How experienced are you with PHP, and how far did you get when trying to code it yourself?
BTW, if you really need one there are already tons of those available on the net. You could try hotscripts.com for instance, or good 'ol google. In my oppinion, hit counters don't really add any significant value to a website, though I created a lot of those when I was bored lol.
I could give you code but that wouldn't really teach you anything. IMO, the best way to learn from it is try to do it yourself, and when you can't figure something out ask around about the specific problem you're dealing with. If you just want code, try hotscripts, they usually have such things laying around.
Franky.
-
September 7th, 2004, 09:54 PM
#3
I wrote a simple PHP script that can do what he wants, but I fully surrender to Sick Dwarf's elitist questions.
Code:
<?php
$filename = "visitors.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents++;
echo($contents);
$fp = fopen('visitors.txt', 'w');
fwrite($fp,$contents);
?>
A text file will be created where-ever the file with the code is and it will keep the number of visitors. The echo() statement prints it out and you can use HTML to purrty it up.
- dave
-
September 8th, 2004, 01:24 AM
#4
Make sure the text file has the correct permissions to be written to if you want to use GW's script.
-
September 8th, 2004, 03:49 AM
#5
If you have access to a mysql db you might be able to use this.
includes/auth.php
PHP Code:
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "password";
$db_name = "yourdb";
?>
put this in your index page
PHP Code:
if(!isset($_COOKIE["hits"])){
setcookie("hits", "true", (time() + (60*30)));
updateHits();
}
what that does is it makes a cookie so that it doesn't increment the hit counter for every single hit, only one hit per 30 minutes per user. That way if someone sits there and refreshes it doesn't increment. But then again they could delete their cookies or not use them at all.
Lastly put this function in the index page also, the last piece of code as well as this once must be withing the <?php ?> tags.
PHP Code:
function updateHits(){
require("./includes/auth.php");
$connection = mysql_connect($db_host, $db_user, $db_pass) or die("Unable to connect!");
$query = "SELECT hits FROM hits";
mysql_select_db($db_name);
$result = mysql_query($query) or die ("Error in query.");
$oldhits = mysql_fetch_assoc($result);
$hits = $oldhits['hits'] + 1;
mysql_query("UPDATE hits SET hits = '".$hits."'") or die ("Error in query.");
$string = $hits;
$im = @imagecreate(50, 25)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 5, 5, 5, $string, $text_color);
imagejpeg($im, "/home/user/public_html/images/hits.jpg", 95);
imagedestroy($im);
}
that actually makes an image with the amount of hits inside it. You must have a table for hits though which you can make like this using the mysql client (make sure you're within the correct db by using "use yourdb")
Code:
create table hits (
hits smallint not null
);
And that in a nutshell is my hit counter. To make that image you must have gd enabled with your php and also maybe libjpeg. I hope you're able to make sense of that and that I didn't leave anything out. I was pulling bits of code from lots of places cause it's all within includes and stuff. Peace.
-
September 8th, 2004, 10:43 AM
#6
Senior Member
ty all... but to exlpain u what i want to do is:....
i have made this Private Servers Status to appear on a png file through fsockopen with php.
http://www.worldofwarcraft.gr/stats.php
and i want somewhere in the image to make appear the counter.
i will try making it with God's Whore way ... looks easier and then if i fail i will try the other ways....
ty guys
Sick Dwarf... well i have all the control of the server.... mysql4... for the php i know some things... but i don't have enough time to try making the counter by myseld due to school....
ty!!!!
-
September 8th, 2004, 11:01 AM
#7
Senior Member
ok i made it only because you helped me. Without you i wouldn't have made it
ty for the priceless help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|