简体   繁体   中英

Unknown error with user online script

I am trying to make this script to work in mysqli, script before nice worked in mysql, so I tried to 'remake it' but it return error and I don't know what to do ? :

Undefined variable: conn

What I'm doing wrong ??

<?
     $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;
                }
            }

        }

        ?>

try the following

<?

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;
        }
    }

}

?>

And When you create the Object of usersOnline pass the $conn variable as the argument similar to below:

Using the Code/Class

    $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;
            }
        }

    }

    ?>

Now your variable is within the class and can be used across the board by any other functions you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM