PHP的在线用户计数器

 更新时间:2016年11月25日 15:58  点击:1526

现在我将告诉你如何看到有多少用户在浏览您的网站。有许多方法来显示它,但我试图保持它简单,干净。

首先,我们必须创建两个表,您的数据库教程。

CREATE TABLE `uonline` (
  `session` varchar(100) NOT NULL,
  `time` int(5) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这将创建两个表。一个是会议和其他的是时间。他们将我们的信息存储。

现在是PHP的时间。让我们一起创造我们的主要变数

<?php教程
session_start();
$ses = session_id();
$time = time();
$timech=$time-300;
session_start(); - This wills start a session
$ses = session_id(); - This will get our session's ID
$time = time(); - This will get the time
$timech=$time-300; - This will set our time to 5min

Now we have to connect to the database:


// Declare SQL login info
$host = "localhost";
$username = "";
$password = "";
$dbname = "";
 
// Now we are going to connect to our database
mysql教程_connect("$host", "$username", "$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>");

$host = "localhost"; - Your SQL's host name, usually it's "localhost"
$username = ""; - Your MySQL username
$password = ""; - Your MySQL password
$dbname = ""; - Your database's name

Now we have to look for existing sessions and get the number of sessions.


$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num = mysql_num_rows($result); 

$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'"); - This will select all info from session column where it's value is "$ses"
$num = mysql_num_rows($result); - It will tell us how many active records are in session column


if($num == "0"){
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");
}

if($num=="0"){ - If there's no records in session column then we must insert some records
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')"); - Inserts session's ID and time to database
}else{ - But if there was more records than 0, let's update their records
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'"); - Updates existing records
} - Ends If statement

Now let's find our info again from the columns:


$result3 = mysql_query("SELECT * FROM uonline");

$result3 = mysql_query("SELECT * FROM uonline"); - This will get all info from uonline table

It's time for showing how many users are looking your site:


$usersonline = mysql_num_rows($result3);
echo "There are: <b>".$usersonline."</b> users online";

$usersonline = mysql_num_rows($result3); - It will get the number of records in columns
echo "There are: <b>".$usersonline."</b> users online"; - This will show how many users are on your site

When the users have left, you must delete their records from database.


mysql_query("DELETE FROM uonline WHERE time<$timech");
?>

mysql_query("DELETE FROM uonline WHERE time<$timech"); - Deletes records from database when 5min has been thru.

And here's the full code:


<?php
session_start();
$ses = session_id();
$time = time();
$timech=$time-300; 
 
$host = "localhost";
$username = "";
$password = "";
$dbname = "";
 
mysql_connect("$host", "$username", "$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>");  
 
$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num = mysql_num_rows($result); 
 
if($num == "0"){
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");

 
$result3 = mysql_query("SELECT * FROM uonline"); 
 
$usersonline = mysql_num_rows($result3);
echo "There are: <b>".$usersonline."</b> users online";  
 
mysql_query("DELETE FROM uonline WHERE time<$timech");
?>

<?php
require ('resize_img.php');
//how to use the class:
//makes a simple thumbnail of an image of 100x100 and saves the image then outputs it.
$imgresize = new resize_img();

$imgresize->sizelimit_x = 100;
$imgresize->sizelimit_y = 100;
$imgresize->keep_proportions = true;
$imgresize->output = 'PNG';

if( $imgresize->resize_image( 'C:/xampp/htdocs/wannabe/image_bin/public/treeshadows_001.gif' ) === false )
{
  echo 'ERROR!';
}
else
{
  $imgresize->save_resizedimage( 'C:/xampp/htdocs/wannabe/image_bin/public/thumbnails/', 'treeshadows_001' );
  $imgresize->output_resizedimage();
}

$imgresize->destroy_resizedimage();

?>

// resize_img.php文件

<?php

/**
 * by Daniel M. Story <admin@danstory.com>
 */

class resize_img
{
  var $image_path = '';
  //holds the image path
  var $sizelimit_x = 250;
  //the limit of the image width
  var $sizelimit_y = 250;
  //the limit of the image height
  var $image_resource = '';
  //holds the image resource
  var $keep_proportions = true;
  //if true it keeps教程 the image proportions when resized
  var $resized_resource = '';
  //holds the resized image resource
  var $hasGD = false;
  var $output = 'SAME';
  //can be JPG, GIF, PNG, or SAME (same will save as old type)
 
  function resize_img()
  {
    if( function_exists('gd_info') ){ $this->hasGD = true; } 
  }
 
  function resize_image( $image_path )
  {
    if( $this->hasGD === false ){ return false; }
    //no GD installed on the server!
   
    list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );
    //this is going to get the image width, height, and format
   
    if( ( $img_width != 0 ) || ( $img_width != 0 ) )
    //make sure it was loaded correctly
    {
      switch( $img_type )
      {
        case 1:
          //GIF
          $this->image_resource = @imagecreatefromgif( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'GIF'; }
          break;
        case 2:
          //JPG
          $this->image_resource = @imagecreatefromjpeg( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'JPG'; }
          break; 
        case 3:
          //PNG
          $this->image_resource = @imagecreatefrompng( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'PNG'; }
      }
      if( $this->image_resource === '' ){ return false; }
      //it wasn't able to load the image
    }
    else{ return false; }
    //something happened!
   
    if( $this->keep_proportions === true )
    {
      if( ($img_width-$this->sizelimit_x) > ($img_height-$this->sizelimit_y) )
      {
      //if the width of the img is greater than the size limit we scale by width
        $scalex = ( $this->sizelimit_x / $img_width );
        $scaley = $scalex;
      }
      else
      //if the height of the img is greater than the size limit we scale by height
      {
        $scalex = ( $this->sizelimit_y / $img_height );
        $scaley = $scalex;
      }

    }
    else
    {
      $scalex = ( $this->sizelimit_x / $img_width );
      $scaley = ( $this->sizelimit_y / $img_height );
      //just make the image fit the image size limit
     
      if( $scalex > 1 ){ $scalex = 1; }
      if( $scaley > 1 ){ $scaley = 1; }
      //don't make it so it streches the image
    }
   
    $new_width = $img_width * $scalex;
    $new_height = $img_height * $scaley;
   
    $this->resized_resource = @imagecreatetruecolor( $new_width, $new_height );
    //creates an image resource, with the width and height of the size limits (or new resized proportion )
   
    if( function_exists( 'imageantialias' )){ @imageantialias( $this->resized_resource, true ); }
    //helps in the quality of the image being resized
   
    @imagecopyresampled( $this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );
    //resize the iamge onto the resized resource
   
    @imagedestroy( $this->image_resource );
    //destory old image resource
   
    return true;
  }
 
  function save_resizedimage( $path, $name )
  {
    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        @imagegif( $this->resized_resource, $path . $name . '.gif' );
        break;
      case 'JPG':
        //JPG
        @imagejpeg( $this->resized_resource, $path . $name . '.jpg' );
        break; 
      case 'PNG':
        //PNG
        @imagepng( $this->resized_resource, $path . $name . '.png' );
    }
  }
 
  function output_resizedimage()
  {
    $the_time = time();
    header('Last-Modified: ' . date( 'D, d M Y H:i:s', $the_time ) . ' GMT');
    header('Cache-Control: public');
   
    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        header('Content-type: image/gif');
        @imagegif( $this->resized_resource );
        break;
      case 'JPG':
        //JPG
        header('Content-type: image/jpg');
        @imagejpeg( $this->resized_resource );
        break; 
      case 'PNG':
        //PNG
        header('Content-type: image/png');
        @imagepng( $this->resized_resource );
    }
  }
 
  function destroy_resizedimage()
  {
    @imagedestroy( $this->resized_resource );
    @imagedestroy( $this->image_resource );
  }

}

