Jump to content

Recommended Posts

Hi

 

I need bit of help if ok with a php mysqli stats coding I have put in my includes/header.php file as want it on every page but for some reason it is displaying 0 users online, the data is being added to the database but not being retrieved for some reason as thought it would say 1 user online as I am on the site and refreshing it, below is the coding I have on usersonline.php file

<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = ""; //your MySQL database username
$db_pass = ""; //your MySQL database password
$database = "";
$timeoutseconds = 300;

//this is where PHP gets the time
$timestamp = time();
$timeout = $timestamp - $timeoutseconds;

//connect to database
//$server = localhost probably
//$db_user = your MySQL database username
//$db_pass = //your MySQL database password
$mysqli = new mysqli($server, $db_user, $db_pass, $database);

//insert the values
$insert = "INSERT INTO useronline VALUES (?, ?, ?)";
$stmt = $mysqli->prepare( $insert );
$stmt->bind_param( 'iss', $timestamp, $_SERVER['REMOTE_ADDR'],$_SERVER['PHP_SELF'] );

if(!$stmt->execute()) {
    print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = "DELETE FROM useronline WHERE timestamp < ?";
$stmt = $mysqli->prepare( $delete );
$stmt->bind_param( 'i', $timeout );

if(!$stmt->execute()) {
    print "Useronline Delete Failed > ";
}

//grab the results
$result = "SELECT DISTINCT ip FROM useronline WHERE file = ?";
$stmt = $mysqli->prepare( $result );
$stmt->bind_param( 's', $_SERVER['PHP_SELF'] );


if(!$stmt->execute()) {
    print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = $stmt->num_rows;


//spit out the results
$mysqli->close();


if($user == 1) {
    print("$user user online\n");
} else {
    print("$user users online\n");
}
?>

then in my header.php file I have the following

    <?php
require_once("usersonline.php"); 
?>

Sorry I have tried to work the issue out but I am not getting any errors so bit stuck on what to fix?

Link to comment
https://forums.phpfreaks.com/topic/299703-php-mysqli-stats-issue/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.