Current location: Hot Scripts Forums » Programming Languages » PHP » Session: User Online?


Session: User Online?

Reply
  #1 (permalink)  
Old 02-15-04, 09:44 PM
AcidicChip AcidicChip is offline
New Member
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Session: User Online?

I am developing a community site, and of course this site uses sessions... Is there a way (using sessions) to know if a user is online? This community also has a mysql database...

Maybe if when the user logs in, it'll update the DB with that user's session ID, and there is a way to see if that user's session is active?

Please let me know, thanks in advance.

Last edited by AcidicChip; 02-15-04 at 10:05 PM.
Reply With Quote
  #2 (permalink)  
Old 02-16-04, 02:57 PM
phpray phpray is offline
New Member
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
You could try registering a session variable for online like
'online' then test if this variable isset.

You could also write a block to check if there is an active
session for that particular user and this would tell you if
they are online or not.

Few different ways you could do this.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 02-18-04, 12:33 AM
NerdRUS NerdRUS is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
here

i got this from some where and modified it so it would work the way you want it to.... '$valid_user' is the registered variable carried over the pages. Online is the table.
PHP Code:

<?

$conn
=mysql_connect("localhost","username","password") or die ("can't connect to server");
@
mysql_select_db("database_name") or die ("can't select database");
?>

<! -- UPDATING ONLINE USER DATABASE (add these line below on top of your ebpage) -->
<?
$query 
"select * from userdb where email = '$valid_user'";
$display mysql_query($query);
$row mysql_fetch_array($display);

$uvisitor$row[username];
$utime=time();
$exptime=$utime-600// (in seconds)

@mysql_query("delete from online where timevisit<$exptime");
$sumthinruther = @mysql_query("select id from online where visitor='$uvisitor'");
$uexists=@mysql_num_rows($sumthinruther);

if (
$uexists "0"){
   @
mysql_query("update online set timevisit='$utime' where visitor='$uvisitor'");
} else {
   @
mysql_query("insert into online (visitor,timevisit,email) values ('$uvisitor','$utime','$valid_user')");
}

?>

<!-- DISPLAYING ONLINE USER DATABASE -->
<style><!--
.kecil {font-family:ms sans serif; font-size:8pt}
--></style>
<?
$rs
=@mysql_query("select * from online order by timevisit desc");

echo 
"<p><div align=center class=kecil>";
$jmlonline=@mysql_num_rows(@mysql_query("select id from online"));
echo 
"<table><tr bgcolor=#000000><td class=kecil><font color='#CCCCCC'><b>Username<td class=kecil><b>Last visit</font></tr>";
while (
$ro=@mysql_fetch_array($rs)){

   echo 
"<tr><td class=kecil>".$ro[visitor]."<td class=kecil>".date('j/n/y H:i',$ro[timevisit])."</tr>";
}
echo 
"</table></div>";
?>
__________________
WEB: Prison City *Under Development* Not released to public yet.
Reply With Quote
  #4 (permalink)  
Old 02-18-04, 03:18 AM
0zone 0zone is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, here is what most sites do:

1. Make a table called online, with the following rows: session, ip, time.

2. When a user views your site check to see if they have a row in the database with a query:
Code:
mysql_query('SELECT session FROM online WHERE session = \'' . session_id() . '\' LIMIT 1');
3. If there was a session row update it with a query:
Code:
mysql_query('UPDATE online SET time = \'' . time() . '\' WHERE session = \'' . session_id() . '\' LIMIT 1');
4. If there was no session row insert one:
Code:
mysql_query('INSERT INTO online SET session = \'' . session_id() . '\', ip = \'' . $_SERVER['REMOTE_ADDR'] . '\', time = \'' . time() . '\'');
5. Lastly you don't want to have people shown as online forever so lets delete users who have not been active for more than 10 minutes (600 seconds):
Code:
mysql_query('DELETE FROM online WHERE time = \'' . (time() - 600) . '\' LIMIT 1');
There you have it!
Reply With Quote
  #5 (permalink)  
Old 06-01-08, 10:14 AM
buruk buruk is offline
New Member
 
Join Date: Jun 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
X_X Some problem ( thanks for now(F) )

problem is
Warning: gethostbyaddr() [function.gethostbyaddr]: Address is not in a.b.c.d form in C:\Inetpub\vhosts\lallal.net\httpdocs\iyi\inc\sol. php on line 212