?>

此脚本时,页面最后修订和产出作为二十分钟前,或XX天前的日期...或者即使您不更新太多 - 二十周前显示!

//File Name
$last_modified = filemtime("FILE.php");
 
{
$timediff = time() - $last_modified;
 
if ($timediff < 3600)
{
if ($timediff < 120)
{
$returndate = "1 minute ago.";
}
else
{
$returndate = intval($timediff / 60) . " minutes ago.";
}
}
else if ($timediff < 7200)
{
$returndate = "1 hour ago.";
}
else if ($timediff < 86400)
{
$returndate = intval($timediff / 3600) . " hours ago.";
}
else if ($timediff < 172800)
{
$returndate = "1 day ago.";
}
else if ($timediff < 604800)
{
$returndate = intval($timediff / 86400) . " days ago.";
}
 
else if ($timediff < 1209600)
{
$returndate = "1 week ago.";
}
else if ($timediff < 3024000)
{
$returndate = intval($timediff / 604900) . " weeks ago.";
}
else
{
$returndate = @date('n-j-Y', $timestamp);
if($type=="fulldate")
{
$returndate = @date('n-j-y, H:i', $timestamp);
 
}
 
else if ($type=="time")
{
 
$returndate = @date('H:i', $timestamp);
 
}
 
}
//Display It
print("Last Modified: ");
print($returndate);
 
}
?>

