Manual of PHP

 
 
 

Web map

 
rim   rim
arrowReady frontarrow of Codes PHParrow active Users with PHP

 

Active users with PHP To print E-mail
We calculate of a simple form the number of present visitors in our place.

In our PHP manual we tackle in his moment the use of meetings and we gave some practical example in which this type of variables can be used to give to our place a more dynamic aspect.

Many of you you could have seen in certain places a users' meter that they are in this moment sailing along the same pages as you. If you have noticed well, you could have observed that, for most of the cases (but the totality), the place in question is programmed by ASP as servant's language.

Really, ASP allows a more accessible management of the meetings by means of the famous file global.asa. This does not mean nevertheless, that PHP is unable to realize the same type of tasks but, rather, we have to work a little more to obtain them. In effect, all the languages have his pros and his contras and, in this particular case, ASP turns out to be more versatile although it is necessary to admit that, with his version 4, PHP has improved very much in everything what it concerns to the meetings treatment.

Here we show you a form simple to take into account the active users of your place using for this intention a table. Inside the above mentioned table, we will be storing the different numbers IP of the visitors of our place and the hour and date in which the visitor has executed for last time the script.

Also, the table has to go erasing progressively the meetings that have not been renewed in a time that we consider to be a limit. We have fixed above mentioned extreme in 24 minutes that it is the maximum time taken defect for PHP to suppress the information of a meeting. To modify this time of maximum life of a meeting it can be done in php.ini from the parameter session.gc_maxlifetime where we will express the above mentioned term in seconds. Eye, this maximum time is restored to his initial value whenever the user realizes a request to the servant, this means that a visitor will be able to sail how long wants for the place keeping the same meeting any time it does not remain any more than 24 minutes without realizing any type of action.

For the correct functioning of the script, it is necessary first of all to create a table in our database. This judgment SQL can help you in the task:

ONE BELIEVE TABLE control_ip (
ip VARCHAR (15) NOT NULL,
date INT (14) UNSIGNED NOT NULL,
INDEX (ip)
);

Since you see, the field ip, that stores the number IP of the visitor, it is index-linked. This will allow us a rapid selection. In compensation, like any index-linked field, his size in memory will be doubled which does not have a lot of importance since the table will have a quite limited number of records. The important thing in effect is that the script is executed quickly without consuming too many resources of the servant, topcoat bearing in mind that it is a question of a code that will be executed systematically in each of the pages of the place.

Let's happen to show next the script that we will use:

<?
////////////////////////////////////////////
//ACTIVE USERS
//He calculates the number of active users
////////////////////////////////////////////

function usuarios_activos ()
{
   //we allow the use of the carrying variable of the number ip in our function
   global $REMOTE_ADDR;

   //we assign a name memotecnico to the variable
   $ip = $REMOTE_ADDR;
   //we define the current moment
   $ahora = steal ();

   //we tune in to the database
   //Use your own parameters!!

   $conn = mysql_connect ($host, $user, $password);
   mysql_select_db ($db, $conn);

   //we update the table
   //erasing the records of the inactive ip (24 minutes)

   $limite = $ahora-24*60;
   $ssql = "delete from control_ip where it dates <".$limite;
   mysql_query ($ssql);

   //we look if the ip of the visitor exists in our table
   $ssql = "select ip, it dates from control_ip where ip = '$ip'";
   $result = mysql_query ($ssql);

   //if it exists we update the field it dates
   if (mysql_num_rows ($result)! = 0) $ssql = "update control_ip set it dates =".$ahora." where ip = '$ip'";
   //if it does not exist we insert the record corresponding to the new meeting
   else $ssql = "insert into control_ip (ip, it dates) value ('$ip', $ahora)";

   //we execute the judgment sql
   mysql_query ($ssql);

   //we calculate the number of meetings
   $ssql = "select ip from control_ip";
   $result = mysql_query ($ssql);
   $usuarios = mysql_num_rows ($result);

   //we liberate memory
   mysql_free_result ($result);

   //we return the result
   return $usuarios;
}
?>

You can observe, how it has being a norm, that the script is expressed in the shape of function. After defining the IP and the moment in which the script is being executed, we happen to intergesticulate with the database. Inside this process, we can distinguish different phases:

  • Connection with the database
  • Sweep of the table to eliminate the obsolete records.
  • Insertion or update of a record if the visitor is new or not.
  • Calculation of the number of records of the table



The function finishes liberating the space of memory used in his consultations and sending the result of the carried out calculation.

In general lines the script is of easy comprehension. It is possible that any of the used functions is not known you. If it is like that, come to the official page of PHP where you will be able to find details as for his employment.

To extract the value provided by the function to our principal script we will have to realize a classic call of the type:

$active_users = usuarios_activos ();

Is here finally a simple script that can give to your place an a little more dynamic image. Also, you can use it and improve it to create your own system of internal statistics. That we leave it to you...

Rubén Alvarez
http://www.desarrolloweb.com/articulos/615.php?manual=6

 

 
Front
Chapters of the Manual of PHP
Introduction to PHP
Variables
My First Script PHP
Operators in PHP
Structures of Control
Functions in PHP
Code inclusion
Counterfoils (array)
Chains of Characters
Classes
Dates
Entry and Exit
Operations with Files
The language SQL and PHP
Connection with MySQL
Meetings
Forms with PHP
To practise in line
List of practices in line
FAQ
Frequent questions
Codes PHP
List of Codes PHP
Forums
Forums PHP
Other Manuals
Manuals of other languages
 
   
 
 
MySpaceScripts.info - Sitemap - Contact us - Sitemap - Contact us - Sitemap - Contact us

Replica Handbags , phone card colombia , www.performance-media.com.br , bílé víno , GlobeSpot.Net Web Directory , Replica Watches , Buy templates