prcollin Posted June 5, 2008 Share Posted June 5, 2008 Thought it was good but when i upload the files to my server the resultant display is just <?php on the website this is index.php (just for testing purposes its a new server) <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); echo $hits[0]; ?> with hitcounter.txt just being <?php include("index.php"); ?> Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/ Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 would be fine but you may want to revise it to store hits basde on some kind of time limit and ip address. Otherwise the person can sit there and hit refresh and you will have 5000 hits in no time. Ray Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558392 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 would be fine but you may want to revise it to store hits basde on some kind of time limit and ip address. Otherwise the person can sit there and hit refresh and you will have 5000 hits in no time. Ray First problem is do you know why it might only be displaying "<? php" when I visit my page instead of the hit number? Second I dont know how to set the limit yet this is my first hit counter so I will have to research into that. Thanks thogh for bringing that to my attention Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558395 Share on other sites More sharing options...
trq Posted June 5, 2008 Share Posted June 5, 2008 If its displaying <?php then it seems more likely your server isn't configured to handle php. Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558396 Share on other sites More sharing options...
fanfavorite Posted June 5, 2008 Share Posted June 5, 2008 The only thing that should be in the hitcounter.txt file is a number. <?php $count_my_page = "hitcounter.txt"; //file to open $hits = file($count_my_page); //reads the file into an array $hits[0] ++; //Add's 1 to the first array $fp = fopen($count_my_page , "w"); //opens hitcounter.txt for writing fputs($fp , "$hits[0]"); //writes the new value to the hitcounter.txt fclose($fp); //closes the file echo $hits[0]; // shows the new value ?> Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558397 Share on other sites More sharing options...
DarkWater Posted June 5, 2008 Share Posted June 5, 2008 Make sure your server has PHP installed. Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558398 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 Make sure your server has PHP installed. Yeah it does php5 its all through hostgator reseller account so php, mysql, apache, curl all that is already isntalled Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558401 Share on other sites More sharing options...
fanfavorite Posted June 5, 2008 Share Posted June 5, 2008 Guys the problem is not php. It obviously is enabled with the result. He is getting <?php because the .txt file lines are: <?php include("index.php"); ?> which: $hits[0] = "<?php" $hits[1] = "include(\"index.php\");" $hits[2] = "?>" All that needs to be done is put a number in the txt file. You can't use php in a .txt file unless you specify the header. Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558402 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 yeah fixed it it works fine now. Now my only problem is how to set it so it only accepts a count from an ip adresss once every 2 hours Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558406 Share on other sites More sharing options...
fanfavorite Posted June 5, 2008 Share Posted June 5, 2008 $_SERVER['REMOTE_ADDR'] gets the ip. Now you need to store the ip's in a database or txt file. Refer to the comments for each line of the code that I posted to figure out how to store the ip address and post back if you need help. Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558411 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 $_SERVER['REMOTE_ADDR'] gets the ip. Now you need to store the ip's in a database or txt file. Refer to the comments for each line of the code that I posted to figure out how to store the ip address and post back if you need help. there currently is no db for the counter yet I will have to create on and get back to u but i think i will do something like Table CREATE TABLE unique ( page VARCHAR(30) NOT NULL, counter INT UNSIGNED, address VARCHAR(30) not null PRIMARY KEY (page) ); php script Hmm ill figure that one out in a bit and post when i get the php script done and you can tell me if its right or not off the top of my head i cant generate the script even though its prolly extremely simple. Trying to learn vb2008, php, C++ and C# all at once is bad for trying to remember simple scripts lol Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558457 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 I am compeletly lost on how to do it so that it restricts unique hits to once every two hours by same ip adress Can any help me out i have googled it and read soemother posts but none of them that i got off of google inlude $_SERVER[REMOTE_ADDR] because i cant find one that does unique by ip address, and def not one that restricts by time Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558485 Share on other sites More sharing options...
Rohan Shenoy Posted June 5, 2008 Share Posted June 5, 2008 ^use a cookie and then log the hit using some conditional statements as you wish Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558491 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 ^use a cookie and then log the hit using some conditional statements as you wish yeah not at cookies yet. Maybe can I have a date function that logs the date a user visits the site and say if id == 'userid' and $date < 1 then $count_my_page = "hitcounter.txt"; //file to open $hits = file($count_my_page); //reads the file into an array $hits[0] ++; //Add's 1 to the first array $fp = fopen($count_my_page , "w"); //opens hitcounter.txt for writing fputs($fp , "$hits[0]"); //writes the new value to the hitcounter.txt fclose($fp); //closes the file echo $hits[0]; // shows the new value else echo $hits[0]; (and just not run any script to add to the file) Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558495 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 you should also log the time. then you can just query the database for the ip address and the last time. if it is past the time then insert it. We will call the time field `lasttime` <?php $ip = $_SERVER['REMOTE_ADDR']; $time = strtotime("now - 2 hours"); $sql = "SELECT COUNT(`page`) AS `count` WHERE `lasttime` >= '$time' AND `address` = '$ip'"; $res = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_assoc($res); $count = $r['count']; if($count < 1){ //insert new row here } ?> Ray Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558501 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Share Posted June 5, 2008 O.o x.x What’s The Difference Between Page Views, Hits And Unique Visitors This is all obvious if you know it, but it is surprising how any people don’t. Ask people you think should know the difference - this is a good question to ask a web designer you are considering hiring. If they get it wrong, I suggest you find another one. Unique Visitors A visitor, or rather a unique visitor is defined as a single person who visits your web site in a single session. So, if you visit a site and you spend say 15 minutes browsing various pages on it - say you visit a total of 25 different pages in that time, that would count as a single visit. Page Views The page view count is the actual number of pages that are made in the timeframe. So if you have 400 page views say, on a single day, that would mean that all your visitors together, viewed pages a total of 400 times. In the example we used earlier we would say that the single visitor who visited 25 pages, would have contributed at least 25 page views. This is because I said he visited 25 different pages, but that doesn’t mean he didn’t visit some of them more than once. In other words he may have contributed maybe 30 or 40 page views. And Finally, Hits . . . So what are these famous hits? Hits are not really of any use to you. But I’ll tell you what they are. Imagine there is a webpage called dogs.html, and on that page is some text and 10 images of dogs. Let’s also say that dogs.html uses a separate CSS file called dogs.css. If you go and view that page, you will incur 12 hits just by viewing that page once. That is because dogs.html counts as one hit, the 10 images count as a further 10 hits and the CSS file counts as one hit, giving a total of 12 hits. Now say you refresh the page. You will incur another 12 hits. You will add 12 hits to the hit count every time you hit refresh. If 5 people visit the page once, that would make 60 hits. Can you see how hits have no meaningful relationship to unique visitor numbers? Now imagine the page dogs.html had 100 thumbnail images on it. This would mean that every time the page was viewed it would cause 102 hits to be added to the hit count. No wonder the guy at the dinner party thought he was getting a lot of visitors because he had a lot of hits. It is likely he thought hits were visitors. http://www.lizjamieson.co.uk/2008/03/18/how-many-hits-does-your-website-get-per-day/ ------------------------------------------------------------------------------------------------------------------- So if you want a hit counter on your site, you should really make it count what it says it's counting, otherwise people will really not have a clue what's going on. =^.^= Fun times! Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558506 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 you should also log the time. then you can just query the database for the ip address and the last time. if it is past the time then insert it. We will call the time field `lasttime` <?php $ip = $_SERVER['REMOTE_ADDR']; $time = strtotime("now - 2 hours"); $sql = "SELECT COUNT(`page`) AS `count` WHERE `lasttime` >= '$time' AND `address` = '$ip'"; $res = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_assoc($res); $count = $r['count']; if($count < 1){ //insert new row here } ?> Ray great thanks I will try it out asap! Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558512 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 ok i created a table called unique added the fields date time ipaddress counter page entered craygo code but what do i add so that it adds a new line and do i just echo 'count' after the $count line and then also after the if statement? Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558566 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 lol you can just store the date and time together store it as a timestamp. <?php $ip = $_SERVER['REMOTE_ADDR']; $time = strtotime("now - 2 hours"); $page = $_SERVER['PHPSELF']; $sql = "SELECT COUNT(`page`) AS `count` WHERE `lasttime` >= '$time' AND `address` = '$ip' AND `page` = '$page'"; $res = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_assoc($res); $count = $r['count']; if($count < 1){ //insert new row here $cur_time = time(); $insert = "INSERT INTO `unique` (`time`, `ipaddress`, `page`) VALUES ('$cur_time', '$ip', '$page')"; mysql_query($insert) or die(mysql_error()); } ?> No need for the counter field because you can use the count function of mysql do do it for you. I added page in up top also. Ray Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558591 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 ok this is my final script <?php include("connect.php"); mysql_connect('$host', '$username', '$password', '$database'); or die ("Couldn't connect to server."); $ip = $_SERVER['REMOTE_ADDR']; $time = strtotime("now - 2 hours"); $page = $_SERVER['PHPSELF']; $sql = "SELECT COUNT(`page`) AS `count` WHERE `lasttime` >= '$time' AND `address` = '$ip' AND `page` = '$page'"; $res = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_assoc($res); $count = $r['count']; if($count < 1){ //insert new row here $cur_time = time(); $insert = "INSERT INTO `unique` (`time`, `ipaddress`, `page`) VALUES ('$cur_time', '$ip', '$page')"; mysql_query($insert) or die(mysql_error()); } ?> i have the connection variables stored in my connect.php script and have index.php which simply is <?php include("counter.php"); ?> i get this error Parse error: syntax error, unexpected T_LOGICAL_OR in /home/freshout/public_html/counter.php on line 6 no clue what it means lol Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558616 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 you have to combine the connection. mysql_connect('$host', '$username', '$password', '$database'); or die ("Couldn't connect to server."); should be mysql_connect('$host', '$username', '$password', '$database') or die ("Couldn't connect to server."); Ray Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558638 Share on other sites More sharing options...
prcollin Posted June 5, 2008 Author Share Posted June 5, 2008 you have to combine the connection. mysql_connect('$host', '$username', '$password', '$database'); or die ("Couldn't connect to server."); should be mysql_connect('$host', '$username', '$password', '$database') or die ("Couldn't connect to server."); Ray now i get Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '$host' (1) in /home/freshout/public_html/counter.php on line 5 Couldn't connect to server. my connect.php looks like <?php $host= "localhost"; $username= "xxxxxx"; $password= "xxxxx"; $database= "xxxx"; ?> what do you think my problem is now beside me being a moron lol Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558688 Share on other sites More sharing options...
fanfavorite Posted June 6, 2008 Share Posted June 6, 2008 Seems that it is reading $host as a string and not as a variable. Try with the quotes: mysql_connect($host, $username, $password, $database) or die ("Couldn't connect to server."); Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-558863 Share on other sites More sharing options...
prcollin Posted June 6, 2008 Author Share Posted June 6, 2008 Seems that it is reading $host as a string and not as a variable. Try with the quotes: mysql_connect($host, $username, $password, $database) or die ("Couldn't connect to server."); now with this code <?php include("connect.php"); $dbh= mysql_connect ("$host", "$user", "$password") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("freshout_counter"); $ip = $_SERVER['REMOTE_ADDR']; $time = strtotime("now - 2 hours"); $page = $_SERVER['PHPSELF']; $sql = "SELECT COUNT(`page`) AS `count` WHERE `lasttime` >= '$time' AND `address` = '$ip' AND `page` = '$page'"; $res = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_assoc($res); $count = $r['count']; if($count < 1){ //insert new row here $cur_time = time(); $insert = "INSERT INTO `unique` (`time`, `ipaddress`, `page`) VALUES ('$cur_time', '$ip', '$page')"; mysql_query($insert) or die(mysql_error()); } ?> i get You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `lasttime` >= '1212757749' AND `address` = '24.147.126.116' AND `page` = '' at line 1 Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-559021 Share on other sites More sharing options...
fanfavorite Posted June 12, 2008 Share Posted June 12, 2008 Sorry I had a typo in my code. I mean't try WITHOUT quotes as you see in my example: mysql_connect($host, $username, $password, $database) or die ("Couldn't connect to server."); Link to comment https://forums.phpfreaks.com/topic/108856-is-this-a-good-hit-counter-script/#findComment-563970 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.