|
|
Make Your Own Very Simple Counter Using PHP! | ||
Discussion by websaint with 36 Replies.
Last Update: April 28, 2010, 6:58 pm ( View Rated (2) ) (View Latest) | Page 1 of 2 pages. | ||
![]() |
|
|
So here comes the script:
CODE
<?php//Simple class for counting visits
//Written by WebSaint
class Teller {
function count() {
$countfile = file("alle.txt");
$count = $countfile[0];
$count= $count + 1;
$fp = fopen("alle.txt","w");
$fw = fwrite($fp,$count);
fclose($fp);
echo $count;
}
}
$obj =& new Teller;
$obj->count();
?>
Then add this to display the number of visitors:
CODE
<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>That's it. I hope you'll find the script useful!
they will link to your counter script, and store their page cound on your server.
its always good to add some kind of proetection againsed this. a good, but not perfect way is to make sure the referer comes from your own site, and nobody elses.
QUOTE (qwijibow)
Good... but you might find others steal your counter if they find it...they will link to your counter script, and store their page cound on your server.
its always good to add some kind of proetection againsed this. a good, but not perfect way is to make sure the referer comes from your own site, and nobody elses.
Well it's impossible to "steal" this. If you include that file from Websaints server the code will be treated as it was run on your server and thus will try to look file alle.txt from your server. If you simply request that file it will only add hits to Websaints counter.
The referer checking would be pointless in this but I'd like comment it a bit in general. People do not seem to know or just forget that nowadays quite many users disable or block referer sending. In certain browsers it is an option and I believe most of the current commercial software firewalls include an option to block them. Not taking this into account can mean that users that do not want to send referers can't use the service at all. For instance one development version of the WordPress blog software required referer sending to make changing it's options possible. The referer checking was simply just for sending the user back to the page where he came from. Other example is a certain torrent site that refuses to work if not getting referer information. They reason is understandable though: there is apparently countless "mirror" sites that are not actual mirrors but simply index their files and points the user to download the .torrents from the original site.
QUOTE (Hercco)
Well it's impossible to "steal" this. If you include that file from Websaints server the code will be treated as it was run on your server and thus will try to look file alle.txt from your server. If you simply request that file it will only add hits to Websaints counter.The referer checking would be pointless in this but I'd like comment it a bit in general. People do not seem to know or just forget that nowadays quite many users disable or block referer sending. In certain browsers it is an option and I believe most of the current commercial software firewalls include an option to block them. Not taking this into account can mean that users that do not want to send referers can't use the service at all. For instance one development version of the WordPress blog software required referer sending to make changing it's options possible. The referer checking was simply just for sending the user back to the page where he came from. Other example is a certain torrent site that refuses to work if not getting referer information. They reason is understandable though: there is apparently countless "mirror" sites that are not actual mirrors but simply index their files and points the user to download the .torrents from the original site.
you misunder stand my meaning.......
i mean someone could replace
CODE
<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>the include file with a coomplete URL to anouther server. and replace "alle.txt" with a different filename.
CODE
<?php<?php
$now=fopen("count.php","r+");
while(!feof($now)){
$cont=fgets($now,2);
if ($cont=="N") {
$check=fgets($now,3);
if($check=="V=") {
$nvisit=fgets($now,4);
echo $nvisit;
settype($nvisit,"integer");
$posi=ftell($now);
fseek($now,($posi-2));
fwrite($now,($nvisit+1));
exit;
}
}
}
fclose($now);
?>
and a count.php like this
<?php
NV=34
?>
QUOTE (qwijibow)
CODE
<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>the include file with a coomplete URL to anouther server. and replace "alle.txt" with a different filename.
And that would accomplish what?
QUOTE (sha)
there are many scripting languages and you can find counter in each. first check which scripting is available on your server and then try to find its script for counter. i reccomond altavista.com rather than google.com for finding free scripts.We're DYI guys, we want to write our own scripts
Greets Deivid
You just have to put its folder in your main directory and include a one line of code in your webpage.
It will display How many visitors are online and complete statistics with IP address.
Sohail Ahmed
sohail4@msn.com
the code is simple just fetch the row with the counter...
and use echo display on the page...
why bother about the security... The data base is secured...
Yordan
OK, Darren, your are right, statcounter is really great. Their counters are not so nice as astahost ones, and the menus to step inside are not really easy to understand, and I experienced several disconnections in the middle of the process of creationg your counter. But if you want something really simple, without any ads, it's a simple line to be included inside your html source. And it works anywhere, including on your own PC for testing purposes, which is not the case for the astahost counter.
So, if you want something easy to implement, very nice, with a comfortable menu-diriven settings, use astahost's cpanel one. If you want something simple, which works on any site including your PC, use statcounter.
I've been trying to figure out this one though, how do we make a download counter, that one is a bit harder isn't it? I suppose we'd make a page that would open the file's URL in a redirect, but also add to the counter and direct them to that instead of the counter?
Although I like to use more heavy stats that give you greater detail in visitors.
You just have to:
CODE
CREATE TABLE website_counter(visits int(10))
And put this code in the top of each page that you want to be counted:
CODE
$conn = mysql_connect("YOUR HOST","YOUR USER","YOUR PASSWORD") or die('MySQL said: ' . mysql_error());$db = mysql_select_db($db);
$sql = mysql_query("SELECT * FROM website_counter") or die('MySQL said: ' . mysql_error());
while($visits = mysql_fetch_array($sql))
{
$visits = $dados["visits"];
$visits_new = $visits + 1;
}
$query = "UPDATE TABLE website_counter SET visits = $visits_new";
mysql_query($query, $conn) or die(mysql_error());
And where you want to show the number os visits:
CODE
echo ("Visits: $visits_new");OBS.: I didn't test it. If you got any errors, post here.
QUOTE
OBS.: I didn't test it.yes, it has to be tested first. For instance, I am curious to see where $dados is defined.
CODE
<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>
Is stored on the server and is not changeable or dependable on userinput.
Anyway, I was thinking that using an OOP approach here might be an overkill. What if you take the following PHP file
vc.php
CODE
<?php
$countfile = file("alle.txt");
$count = $countfile[0];
$count= $count + 1;
$fp = fopen("alle.txt","w");
$fw = fwrite($fp,$count);
fclose($fp);
echo $count;
}
?>
Basically you know that whenever vc.php is called there is a new page visit so which means that you need the variable $count back.
So all you need to do now in your other php files is to have them include("vc.php") in the code.
eg
index.php
CODE
<?php
echo("hi");
include("vc.php");
?>
This would be failsafe for any crosssitescripting attempts since there's no userinput expected. Also, even if the name of vc.php is known so people can call it directly, it doesn't matter since they could only +1 to your counter, which is also possible if they reload your page 20+ times. Also, people still need to know the name of the counter script, which is quite difficult to guess if they don't see it stated somewhere.
Alternatively, you could also add an ip banning system to this script to check if a user has accessed this site before. Which is not really necessary if you want to know how much your page has been loaded.
About the busy website thing, it'd take quite a lot of requests per second to be faster than the opening and writing of one byte in a single file for it to have a deadlock. Also, If one has a busy site that can accomplish such thing, it'd be wiser to use a database approach since you can generate more statistics than just the amount of page visits (which is only interesting with a lot of visitors).
Ps. Anything flash is bad
QUOTE (yordan)
@Alexandre : your program has a great advantage, you are independent from any provider, so you can use it on a server other than astahost, or even on a server on a private network.yes, it has to be tested first. For instance, I am curious to see where $dados is defined.
Sorry, is not $dados['visits'], but $visits['visits']
Sorry again for the mistake.
QUOTE (sohahm)
I know a very good free script named EliteStats.
You just have to put its folder in your main directory and include a one line of code in your webpage.
It will display How many visitors are online and complete statistics with IP address.
Sohail Ahmed
sohail4@msn.com
Link: view Post: 4590
These scripts are so good, personally i was using them before begining to learn PHP, since that time i don't like to put any ready scripts on my site, i prefer to create it myself, that's the joy of PHP and other scripting languages, so i think better to post these useful links to these ready-made scripts on a new topic and to leave us enjoying this scripting battle
Personally i prefer the second counter, i like the combination between PHP/MySQL, more secured and better than ordinary text file, but ofcourse the first one is more simpler one.
QUOTE (Alexandre Cisneiros)
OBS.: I didn't test it. If you got any errors, post here.
Link: view Post: 67865
it gives me this error:
CODE
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 'TABLE counter SET visits =' at line 1i tried a lot but never got a result
QUOTE
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 'TABLE counter SET visits =' at line 1hope anyone can tell us why it's not working.
QUOTE (XIII)
did anyone tried with this?, i tried alot but it keeps telling me there is an error :
hope anyone can tell us why it's not working.
Link: view Post: 74858
Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.
QUOTE (miCRoSCoPiC)
Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.
Link: view Post: 74863
That's the code for the whole script, ofcourse i'm putting the values for connect in my script, the counter table called "counter"
CODE
<?php$conn = mysql_connect("localhost","db_username","db_pssword") or die('MySQL said: ' . mysql_error());
$db = mysql_select_db("db_name");
$sql = mysql_query("SELECT * FROM counter") or die('MySQL said: ' . mysql_error());
while($visits = mysql_fetch_array($sql))
{
$visits = $visits["visits"];
$visits_new = $visits + 1;
}
$query = ("UPDATE TABLE counter SET visits = '$visits_new' ");
mysql_query($query, $conn) or die(mysql_error());
echo ("visits : $visits_new");
?>
QUOTE (XIII)
while($visits = mysql_fetch_array($sql))
{
$visits = $visits["visits"];
$visits_new = $visits + 1;
}
$query = ("UPDATE TABLE counter SET visits = '$visits_new' ");
mysql_query($query, $conn) or die(mysql_error());
Link: view Post: 74866
One error I can spot rightaway is with the $query statement. Why is the $visits_new variable within single quotes ? The function of the script is to update the table with the new value of $visits_new, right? In that case, it has to be enclosed with DOUBLE QUOTES in order for php to parse it and replace it with it's value.
Another factor that's confusing is that why's the statement enclosed by paranthesis ? ( $query = (...)) - those () are totally unnecessary here, although they shouldn't ideally cause a problem...
Try modifying that line to:
CODE
$query = 'UPDATE TABLE counter SET visits = "$visits_new" ';
Try this and let me know if it still produces the same error.
QUOTE (miCRoSCoPiC)
Try modifying that line to:
CODE
$query = 'UPDATE TABLE counter SET visits = "$visits_new" ';
Try this and let me know if it still produces the same error.
Link: view Post: 74868
Now i got the same error, really so strange case to me
QUOTE
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 'TABLE counter SET visits = "$visits_new"' at line 1
Similar Topics:
page counter with php+mysql
A Simple Checking amp Validation ...
Php Counter
(4) How To Create A PHP Based Hit Counter With MySQL
|
HOME 





PHP MySQL Tutorial: Create a Simple View Counter -HD-
PHP: Simple visitor counter
Create a simple hit counter using PHP and MySQL Part 1
Create a simple PHP and MySQL hit counter Part 2
PHP: Why simple counter via file won't work
Simple PHP/MySQL counter
How To Make A Hit Counter In PHP
PHP MySQL Tutorial: Create a Simple View Counter -HD-
How To Create a Hit Counter - Page Views (PHP) Part 1 of 2
ActionScript 3.0 Tutorial - Hit Counter Flash + PHP for full Flash website pages

