php - User Online Remains 1 -
everything working , communicating database regardless of how many users online stays 1
i have helper functions created num rows , query
online users
function users_online(){ $session = session_id(); $time = time(); $time_out_in_seconds = 60; $time_out = $time = $time_out_in_seconds; $sql = "select * users_online session = '$session'"; $result = query($sql); $count = row_count($result); if ($count == null) { $sql= "insert users_online(session, time) values ('$session','$time')"; query($sql); } else { $sql= "update users_online set time = '$time' session = '$session'"; query($sql); } $sql = "select * users_online time > '$time_out'"; return $count_user = row_count($result); }
db config helper functions
$connection = mysqli_connect('localhost', 'root', '', 'danger_db'); function row_count($result) { return mysqli_num_rows($result); } function escape($string) { global $connection; return mysqli_real_escape_string($connection, $string); } function query($query) { global $connection; return mysqli_query($connection, $query); } function confim($result) { global $connection; if (! $result) { die("query failed" . mysqli_error($connection)); } } function fetch_array($result) { global $connection; return mysqli_fetch_array($result); }
database table
create table if not exists `users_online` ( `id` int(11) not null, `session` varchar(255) not null, `time` int(11) not null ) engine=myisam default charset=utf8 auto_increment=31 ; -- -- dumping data table `users_online` -- insert `users_online` (`id`, `session`, `time`) values (30, 'n3kppn4lp0n9h4gdm7a6rh84b2', 1452969896), (29, 'mqhrkr2b3f2rga1vhqh4t6pe23', 1452969689), (28, 'dpg2nmsrvsvottj9k7ot1gvcm6', 1452969893);
$time = time(); $time_out_in_seconds = 60; $time_out = $time = $time_out_in_seconds;
what trying here? setting time_out
and $time
equal $time_out_in_seconds
. you're setting 3 values 60
static value.
then later in code saving value 60
database , calling time > '$time_out'
sql you're setting user online being 60 , asking database return rows greater than 60.
solution
replace above code with:
$time = time(); $time_out_in_seconds = 60; $time_out = $time - $time_out_in_seconds; //edit further down page show users //updated within last 60 seconds.
this mean sql query check time of session against current time updating sessions with current time well. should give better results.
you have poor coding @ end of code here:
$sql = "select * users_online time > '$time_out'"; return $count_user = row_count($result);
the return takes value , passes calling script, value value want give number of users online, value of return? made of row_count($result)
. $result
? last $sql
row 1 need results from, row has no relation result variable.
you have missed line , need reinsert line $result = query($sql);
so:
$sql = "select * users_online time > '$time_out'"; $result = query($sql); return row_count($result); }
this give numeric value number of rows returned of online users, result of function users_online();
Comments
Post a Comment