1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Visit counter site in PHP

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

  1. Scripting

    Scripting John Hoder

    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
    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".

    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:
    "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"));
    "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

    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

    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

  5. shabbir

    shabbir Administrator Staff Member

    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

    Nice code . Please add ";" after the last echo line :)
  7. vinod1

    vinod1 New Member

    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