PHP实例教程:网站在线人数的程序代码
,后台有MY
SQL数据库支持
。可以直接统计出网站当前的在线人数
。    首先是创建MY
SQL数据库表。  
  CREATETABLEtablename(  
  fieldtype(max_length)DEFAULT'default_value'(NOT)NULL  
  }  
  可以使用的SQL语句。  
  CREATETABLEuseronline(  
  timestampint(15)DEFAULT'0'NOTNULL,  
  ipvarchar(40)NOTNULL,  
  filevarchar(100)NOTNULL,  
  PRIMARYKEY(timestamp),  
  KEYip(ip),  
  KEYfile(file)  
  );  
  下面我们开始使用
PHP脚本
,首先定义MYSQL的信息。  
  $server="localhost";//你的
服务器    $db_user="root";//你的
mysql的用户名  
  $db_pass="password";//你的
mysql的密码  
  $database="users";//表的名字  
  设置统计的时间(多少秒内在线人数)  
  $timeoutseconds=300;  
  取当前时间。  
  $timestamp=time();  
  上面的完整代码:  
  <?php  
  $server="localhost";//yourserver  
  $db_user="root";//yourmysqldatabaseusername  
  $db_pass="password";//yourmysqldatabasepasswordifany  
  $database="users";//thedbname  
  $timeoutseconds=300;//timeoutsecondslimit  
  //getthecurrenttime  
  $timestamp=time();  
  //calculatethelowesttimestampallowed  
  $timeout=$timestamp-$timeoutseconds;  
  ?>  
  连接mysql  
  mysql_connect('localhost','username','password');  
  也允许使用变量形式。  
  mysql_connect($server,$db_user,$db_pass);  
  如果mysql数据库没有密码的话可以使用下面代码连接(当然建议大家一定要设置好自己的密码,这样起码黑客得要解密啊)  
  mysql_connect($server,$db_user);  
  查询数据库的代码:  
  mysql_db_query('database','query');  
  我们只要有访客就要增加一条记录。  
  $insert=mysql_db_query($database,"INSERTINTOuseronlineVALUES  
  ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");  
  然后我们给出如果用户用错误信息的处理方式。  
  if(!($insert)){  
  print"UseronlineInsertFailed>";  
  }  
  然后我们得实现当超过我们设置的时间我们就要删除该用户记录。  
  $delete=mysql_db_query($database,"DELETEFROMuseronlineWHEREtimestamp<$timeout");  
  同样给出删除记录出错的处理。  
  if(!($delete)){  
  print"UseronlineDeleteFailed>";  
  }  
  下面我们显示数据库中有多少个不同的IP  
  $result=mysql_db_query($database,"SELECTDISTINCTipFROMuseronlineWHEREfile='".$_SERVER['PHP_SELF']."'");  
  我们使用mysql_num_rows(query);来统计用户,代码如下:  
  $user=mysql_num_rows($result);  
  最后我们要关闭数据库。  
  mysql_close();  
  显示在线的人数。  
  if($user==1){  
  print("1useronline
");  
  }else{  
  print("$userusersonline
");  
  }  
  最终把上面代码写成一个PHP文件如下。  
  <?php  
  //Putyourbasicserverinfohere  
  $server="localhost";//normallylocalhost  
  $db_user="root";//yourMySQLdatabaseusername  
  $db_pass="password";//yourMySQLdatabasepassword  
  $database="users";  
  $timeoutseconds=300;//itwilldeleteallpeoplewhichhaven'trefreshed(soprobbablyare  
  //offlineorinactive)in$timieoutsecondstime(soitactuallychecksthepeoplethatareactiveinthelast  
  //$timeoutsecondsseconds)  
  //thisiswherePHPgetsthetime  
  $timestamp=time();  
  //countsthetimeout,allpeoplewhichhavebeenseenlastonlineinearlierthanthistimestamp,willgetremoved  
  $timeout=$timestamp-$timeoutseconds;  
  //connecttodatabase  
  mysql_connect($server,$db_user);  
  //addthetimestampfromtheusertotheonlinelist  
  $insert=mysql_db_query($database,"INSERTINTOuseronlineVALUES  
  ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");  
  if(!($insert)){  
  print"UseronlineInsertFailed>";  
  }  
  //deletethepeopleswhichhaven'tbeenonline/activeinthelast$timeoutsecondsseconds.  
  $delete=mysql_db_query($database,"DELETEFROMuseronlineWHEREtimestamp<$timeout");  
  if(!($delete)){  
  print"UseronlineDeleteFailed>";  
  }  
  //selecttheamountofpeopleonline,alluniques,whichareonlineonTHISpage  
  $result=mysql_db_query($database,"SELECTDISTINCTipFROMuseronlineWHEREfile='".$_SERVER['PHP_SELF']."'");  
  if(!($result)){  
  print"UseronlineSelectError>";  
  }  
  //Countthenumberofrows=thenumberofpeopleonline  
  $user=mysql_num_rows($result);  
  //spitouttheresults  
  mysql_close();  
  if($user==1){  
  print("1useronline
");  
  }else{  
  print("$userusersonline
");  
  }  
  ?>