我正在尝试使这个脚本在mysqli中工作,在nice之前的脚本在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: %sn", 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 visitorrn";
$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;
// 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: %sn", 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 visitorrn";
$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;
}
}
}
?>
现在,您的变量位于类中,并且可以由您拥有的任何其他函数全面使用。
请尝试以下操作
<?
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 visitorrn";
$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;
}
}
}
?>
当您创建用户在线对象时,将$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: %sn", mysqli_connect_error());
exit();
}
$obj = new usersOnline($conn);