|
|
#1 (permalink) |
|
Senior Member
Join Date: Nov 2006
Posts: 225
|
Hi, i want to use php & mysql to make a counter that tells ppl how many people on online at that moment. like a visitor counter kinda thing. DO you know how to create one / when to get one from? as well i need help with bookmark this page now and send this page to a friend, anything?? thanks a lot!!
roy77 |
|
|
|
|
#2 (permalink) |
|
Senior Member
|
Well, you can use Extremetracking or Google Analytics
These are my favorites, the first one is visible (public) and other one is private and they have very good performances and results! |
|
|
|
|
#3 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
Creating a PHP Hit Counter with MySQL.
This tutorial is a fairly straightforward introduction to using MySQL with PHP. We're going to build you a hit counter for your site. We will expand on this script in future tutorials, but for now, we're going to keep things extremely simple, with a few variables, some if statements, perhaps a loop or two, and a few of PHP's MySQL functions. If you've never used, or even heard of, MySQL before then you will need to check with your web hosting company to make sure that your account supports MySQL databases, and that you know the following pieces of information before we start. - MySQL Hostname (usually "localhost"). - MySQL Account Username - MySQL Account Password - MySQL Database Name (you may have multiple databases, so, check with your host if you're not sure what you're doing). Also check, with your hosting company, what means you have at your disposal to access your database already. If you have a CPanel based hosting account, you will probably have access to phpMyAdmin, or some other similar such utility. If your account does not come with phpMyAdmin, or an alternative, you can download it from the phpMyAdmin Website. How to use phpMyAdmin and other tools for accessing your database are beyond the scope of this tutorial, and is something you should do some reading up on at the phpMyAdmin and MySQL websites if you're not too sure of what you're doing. This is not a MySQL tutorial, this is simply a PHP tutorial explaining how to connect to a MySQL database, read/write data, and display that data. Anyway, assuming you have phpMyAdmin setup, or another alternative for accessing your MySQL database, you need to enter the following in order to create the table we're going to use. PHP Code:
- page_id : As each new URL is discovered by the hit counter, MySQL will automatically assign it a page_id. This value is used in various parts of the program to help distinguish between one database record and another - page_name : This is the filename of the page being loaded. - page_hits : This is the number of times the particular page has been loaded. As I have said, this is a very basic introduction, and we aren't going to get into things like sessions and reload detection (when a user keeps reloading the same page over and over to bump up the hits), but this should get you started. So, let's create a file called hitcount.php, and insert the below code into it. PHP Code:
We learned from our basic PHP introduction that chunks of PHP code are opened with <?php and closed with ?> (a tag we've neglected to include at this point), and we've also learned that all lines in PHP end with the ; character. So, let's have a look at the remainder of the code. You'll see the first 4 lines of code all contain what looks like a variable, but what's with all the ['host'], etc? Well, this particular type of variable is called an array. A variable is a container, and an array is a variable, however an array is a container of containers. The actual variable name is "dbase", signified as a variable by the $ symbol at the beginning of the word. Whenever you see a variable with ['something'] after its name means that we're setting or reading a value inside an array of variables. This particular array variable contains four values. "host" is the hostname of your MySQL server, "user" is the username you use to login, "pass" is, of course, your password, and "name" is the name of the database you're going to be accessing. You can read more about arrays at the PHP Website. Now, let's have a look at the two functions we've called, mysql_connect() and mysql_select_db(). mysql_connect(), as the name implies, connects you to the MySQL server and logs you in. It accepts up to 5 parameters, however we're only interested in the first three, which are the hostname to which we wish to connect, the username we want to login with, and the password belonging to that username. These are signified by $dbase['host'], $dbase['user'] and $dbase['pass']. The mysql_select_db() function accepts up to 2 parameters, and here we're going to use both of them. The first parameter is the name of the database we wish to connect to, and the second is an identifier for MySQL queries, so we know which MySQL connection we want to use to select this database. The database name is defined by the $dbase['name'] variable. The second parameter is described below. Now, you'll notice in I neglected to mention it when we spoke about the mysql_connect() line, but there's another variable, $dblink. This variable is called a resource identifier, which is kind of like giving a name to your connection. In this example using a resource identifier is not so essential, but if your script connects to multiple databases, or even the same database with multiple different username & password combinations, resource identifiers are essential so that when you query the database, your script knows which MySQL connection to use. The more advanced amongst you will notice that we've added no error checking in this script. Error checking is something that we'll go into in a future tutorial, or if you wish to see more information, and example source, regarding MySQL's error checking when attempting to connect and select a database, please see Example 1 at the mysql_select_db() page at PHP.net. So, assuming our login details were correctly defined in the array variable, if we upload hitcount.php to our web server and load it in a browser, we should be confronted with a nice empty page. If this is what you see, then all is well. If you saw anything other than a blank page, then you're probably seeing error messages, so check you haven't made any typos, and that your login information is accurate. Now that we're logged in, what can we do? Well, the first thing we want to do is determine the URL of the page we've just loaded. This is simply done by using the $_SERVER global variable that PHP so considerately provides for us. PHP Code:
So, now we have the page name, so let's do something with it. The first thing we need to do is find out if the path already exists in the database. This is done with a simple MySQL query. Add these lines to the end of your hitcount.php file. PHP Code:
PHP Code:
PHP Code:
First the if statement. "If" checks the value of something, and if that value returns true, performs a specified action. If the condition is not true, you can have it check something else, or several something elses, or simply perform a different set of actions. In this case, we're saying "if ($data)". Which, in plain English, basically means "if $data exists do something". As I said earlier, if the current URL previously exists in the database, $data will have something contained within it, so it will exist, and return true. If a result was not pulled from the database, and this page is being loaded for the first time, $data will not exist, and the condition will return false, and the first set of actions will not be performed. So, what's done if something is stored inside $data? Well, the first thing we do is increment the number of hits the page has had. If you have an integer (a whole number) stored inside a variable, you can add ++ to the end of it to have it increment by 1, which is what we've done here. The next thing we do is to create another MySQL query that will update the existing record in the database. As we've already got something stored inside $data, it's generally more reliable to use $data['page_id'] as a comparison rather than the path of the page. So, we update the record with the new hit count. Then we execute this query using the $sql variable contents. And if the page was not already in the database and $data does not exist? Well, here we insert a new record into the database, and give it 1 hit. We don't use a value for page_id, as this is automatically generated by MySQL when we create a new record due to the auto_increment flag set on this field. Again, we store the query inside the $sql variable, then run that query using the mysql_query() function. And finally you'll notice that we close our script with the ?> tag. So, before I carry on, here's all the code in a single chunk. PHP Code:
PHP Code:
By the way, as we haven't mentioned them before, those three lines starting with // are comments. There are two main ways to define a comment, either by using //, which comments everything from that point on to the end of the line, and using /* */ pairs. Using /* */ your comments can span multiple lines. Thus, those three lines above could've been written as : PHP Code:
Part 2 of this tutorial will detail various methods we can use to actually display the number of hits your pages have received. But, if you go ahead and set this up on your site now, you can start collecting some hits statistics for us to play with in the next tutorial. Have fun! |
|
|
|
|
#4 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
Also look here: http://www.betlik.com/script-visitor-counter-t697.html
|
|
|
|
|
#8 (permalink) |
|
Senior Member
Join Date: Oct 2006
Posts: 216
|
visit to http://www.www.hotscripts.com/PHP/Sc...ers/index.html
you can find there many visitor counter. |
|
|