line 212
Code:
$exptime=$utime-600; // (in seconds)
sol.php code
Code:
<script language=JavaScript>
<!--
function Win() {
 width = screen.width;
 height = screen.height;
<?
$toplammesaj = @mysql_num_rows(@mysql_query("select * from mesajlar where uyeid = $msql_id and durum = 2"));
if(($toplammesaj != 0) and ($bakildimi != "ok")){
echo "window.open('hesabim/popup.php' , 'domain' , 'width='+440+',height='+129+',fullscreen=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0');";
}?>
}
Win();
//-->

</script><style type="text/css">
<!--
.style1 {
	color: #FF0000;
	font-weight: bold;
}
-->
</style>
<TABLE cellSpacing=0 cellPadding=0 width="250" border=0>
              <TBODY>
              <TR>
                <TD style="FONT-SIZE: 12px; COLOR: #3d6074" vAlign=top
                width="100%" colSpan=2><img src="images/giris.gif" width="119" height="30"><?
				$tablo = "select id from users where login = '$msql_sifre' and password='$msql_sifre2'";
				$sorgu = mysql_query($tablo);
				if (mysql_num_rows($sorgu) < 1 ){
																?><form action="login.php" method="post" name="" id=""><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="34%"><div align="right">Kullanıcı Adı&nbsp;&nbsp;</div></td>
                        <td width="66%"><INPUT name=kullaniciadi class=input id="kullaniciadi" style="WIDTH: 90px"
                  maxLength=20></td>
                      </tr>
                      <tr>
                        <td><div align="right">Şifre&nbsp;&nbsp;</div></td>
                        <td><INPUT name=sifre type="password" class=input id="sifre" style="WIDTH: 90px"
                  maxLength=20></td>
                      </tr>
                      <tr>
                        <td><input name="olay" type="hidden" id="olay" value="login"></td>
                        <td><INPUT name="submit" type=submit style="FONT-SIZE: 11px" value="   Giriş   ">
                            <br>
                            <a href="yeniuye.php" class="konulink">Yeni üye</a><br>
                            <a href="javascript:openWin('sifremiunuttum.php','','toolbar=0, location=0, directories=0, status=0, menuBar=0, scrollBars=0, resizable=0, width=350,height=150, top=0');" class="konulink">Şifremi Unuttum</a> </td>
                      </tr>
                    </table></form><?}else{?>
                    <br>                    <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
                      <tr>
                        <td>Hoşgeldin <span class="style1">
                        <?=$msql_sifre;?>
                        </span><br>
                        <a href="hesabim.php" class=konulink><strong>Hesabınız</strong></a><br>
						<a href="hesabim.php" class=konulink><strong><?echo @mysql_num_rows(@mysql_query("select id from mesajlar where uyeid = $msql_id and durum = 2"));?> yeni mesajınız var</strong></a>
                        <br>
                        <a href="cik.php" class=konulink><strong>&Ccedil;ıkış</strong></a></td>
                      </tr>
                    </table>                    <br>                    <?}?><img src="images/cizgi.gif" width="249" height="1"> <IMG src="images/hizliarama.gif"></TD>
              </TR>
              <FORM action="arama.php" method=post>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>Kullanıcı Adı&nbsp;&nbsp;</TD>
                <TD width="65%"><INPUT class=input style="WIDTH: 90px" maxLength=14 name="kullaniciadi" <?if($olay == "kullaniciadi"){echo "value=\"$kullaniciadi\"";}?>>				<INPUT name="Submit" type=submit style="FONT-SIZE: 11px" value=ara>
				<input name="olay" type="hidden" id="olay" value="arama">
              </TD>
              </TR></FORM>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="100%" colSpan=2
                height=5></TD></TR>
              <FORM name=hizli_arama action="arama.php" method=post>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>Cinsiyet&nbsp;&nbsp;</TD>
                <TD width="65%">
				<?
				if($olay == "normal"){
					if($cins == ""){$secenek1 = "selected";}
					if($cins == "1"){$secenek2 = "selected";}
					if($cins == "2"){$secenek3 = "selected";}
				}
				?>
				<SELECT name=cins class=input id="cins" style="FONT-SIZE: 10px; WIDTH: 55px">
                  <OPTION value="" <?=$secenek1;?>>Seç</OPTION>
				  <OPTION value="2" <?=$secenek2;?>>Kadın</OPTION>
				  <OPTION value="1" <?=$secenek3;?>>Erkek</OPTION></SELECT></TD></TR>
				  <tr>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>Yaş Aralığı&nbsp;&nbsp;</TD>
                <TD width="65%"><SELECT name=yas1 class=input id="yas1"
                  style="FONT-SIZE: 10px; WIDTH: 55px">
                  <OPTION value=""  <?if($yas1 == ""){echo "selected";}?>>Seç</OPTION>
				  <?
				  $query = "SELECT * FROM yas order by id";
				$result = mysql_query ($query) or die ("hata");
				while ($line = mysql_fetch_array($result)){
				?>
				 <OPTION value="<?=$line[yas];?>" <?if($yas1 == $line[id]){echo "selected";}?>><?=$line[yas];?></OPTION>
				 <?}?>
				  </SELECT> - <SELECT name=yas2 class=input id="yas2" style="FONT-SIZE: 10px; WIDTH: 55px">
                <OPTION value=""   <?if($yas2 == ""){echo "selected";}?>>Seç</OPTION>
				<?
				$query = "SELECT * FROM yas order by id";
				$result = mysql_query ($query) or die ("hata");
				while ($line = mysql_fetch_array($result)){
				?>
				 <OPTION value="<?=$line[yas];?>" <?if($yas2 == $line[id]){echo "selected";}?>><?=$line[yas];?></OPTION>
				 <?}?></SELECT> </TD></TR>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>Yaşadığı İl&nbsp;&nbsp;</TD>
                <TD width="65%">
				<SELECT name=sehir class=input id="sehir" style="FONT-SIZE: 10px; WIDTH: 120px">
				<OPTION value=""   <?if($il == ""){echo "selected";}?>>Seç</OPTION>
				  <?
				  $query = "SELECT * FROM sehir order by id";
			      $result = mysql_query ($query) or die ("hata");
				  while ($line = mysql_fetch_array($result)){
				  ?>
				<OPTION value="<?=$line[id];?>" <?if($il == $line[id]){echo "selected";}?>><?=$line[sehir];?></OPTION>
				 <?}?>
				 <OPTION value="x"   <?if($il == "x"){echo "selected";}?>>--Yurt Disi--</OPTION>
				</SELECT></TD></TR>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>&nbsp;&nbsp;</TD>
                <TD width="65%"><INPUT type=checkbox value=1 name=foto id="foto" <?if($foto == "1"){echo "checked";}?>>
                  <label for="foto">Sadece Fotoğraflılar</label><br>
                  <INPUT type=checkbox value=1 name=online id="online" <?if($online == "1"){echo "checked";}?>>
                  <label for="online">Sadece Online&nbsp;Olanlar</label></TD>
              </TR>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="100%" colSpan=2
                height=5></TD></TR>
              <TR>
                <TD style="COLOR: #3d6074" align=right width="35%"
                  height=1>&nbsp;&nbsp;</TD>
                <TD width="65%" height="25"><INPUT style="FONT-SIZE: 11px" type=submit value="Arama">
                  &nbsp;&nbsp;<A class=konulink
                  href="arama.php">detaylı
          ara</A>
                <input name="olay" type="hidden" id="olay" value="arama"></TD>
              </TR></FORM></TABLE><img src="images/cizgi.gif" width="249" height="1">
          <img src="images/yenibayanlar.gif" width="119" height="30">
          <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="62%"><strong>Rumuz</strong></td>
              <td width="9%"><strong>Yaş</strong></td>
              <td width="29%"><div align="right"><strong>Nerden</strong></div></td>
            </tr>
			<?
			$query = "SELECT id, no, login, sehir, ulke, dogum FROM users where cins = 2 order by id desc limit $anasayfa";
			$result = mysql_query ($query) or die ("hata");
			while ($line = mysql_fetch_array($result)){
			?>
            <tr>
              <td> <a href="profil.php?no=<?=$line[no];?>" class="konulink"><strong><?=$line[login];?></strong></a> </td>
              <td><?echo date("Y-m-d")-$line[dogum];?></td>
              <td><div align="right">
			  <?
			  if($line[sehir] != "x"){
			  if($line[sehir] != ""){$query2 = "SELECT * FROM sehir where id = $line[sehir]";$result2 = mysql_query ($query2) or die ("hata");while ($line2 = mysql_fetch_array($result2)){echo $line2[sehir];}}
			  }else{
			  if($line[ulke] != ""){$query2 = "SELECT * FROM ulkeler where id = $line[ulke]";$result2 = mysql_query ($query2) or die ("hata");while ($line2 = mysql_fetch_array($result2)){echo $line2[ulke];}}
			  }
			  ?></div></td>
            </tr>
           <?}?>
          </table>
          <img src="images/cizgi.gif" width="249" height="1"> <img src="images/yenierkekler.gif" width="119" height="30">
          <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="62%"><strong>Rumuz</strong></td>
              <td width="9%"><strong>Yaş</strong></td>
              <td width="29%"><div align="right"><strong>Nerden</strong></div></td>
            </tr>
            <?
			$query = "SELECT id, no, login, sehir, ulke, dogum FROM users where cins = 1 order by id desc limit $anasayfa";
			$result = mysql_query ($query) or die ("hata");
			while ($line = mysql_fetch_array($result)){
			?>
            <tr>
              <td> <a href="profil.php?no=<?=$line[no];?>" class="konulink"><strong>
                <?=$line[login];?>
              </strong></a> </td>
              <td><?echo date("Y-m-d")-$line[dogum];?></td>
              <td><div align="right">
                  <?
			  if($line[sehir] != "x"){
			  if($line[sehir] != ""){$query2 = "SELECT * FROM sehir where id = $line[sehir]";$result2 = mysql_query ($query2) or die ("hata");while ($line2 = mysql_fetch_array($result2)){echo $line2[sehir];}}
			  }else{
			  if($line[ulke] != ""){$query2 = "SELECT * FROM ulkeler where id = $line[ulke]";$result2 = mysql_query ($query2) or die ("hata");while ($line2 = mysql_fetch_array($result2)){echo $line2[ulke];}}
			  }
			  ?>
              </div></td>
            </tr>
            <?}?>
          </table>
          <img src="images/cizgi.gif" width="249" height="1">          <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="62%"><strong>Sitede şu an </strong></td>
            </tr>

            <tr>
              <td>Toplam
              <?