它应该很容易理解。

$ last_modified = filemtime(“FILE.php”); - 更改FILE.php到文件您要连接。

确定了PHP脚本所需的时间执行正确的一微秒。

插入在页面顶部的代码:

<?php 
          $mtime = microtime(); 
          $mtime = explode(' ', $mtime); 
          $mtime = $mtime[1] + $mtime[0]; 
          $starttime = $mtime; 
?>
然后添加在页面底部的以下内容:

<?php 
          $mtime = microtime(); 
          $mtime = explode(" ", $mtime); 
          $mtime = $mtime[1] + $mtime[0]; 
          $endtime = $mtime; 
          $totaltime = ($endtime - $starttime); 
          echo 'This page was created in ' .$totaltime. ' seconds.'; 
?>

PHP的microtime中()函数返回当前的Unix时间戳微秒。

 

有时,您可能希望让无条件循环的开始,并允许括号内的语句来决定何时退出循环。

有两个特殊的语句可用在循环使用:中断和继续。

break语句终止或For循环的同时,继续执行现行的代码如下循环后(如有)。或者,

你可以把一个数字后,折价关键字,说明如何循环结构的多层次,以摆脱。这样,埋

藏在一份声明中深层嵌套的循环可以打破最外层循环。

<?php
echo "<p><b>Example of using the Break statement:</b></p>";

for ($i=0; $i<=10; $i++) {
   if ($i==3){break;}
   echo "The number is ".$i;
   echo "<br />";
}

echo "<p><b>One more example of using the Break statement:</b><p>";

$i = 0;
$j = 0;

while ($i < 10) {
  while ($j < 10) {
    if ($j == 5) {break 2;} // breaks out of two while loops教程
    $j++;
  }
  $i++;
}

echo "The first number is ".$i."<br />";
echo "The second number is ".$j."<br />";
?>

continue语句终止了在语句块执行一或For循环的同时,继续对下一个迭代循环的执行

<?php
echo "<p><b>Example of using the Continue statement:</b><p>";

for ($i=0; $i<=10; $i++) {
   if (i==3){continue;}
   echo "The number is ".$i;
   echo "<br />";
}
?>

[!--infotagslink--]

相关文章

  • php 获取用户IP与IE信息程序

    php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • php根据用户语言跳转相应网页

    当来访者浏览器语言是中文就进入中文版面,国外的用户默认浏览器不是中文的就跳转英文页面。 <&#63;php $lan = substr(&#8194;$HTTP_ACCEPT_LANGUAGE,0,5); if ($lan == "zh-cn") print("<meta http-equiv='refresh' c...2015-11-08
  • js检测用户输入密码强度

    一个用Javascript检测用户输入密码强度的效果代码,以下代码主要是从以下四个方面检测用户输入的密码的强度的,有兴趣的朋友可以自己添加或修改成自己想要的形式! 1. 如果输入的密码位数少于5位,那么就判定为弱。 2. 如果...2015-10-23
  • php有效防止同一用户多次登录

    【问题描述】:同一用户在同一时间多次登录如果不能检测出来,是危险的。因为,你无法知道是否有其他用户在登录你的账户。如何禁止同一用户多次登录呢? 【解决方案】 (1) 每次登录,身份认证成功后,重新产生一个session_id。 s...2015-11-24
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • php ajax注册验证用户名是否存在代码

    这是注册程序是一款当用户输入完用户名是,就会自动去数据库中查询用户要注册的用户名是否己经被注册了,如果是返回提示否则提示可以注册。 conn.php文件 代...2016-11-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • 微信小程序用户授权最佳实践指南

    这篇文章主要给大家介绍了关于微信小程序用户授权最佳实践的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-08
  • golang与php实现计算两个经纬度之间距离的方法

    这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
  • PHP如何使用cURL实现Get和Post请求

    这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
  • 谈谈PHP中相对路径的问题与绝对路径的使用

    经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24