Jump to content

Getting the page members are currently on?


DootThaLoop

Recommended Posts

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!

$_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!

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?

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!

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

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...

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 :/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.