$uvisitor=$REMOTE_ADDR;
$uvisitor.="|".gethostbyaddr($uvisitor);
$utime=time();
mysql_query('DELETE FROM online WHERE time = \'' . (time() - 600) . '\' LIMIT 1');

@mysql_query("delete from online where zaman<$exptime");
$uexists=@mysql_num_rows(@mysql_query("select id from online where ziyaretci='$uvisitor'"));

if ($uexists>0){
@mysql_query("update online set zaman='$utime' where ziyaretci='$uvisitor'");
} else {
@mysql_query("insert into online (ziyaretci,zaman) values ('$uvisitor','$utime')");
}

$rs=@mysql_query("select * from online");
while ($ro=@mysql_fetch_array($rs)){
}

$jmlonline=@mysql_num_rows(@mysql_query("select id from online"));
echo "$jmlonline";
?>
               kullanıcı online. <br> <?$kayitlilar = @mysql_num_rows(@mysql_query("select kullanici from online2"));
              echo $kayitlilar;?> kayıtlı, <?echo ($jmlonline - $kayitlilar);?> misafir</td>
            </tr>
   
          </table>
Reply With Quote
  #6 (permalink)  
Old 06-02-08, 07:17 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I imagine this bit of code
PHP Code:

$uvisitor=$REMOTE_ADDR;
$uvisitor.="|".gethostbyaddr($uvisitor); 
Should be:
PHP Code:

$uvisitor=$_SERVER['REMOTE_ADDR'];
$uvisitor.="|".gethostbyaddr($uvisitor); 
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
session help darkcarnival PHP 5 05-09-05 12:40 AM
vbscript for modifying values in attributes in Session tab of a user in AD W2K-serv Kjell Script Requests 0 01-28-04 08:04 AM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM
need help finding a good user online tutorial darkcarnival PHP 4 12-07-03 10:03 AM
[ Wanted ] Who's online Script redspider ASP 5 06-13-03 07:44 PM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.