NB: I'm a beginner at php programming.

Hi, I've been trying to add an 'Online' or 'Offline' plugin to my login system. To display the username you use thr following:

$session->username

In the database, I have a table for active users. When a user logs in, he is added to that table and when a user logs out then he is removed from that table. The table name is 'blog_active_user'.

I've tried something like:

<?php
$query = "SELECT username FROM blog_active_users"

if($query == $session->username){
echo '<p>User is online</p>';
}
else '<p>User if offline</p>';
} 
?>

But then I get an error: Parse error: syntax error, unexpected 'if' (T_IF) in /home/********/public_html/main.php on line 47.

Please could someone help.

to do an online offline system u nees to set a column for the last_active to store when the user was lastly active (this is used in case the user close the browser without signing out)
and u need to set another column is_online to check if the user is online when logged in or offline when signed out
In this case, the condition to check if the user is online is:
check if the user is online and the last_active is less than 10min

@cmps I'm going to add another plugin that logs out a user if he is inactive later but for now, I'm focusing on the other things.
@pritaes I added a semi-colon after

$query = "SELECT username FROM blog_active_users"; //<--- Here

But now I get another error: Parse error: syntax error, unexpected '}' in /home/********/public_html/main.php on line 72

This is the whole page:

<?php
/**
 * Main.php
 *
 * This is an example of the main page of a website. Here
 * users will be able to login. However, like on most sites
 * the login form doesn't just have to be on the main page,
 * but re-appear on subsequent pages, depending on whether
 * the user has logged in or not.
 *
 * 
 */
include("include/session.php");
?>

<html>
<title>Login Script - ProMan</title>

<body>

<table>
<tr>
  <td>


<?php
/**
 * User has already logged in, so display relavent links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
   echo '<h3></h3>';
   echo '<h1><img src="images/lock_unlocked.png" width="32" height="32">Logged In</h1>';

   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] &nbsp;&nbsp;"
       ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";



   echo "[<a href=\"forum/index.php\">Forum</a>]<br>";



$query = "SELECT username FROM blog_active_users";

if($query == $session->username){
echo '<p>User is online</p>';
}
else '<p>User if offline</p>';
} 


if(MAIL){
      echo "[<a href=\"mail.php\">You have $numUnreadMail unread mail</a>]<br>";
   }





if($numFriendRequests == 0){
      echo "[<a href=\"friends.php?user=$session->username\">$numFriends Friends</a>] &nbsp;&nbsp;";
   }else{
      echo "[<a href=\"friends.php?user=$session->username\">$numFriendRequests Friends Requests</a>] &nbsp;&nbsp;";
   } 

   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>] &nbsp;&nbsp;";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>


<h1><img src="images/lock_locked.png" width="32" height="32" alt="Login">Login</h1>
<?php

if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<?php echo $form->value("user"); ?>"></td><td><?php echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<?php echo $form->value("pass"); ?>"></td><td><?php echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <?php if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

<?php
}

?>

</td></tr>
</table>


</body>
</html>

You are also missing an opening brace after else
} else {

Thanks @JorgeM I don't know how I missed that. But now I have fixed all the errors and it doesn't echo anything.
___________________________________________________________________________________________________________________
EDIT: I removed one of the '=' from

if($query == $session->username){

And now it works. Thanks to all those who helped me.
___________________________________________________________________________________________________________________

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.