DootThaLoop Posted May 17, 2008 Share Posted May 17, 2008 Hello, I'm kind of new at PHP. And I'm asking your help once again. I've put together [heavily edited] this users online script I found. What it does is display what logged in users are online on my site. But I need to also know what page they are on. I think I would have to somehow make use of: $_SERVER['REQUEST_URI'] But I'm at a completely stuck point. I have no idea what I would have to do to display the page that the members are on. For your reference, here is my main script: <?php session_start(); #Start the session $hostname = "localhost"; #MySQL Hostname $username = "helloism_hellois"; #MySQL Username $password = "********"; #MySQL Password $database = "helloism_login"; #MySQL Database $connect = mysql_connect(localhost, helloism_hellois, *******); #Connect to the mysql host $select_db = mysql_select_db(helloism_login, $connect); #Select the database if (isset($_SESSION['PHPSESSID'])) { #If the user is logged in, good for them, if not, they become an ip address $username = $_SESSION['username']; #Username } else { $username = $_SERVER['REMOTE_ADDR']; #Username is IP Address } $time = time(); #Current time $previous = "1"; #Time to check in seconds $timeout = $time-$previous; #Timeout $query = "SELECT * FROM active_users WHERE username=\"$username\" AND timestamp < \"$timeout\""; #Past 2 minutes $verify = mysql_query($query); #Execute query $row_verify = "mysql_fetch_assoc($verify)"; #Check if you have been here in two minutes if (!isset($row_verify['username'])) #See if you were found $query = "INSERT INTO online (username, timestamp) VALUES (\"$username\", \"$time\")"; #Put you on the online list $insert = mysql_query($query); #Execute query $query = "SELECT * FROM active_users WHERE timestamp < \"$timeout\""; #Check and see who is online ?> <font size="3" face="Courier New" color="FFFFFF"> <? $online = mysql_query($query); #Execute query $row_online = "mysql_fetch_assoc($username)"; #Grab the users if (isset($row_online['username'])) { do { echo ($row_online['username'].""); #Output username echo '<br>'; #put a break after each database entry while($row_online = mysql_fetch_assoc($online)); #Until all records are displayed } else { echo "No one's online."; #Inform user that no one is online } ?> <title>[:Users Online:]</title> </head> <body bgcolor="000000" text="FFFFFF"> </body> </html> The script works fine so far. I just need to know about what I asked. If someone could either tell me what I have to do, or point me in the right direction, it would be much appreciated. Thanks for looking! Quote Link to comment Share on other sites More sharing options...
947740 Posted May 17, 2008 Share Posted May 17, 2008 On every page, you could have something like the phpself and insert that into the database, along with the other info. I do not know the exact syntax for phpself, however. Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 You mean this code? $_SERVER['PHP_SELF'] Not the one that I mentioned before? Quote Link to comment Share on other sites More sharing options...
947740 Posted May 17, 2008 Share Posted May 17, 2008 Yeah. I always have trouble remembering that for some reason... Does that help at all? Quote Link to comment Share on other sites More sharing options...
mlin Posted May 17, 2008 Share Posted May 17, 2008 $_SERVER['PHP_SELF'] contains the currently requested file, but you were correct about $_SERVER['REQUEST_URI'] which will contain the file And any get variables in the request. Use the latter. parse_url will help you out, it'll break down the sections of your uri into an easy to use array. I'd recommend logging each user who is logged in to an 'onlinenow' table that holds the username, file, variables, and timestamp. Setup some sort of garbage collection to delete rows where the timestamp is expired. Then to display who is online for the entire site, query just the usernames. And to query who is online for a particular page, query the file and variables as well. Hope that helps! Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 Okay so what I did was, I put all the above script into a file called 'userinfo.php', and plan to include it in each page that I want the user's page url from. So.... after inserting the last line, I came up with this error: Parse error: syntax error, unexpected T_VARIABLE in /home/helloism/public_html/ynw/login/userinfo.php on line 40 And here is the offending line: $query = "INSERT INTO online (page) VALUES ("$_SERVER['REQUEST_URI']")"; Now..... I did make a field on the table 'online' named 'page' so what could be wrong? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $query = "INSERT INTO online (page) VALUES ('{$_SERVER['REQUEST_URI']}')"; 1) You can't use double quotes within double quotes without escaping them. 2) You need to encapsulate any array variables in { } when using them in a string. Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 Okay so I've been experimenting with it. I have several files; the pages themselves of course, the users.php which contains the entire script I mentioned in my first post, and then the file userinfo.php, which contains most of the info from the users script, as well as the line: $query = "INSERT INTO online (page) VALUES ('{$_SERVER['REQUEST_URI']}')"; Which I'm assuming will insert the users page information into a row I named 'page' in the 'online' table. Also I did an include on one of my pages for a test, that includes the userinfo.php. So.... am I doing everything right so far? If not, tips? What else would I have to do? Thanks! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 I just thought about it, and you're doing it wrong. You need to have a column for each user in the online table (make it when they register) and UPDATE the page column on each page they access, not insert a new row. Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 So will this affect already registered users, or no? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Hmm? What do you mean? Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 Well you said make it when they register, so does that mean it possibly won't affect already registered users? Or maybe I'm confusing myself... Quote Link to comment Share on other sites More sharing options...
mlin Posted May 17, 2008 Share Posted May 17, 2008 when the user logs in to your site, you'll use the Insert statement to add a row to your online table. Every request thereafter should update that row's timestamp and page. You shouldn't have more than one row for each user. don't forget the garbage collection Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted May 17, 2008 Share Posted May 17, 2008 I got this script a while back that grabs what page the user is on, plus the values on the URL. You can then insert this into the database etc... <?php /* Grab current pages complete URL including after the question mark */ $QueryString=""; foreach ($_GET as $key => $value) { $value = urlencode(stripslashes($value)); if($QueryString!="") $QueryString .="&"; $QueryString .= "$key=$value"; } $pageName=basename($_SERVER['PHP_SELF'] ); $page =$pageName."?".$QueryString; // echo $page; // will show the full page // echo $QueryString; // will show after ? // end of URL grab ?> Regards ACE Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 Alright, I used the code in the previous mentioned post. Now.... to get it to working properly, I have to insert something? Would this go on my main login page, or on every page? And what would I have to insert? Once again, I'm kind of a newbie at PHP, so I'm taking a wild guess here, and I'm likely wrong, but would it be something like this: $verify = mysql_query($query); $query = "INSERT active_users (username) INTO online (nowonline) VALUES (getpage.php)"; Thanks once again... Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted May 17, 2008 Share Posted May 17, 2008 other way around $query = "INSERT active_users (username) INTO online (nowonline) VALUES (getpage.php)"; $verify = mysql_query($query); ACE Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 17, 2008 Author Share Posted May 17, 2008 Hmm..... it still doesn't display what page the users are on though. It DOES display one page name, but thats the name of the user.php userlist file :/ ... Maybe I put in the wrong values for that query? Quote Link to comment Share on other sites More sharing options...
DootThaLoop Posted May 18, 2008 Author Share Posted May 18, 2008 I'm really not sure about this one... I tried this: $query = "INSERT INTO active_users (nowonline) VALUES ($page)"; $query = "SELECT nowonline FROM active_users"; $verify = mysql_query($query); But it still didn't work... What exactly am I suppose to put into the INSERT in order to get it to display what pages members are on? Also, that nowonline thing I made kinda screws up the table and I have to remake the table everytime I tamper with the script trying to get it to work... its a mess :/ Quote Link to comment 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.