电脑安全

您当前的位置:主页 > 电脑安全 >

PHP网站在线人数-网络编程

来源:[db:来源]标题:   在线    编辑:电脑技术全能网 时间:2019-09-23 12:34

原标题:PHP网站在线人数-网络编程
   刚用PHP写了一个网站在耳目数的顺序,请各人出去指导下!我是用PHP+MYSQL来写的,道理:网站在耳目数的顺序代码+后盾有MYSQL数据库支撑,能够间接统计出网站以后的在耳目数。起首我创立MYSQL数据库表。CREATE TABLE tablename (field type(max_length) DEFAULT 'default_value' (NOT) NULL}能够应用的SQL语句。CREATE TABLE useronline (timestamp int(15) DEFAULT '0' NOT NULL,ip varchar(40) NOT NULL,file varchar(100) NOT NULL,PRIMARY KEY (timestamp),KEY ip (ip),KEY file (file));上面咱们是PHP剧本,起首我界说MYSQL的信息。$server = "localhost"; //你的效劳器$db_user = "root"; //你的mysql的用户名$db_pass = "password"; //你的mysql的暗码$database = "users"; //表的名字设置统计的时光(几多秒内涵耳目数)$timeoutseconds = 300;取以后时光。$timestamp = time();下面的完全代码:<?php$server = "localhost"; //your server$db_user = "root"; //your mysql database username$db_pass = "password"; //your mysql database password if any$database = "users"; //the db name$timeoutseconds = 300;//timeoutseconds limit//get the current time$timestamp = time();//calculate the lowest timestamp allowed$timeout = $timestamp-$timeoutseconds;?>衔接mysqlmysql_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, "INSERT INTO useronline VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");假如用户用过错信息的话,如许处置。if(!($insert)) {print "Useronline Insert Failed > ";}而后完成当超越设置的时光就删除该用户记载。$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");一样给出删除记载犯错的处置。if(!($delete)) {print "Useronline Delete Failed > ";}上面咱们处理数据库中差别IP的成绩$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");应用mysql_num_rows(query);来统计用户,代码以下。$user = mysql_num_rows($result);最初封闭数据库。mysql_close();表现在线的人数。if($user == 1) {print("1 user online/n");} else {print("$user users online/n");}终极把下面代码写成一个PHP文件以下。<?php//Put your basic server info here$server = "localhost"; //normally localhost$db_user = "root"; //your MySQL database username$db_pass = "password"; //your MySQL database password$database = "users";$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last// $timeoutseconds seconds)//this is where PHP gets the time$timestamp = time();//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed$timeout = $timestamp-$timeoutseconds;//connect to databasemysql_connect($server, $db_user);//add the timestamp from the user to the online list$insert = mysql_db_query($database, "INSERT INTO useronline VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");if(!($insert)) {print "Useronline Insert Failed > ";}//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");if(!($delete)) {print "Useronline Delete Failed > ";}//select the amount of people online, all uniques, which are online on THIS page$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");if(!($result)) {print "Useronline Select Error > ";}//Count the number of rows = the number of people online$user = mysql_num_rows($result);//spit out the resultsmysql_close();if($user == 1) {print("1 user online/n");} else {print("$user users online/n");}?>

上一篇:PHP的AJAX技术实现文件异步上传-网络编程

下一篇:没有了