Jump to content

Recommended Posts

Advance thank you advice only.

 

 

Currently i am adding a log in system, and i want the database to update if the users is logged in,

via the database as added below with the term yes.

 

I have set the database to no as default and want the database to update when users are on the index page.

 

does it look correct.

 

DONT WONT TO RUN A CRON JOB.

 

 

<?php session_start();
/*
create table forum_logged_in_users(
logged_in_users_id int auto_increment primary key,
user_id int(10) not null,
logged_in enum('yes','no') default 'no' not null,
timestamp int(50) not null
)
*/

if($_SESSION['username']){

$time=time();

$sql="update forum_logged_in_users set logged_in='yes' , timestamp='$time_now' where user_id=' ".$_SESSION['user_id']."' ";
$res=mysql_query($sql)or die(mysql_error);


}elseif(!$_SESSION['username']){

// update the database with all users that are not online.

$sql="update forum_logged_in_users set logged_in='no' , timestamp='$time_now' ";
$res=mysql_query($sql)or die(mysql_error);

}

?>

Link to comment
https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/
Share on other sites

yes it works but is it ok.

 

<?php session_start();
/*
create table forum_logged_in_users(
logged_in_users_id int auto_increment primary key,
user_id int(10) not null,
logged_in enum('yes','no') default 'no' not null,
timestamp int(50) not null
)
*/

if($_SESSION['username']){

$time=time();

$sql="update forum_logged_in_users set logged_in='yes' , timestamp='$time_now' where user_id=' ".$_SESSION['user_id']."' ";
$res=mysql_query($sql)or die(mysql_error);


}elseif(!$_SESSION['username']){


$sql="update forum_logged_in_users set logged_in='no' , timestamp='$time_now' ";
$res2=mysql_query($sql)or die(mysql_error);

}

$sql2="select * from forum_logged_in_users";
$res3=mysql_query($sql2)or die(mysql_error());

while($row=mysql_fetch_assoc($res3)){

if($row['logged_in_users']=="yes"){

	$logged="Online: ".$_SESSION['username']." ";

}else{

$logged="offline: ".$_SESSION['username']." ";
}
}
echo $logged;
?>

That is fine. As long as you do it with each person logging in it should not be too bad. But for the first person to login in the morning it may take it a while to update the DB etc.

 

You may limit that query to 10 or 20, so it does not take a huge toll on 1 particular user and it spreads it among many.

so limit in the update query is allowed then.

 

but if there 50 users online what happens then. very interesting.

 


   $sql="update forum_logged_in_users set logged_in='yes' , timestamp='$time_now' where user_id=' ".$_SESSION['user_id']."' LIMIT 20";

 

 

Thank you.

 

I get what your saying now, Your thinking off the over load off the database well,

that ok it on a dedicated mysql server, So in my case i don't think i need to use the limit statement

i think,

 

what you think of that situation?

 

Also what you think the average amount off users would be online, that way can set the limit to it. << floored question use a loggin average count.

 

10 or 20 sounds grate.

 

Afther thinking hard, Yes ill set limit to 20 as advised very good i dear thank you.

 

if the forum grows then the limit can also grow cheers.

 

ill create a new table, with the amount off users logging in, get the average of login's and set the limit via the average.

What about this i dear, don't no if the 00 at the end make a difference to the limit clause.

 

get the average off users logging in.

 

and use it as a limit

 

<?php session_start();
/*
create table forum_logged_in_users(
logged_in_users_id int auto_increment primary key,
user_id int(10) not null,
logged_in enum('yes','no') default 'no' not null,
timestamp int(50) not null
)


create table forum_user_login(
forum_user_login_id int auto_increment primary key,
num int(10) not null
)

*/

if($_SESSION['username']){

$time=time();

$av="select count(*) as cnt, SUM(num) as sum, (SUM(num)/count(*)) as avg FROM forum_user_login";
$av_res=mysql_query($av)or die(mysql_error());

while($avs=mysql_fetch_assoc($av_res)){


$sql="update forum_logged_in_users set logged_in='yes' , timestamp='$time_now' where user_id=' ".$_SESSION['user_id']."' limit ".$avs['num']." ";
$res=mysql_query($sql)or die(mysql_error);

}

}elseif(!$_SESSION['username']){


$sql="update forum_logged_in_users set logged_in='no' , timestamp='$time_now' ";
$res2=mysql_query($sql)or die(mysql_error);

}

$sql2="select * from forum_logged_in_users";
$res3=mysql_query($sql2)or die(mysql_error());

while($row=mysql_fetch_assoc($res3)){

if($row['logged_in_users']=="yes"){

	$logged="Online: ".$_SESSION['username']." ";

}else{

$logged="offline: ".$_SESSION['username']." ";
}
}
echo $logged;
?>

 

half load on the database.

 

better then before

<?php session_start();
/*
create table forum_logged_in_users(
logged_in_users_id int auto_increment primary key,
user_id int(10) not null,
logged_in enum('yes','no') default 'no' not null,
timestamp int(50) not null
)


create table forum_user_login(
forum_user_login_id int auto_increment primary key,
num int(10) not null
)

*/

if($_SESSION['username']){
   
   $time=time();
   
  $av="select count(num) FROM forum_user_login";
  $av_res=mysql_query($av)or die(mysql_error());

  while($c=mysql_fetch_assoc($av_res)){

if( ($c['num']<20) ){


	$limit=10;

}elseif(($c['num']<40)){


$limit=20;

}elseif( ($c['num'] <60)){


$limit=30;

}elseif(($c['num'] <80)){


$limit=40;

}elseif(($c['num'] <100)){


$limit=50;

}
   
   $sql="update forum_logged_in_users set logged_in='yes' , timestamp='$time_now' where user_id=' ".$_SESSION['user_id']."' $limit ";
   $res=mysql_query($sql)or die(mysql_error);
   
   
  }

}elseif(!$_SESSION['username']){
   
   
   $sql="update forum_logged_in_users set logged_in='no' , timestamp='$time_now' ";
   $res2=mysql_query($sql)or die(mysql_error);

}

$sql2="select * from forum_logged_in_users";
$res3=mysql_query($sql2)or die(mysql_error());

while($row=mysql_fetch_assoc($res3)){
   
   if($row['logged_in_users']=="yes"){
      
      $logged="Online: ".$_SESSION['username']." ";
   
   }else{
   
   $logged="offline: ".$_SESSION['username']." ";
}
}

   echo $logged;
?>

 

 

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.