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?
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)){
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ı </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 </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>Çı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ı </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 </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ığı </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 </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> </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 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> </TD>
<TD width="65%" height="25"><INPUT style="FONT-SIZE: 11px" type=submit value="Arama">
<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>