redarrow Posted December 27, 2008 Share Posted December 27, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/ Share on other sites More sharing options...
revraz Posted December 27, 2008 Share Posted December 27, 2008 Does it work? Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724529 Share on other sites More sharing options...
redarrow Posted December 27, 2008 Author Share Posted December 27, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724531 Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724538 Share on other sites More sharing options...
redarrow Posted December 27, 2008 Author Share Posted December 27, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724541 Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 It will just finish the updating when the next person hits the page/script. This is the downside of not using CRON. Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724543 Share on other sites More sharing options...
redarrow Posted December 27, 2008 Author Share Posted December 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724546 Share on other sites More sharing options...
redarrow Posted December 27, 2008 Author Share Posted December 27, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724557 Share on other sites More sharing options...
redarrow Posted December 27, 2008 Author Share Posted December 27, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138568-solved-updatting-logged-in-users/#findComment-724612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.