1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Visit counter site in PHP

Discussion in 'PHP' started by Scripting, Aug 5, 2011.

  1. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    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".

    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!
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    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 ...
     
  4. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Its more simpler than that as well. In any forums you always see 2 buttons aka [​IMG] & [​IMG]
     
    Scripting likes this.
  6. Alex.Gabriel

    Alex.Gabriel New Member

    Joined:
    Oct 23, 2011
    Messages:
    86
    Likes Received:
    7
    Trophy Points:
    0
    Occupation:
    Linux system administrator
    Location:
    Italy
    Home Page:
    Nice code . Please add ";" after the last echo line :)
     
  7. vinod1

    vinod1 New Member

    Joined:
    Jul 4, 2014
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    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.
     

Share This Page