I need to know how I can setup a php script that would display "There are currently ## users on this site" on my main page of my site and it would be accurate. I was told you need to make it so the user registers but I need to know if thier is an easyer way and if so how and if not how do I do the user registration thing? Any help would be greatly appreciated! Thanks.
You could do it with a Java applet and server side script, with JavaScript and Server-side script (although it wouldn't be very accurate), by time using PHP (ask me to explain later, I'm tired, it's bout 10:30 and I've had quite a lot to drink), or by logins. Tomorrow I will make you a script combining JavaScript and timing to do it roughly. I look forward to working with you again.
Here's a script tht will calculate the number of users on a page at any one time, roughly. (I say roughly because people without JavaScript will never be detected as leaving). Without using login + register I think it's the best you can do. This is the bit where Pyro proves me wrong...
visitors.php:
Code:
<?php
$fh=fopen("online.txt","r+");
$read=fgets($fh,12);
setType($read,"integer");
if($_GET['minus']) $read--; else $read++;
rewind($fh);
fwrite($fh,$read);
echo("There are currently $read users online");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Visitor counter</title>
<?php if(!$minus) echo(' <script type="text/javascript">
<!--
function leave() {
notify=window.open("visitors.php?minus=yes");
window.close(notify);
}
// -->
</script>');?>
</head>
<body <?php if(!minus)echo('onunload="leave()"'); else echo('onload="window.close()');?> >
</body>
</html>
online.txt:
Code:
0
Just add your content to the framework provided by visitors.php. Need any more help, just post.
Thanks for your response. I was wundering if you could possibly post the files or the link to an example so I could see it in action and see if I like it? I would do it but I usally screw it up pretty bad and I want a working example. You could post both of you ideas the "Non Registration + Login" and the "Login + Registration" one so I could see which one I will use. Thank you so much for your help. I may not respond right away as I'll probably be gone all day so I'll try to view your response as soon as possible. Thanks again for your help.
lol I am inturding on the sacred ground of the php forum and I myself do not use php. Anyways to do a login/register you would need login forms and reistration forms and what makes you thing people are going to login if there is nothing for them you see where it says people browsing this forum thats login and registration but because we need to login to post it forces us to login which makes that method accurate but notice it only tells you who is loggedin not who is browsing and it does this by adding your name to a data base then removing it onunload or whatever the php equivalent to that is. It is the same in all server side llanguages the principle atleast not the code.
Well I was kinda planning to have a login/registation thing any way since I plan to have a chat room and forum and stuff on my site that I will only want registered users to have access to! I think that all I will worrie about is the amount of users logged in so people will know when someones on the forum or chat room or what not! I also want to be able for them to be greeted with their username when they log in on the main page and if they are not logged in it will say guest!
well the greet username is not as hard as the currently logged in junk all you have to is grab their session username or whatever the feild in your data base is for user name.
The hard part is remembering how to use loads of functions- there are literally hundreds. Guess what Pyro can do that I can't! Hopefully that will come with experience. Pyro, how long have you been using PHP?
Well I ddint find anything in the 5 minutes I searched, but ther are hints that it is possible. You would have to access the global $_SESSION of course. If nothing else, set up a database table and log users from your main page by either there ip address or their GUID and check it on each page they go to also log their sessid and then on sessid_timeout have it remove it
sorry im in a hurry to get home and play counterstrike lol just got off work
Thank you guys. You all had great answers for my question. Pyro, thanks for your response! I think your idea will work for what I'm trying to accomplish but being such a newb to php that you know I am I need a little more explination on how to impliment this idea if you have time. From what I gather I need to make a php file with the php code you gave me and make a log.txt or something but I need more explination on what goes in the txt file and where I put the php file and the txt file and how I would set up my main html file to display "Currently Active Users". I would greatly appreciate any more help you can provide. Thanks so much for your help and hope to see a response from you soon! Also thanks again to all that responded.
Bookmarks