Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Visit counter site in PHP (http://www.go4expert.com/forums/visit-counter-site-php-t26446/)

Scripting 5Aug2011 12:46

Visit counter site in PHP
 
In this article, I'll demonstrate how to create a very simple-counter for a web (WebCounter).

First of all, prepare the database - more accurately, create the necessary tables.

The table name will be "counter" and will have two columns: "id" - number of records to be added and automatically zoom in for each record in a table and "address" - IP addresses of visitors.

To do this, use the following SQL code (maybe you know where it is contracting;)):

Code: SQL
Code:

CREATE TABLE counter (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, address VARCHAR (15))
Now let's go on to our script. Generally, the script will work like this: Verifies whether an IP address is not yet in the database (which is the case of a new visitor), or IP address we already have (which means that the visitor was already here). In the first case, add a database to record and extract the highest "id", in the second case, just dump the "id".

Code: PHP

$ Remote_ip = $ _SERVER ['REMOTE_ADDR']; / / find the IP
/ / We are looking for it in the database:
$ Sql_query = "SELECT * FROM counter WHERE address = '$ remote_ip'";
$ Sql_res = mysql_query ($ sql_query);
/ / If the first time here - not a record:
if (mysql_fetch_array ($ sql_res)) {
/ / Add it to DB
$ Sql_query = "INSERT INTO counter (address) VALUES ('$ remote_ip')";
mysql_query ($ sql_query);
/ / Get the highest id
$ Id = mysql_fetch_array (mysql_query ("SELECT MAX (id) FROM counter"));
/ / And extract it:
echo "You are visitor #". $ id ['MAX (id)'];
} Else {/ / when he returns (which put to other cases) ...
/ / Get the highest id:
$ Count = mysql_fetch_array (mysql_query ("SELECT MAX (id) FROM counter"));
echo "You are visitor #". $ count ['MAX (id)'] / / and extract it
}


Do not forget to connect to database server (mysql_connect ('server', 'user', 'pass')), and select the database (mysql_select_db ('db_name')).

Of course, this simple code can be extended to - You can add there a last visit func, or counter of the views, etc.

P. S. I apologize for the terrible grammar ..., I had no time to correct it :(

Stay tuned for the further Articles!

shabbir 5Aug2011 17:25

Re: Visit counter site in PHP
 
Quote:

P. S. I apologize for the terrible grammar ..., I had no time to correct it :(
This is the most funny part of your article. Please don't write the wrong grammar then so you may not need to correct it. If you are very busy you may not write article in the first place because here no one forced you to write articles. Apart from that I have few more points to make.
  1. Every time you are submitting your articles in forums and not as articles. I can help you once to get the forum thread into article but you should be learning the process of writing articles.
  2. Make sure you write some thing unique to help fellow programmers and just for the sake of writing the content is not needed.
  3. Last but not the least is make sure you submit articles which needs almost no editing and your articles always need lot of them. Please make sure you submit only once you have reviewed your content.

Scripting 8Aug2011 02:21

Re: Visit counter site in PHP
 
Quote:

Originally Posted by shabbir (Post 85767)
This is the most funny part of your article. Please don't write the wrong grammar then so you may not need to correct it. If you are very busy you may not write article in the first place because here no one forced you to write articles. Apart from that I have few more points to make.
  1. Every time you are submitting your articles in forums and not as articles. I can help you once to get the forum thread into article but you should be learning the process of writing articles.
  2. Make sure you write some thing unique to help fellow programmers and just for the sake of writing the content is not needed.
  3. Last but not the least is make sure you submit articles which needs almost no editing and your articles always need lot of them. Please make sure you submit only once you have reviewed your content.

I'm sorry Shabbir, I thought I have a lot of time, but the something had run into and I had to post it as it was ... 1st I don't know how to submit articles, my bad, i know, sorry for that too, 2nd I'll try to write about something really interesting, i swear, I'll get it up! 3rd I tried my best, I truly apologize. I will get it right next time :)
PS: Thanks for editting my articles ...

pein87 9Aug2011 03:45

Re: Visit counter site in PHP
 
@Scripting
good first try, if you work on it or try something a bit different he might except it as an article. of course you need to post them in them here http://www.go4expert.com/forumdisplay.php?f=1

shabbir 9Aug2011 09:05

Re: Visit counter site in PHP
 
Quote:

Originally Posted by pein87 (Post 85855)
@Scripting
good first try, if you work on it or try something a bit different he might except it as an article. of course you need to post them in them here http://www.go4expert.com/forumdisplay.php?f=1

Its more simpler than that as well. In any forums you always see 2 buttons aka http://i.g4estatic.com/buttons/newtopic.gif & http://i.g4estatic.com/buttons/submitarticle.gif

Alex.Gabriel 16Jun2012 00:02

Re: Visit counter site in PHP
 
Nice code . Please add ";" after the last echo line :)

vinod1 8Jul2014 17:52

Re: Visit counter site in PHP
 
I impress after see the complete code by you. That's the nice after read your post. I's very good thing and informative for the developer point of view.


All times are GMT +5.5. The time now is 03:16.