[英]Unknown error with user online script
我试图让这个脚本在 mysqli 中工作,脚本在 mysql 中很好地工作之前,所以我试图“重新制作它”,但它返回错误,我不知道该怎么办? :
未定义变量:conn
我做错了什么??
<?
$host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
$user = "my_user"; // your MySQL username
$pass = "mypassword"; // your MySQL password
$db = "my_db"; // the database to which you're trying to connect to
//start database
$conn = mysqli_connect($host,$user,$pass,$db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
class usersOnline {
var $timeout = 600;
var $count = 0;
var $error;
var $i = 0;
function usersOnline () {
$this->timestamp = time();
$this->ip = $this->ipCheck();
$this->new_user();
$this->delete_user();
$this->count_users();
}
function ipCheck() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function new_user() {
$insert = mysqli_query ($conn,"INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
if (!$insert) {
$this->error[$this->i] = "Unable to record new visitor\r\n";
$this->i ++;
}
}
function delete_user() {
$delete = mysqli_query ($conn,"DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
if (!$delete) {
$this->error[$this->i] = "Unable to delete visitors";
$this->i ++;
}
}
function count_users() {
if (count($this->error) == 0) {
$count = mysqli_num_rows ( mysqli_query($conn,"SELECT DISTINCT ip FROM useronline"));
return $count;
}
}
}
?>
尝试以下
<?
class usersOnline {
var $timeout = 600;
var $count = 0;
var $error;
var $i = 0;
var $conn;
public function __construct($conn) {
$this->conn = $conn;
}
public function usersOnline () {
$this->timestamp = time();
$this->ip = $this->ipCheck();
$this->new_user();
$this->delete_user();
$this->count_users();
}
public function ipCheck() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
public function new_user() {
$insert = mysqli_query ($this->conn,"INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
if (!$insert) {
$this->error[$this->i] = "Unable to record new visitor\r\n";
$this->i ++;
}
}
public function delete_user() {
$delete = mysqli_query ($this->conn,"DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
if (!$delete) {
$this->error[$this->i] = "Unable to delete visitors";
$this->i ++;
}
}
public function count_users() {
if (count($this->error) == 0) {
$count = mysqli_num_rows ( mysqli_query($this->conn,"SELECT DISTINCT ip FROM useronline"));
return $count;
}
}
}
?>
并且当您创建 usersOnline 的对象时,将 $conn 变量作为类似于以下的参数:
使用代码/类
$host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
$user = "my_user"; // your MySQL username
$pass = "mypassword"; // your MySQL password
$db = "my_db"; // the database to which you're trying to connect to
//start database
$conn = mysqli_connect($host,$user,$pass,$db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$obj = new usersOnline($conn);
<?
class usersOnline {
var $timeout = 600;
var $count = 0;
var $error;
var $i = 0;
// Declare the DB variable
protected $conn;
public function __construct(){
$host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
$user = "my_user"; // your MySQL username
$pass = "mypassword"; // your MySQL password
$db = "my_db"; // the database to which you're trying to connect to
//start database and set global db variable
$this->conn = mysqli_connect($host,$user,$pass,$db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function usersOnline () {
$this->timestamp = time();
$this->ip = $this->ipCheck();
$this->new_user();
$this->delete_user();
$this->count_users();
}
function ipCheck() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function new_user() {
// use global DB variable to get databse
$insert = mysqli_query ($this->conn,"INSERT INTO useronline(timestamp, ip) VALUES (now(), '$this->ip')");
if (!$insert) {
$this->error[$this->i] = "Unable to record new visitor\r\n";
$this->i ++;
}
}
function delete_user() {
$delete = mysqli_query ($this->conn,"DELETE FROM useronline WHERE timestamp < (DATE_SUB(now(), INTERVAL {$this->timeout} SECOND))");
if (!$delete) {
$this->error[$this->i] = "Unable to delete visitors";
$this->i ++;
}
}
function count_users() {
if (count($this->error) == 0) {
$count = mysqli_num_rows ( mysqli_query($this->conn,"SELECT DISTINCT ip FROM useronline"));
return $count;
}
}
}
?>
现在您的变量在类中,可以被您拥有的任何其他函数全面使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.