|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与, PHP实例教程:网站在耳目数的法式代码,后台有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();
下面的完全代码:
以下为援用的内容:
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
毗连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, "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 database
mysql_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 results
mysql_close();
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}
?>
根据功能来进行封装等。很多的不懂,在使用搜索引擎查找,或者请教老师和在老师详细的讲解、指导下,都能顺利解决。 |
|