Jump to content

Recommended Posts

I want to know how to show what users are online for my chat.

 

here is the code that controls if a user is logged in so they can chat.

 

<?php
session_start();
include_once"config.php";
if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){
         echo '<h4>You are not logged in. Please do so <a href="http://jays.netne.net/loginsystem/login.php">Login</a></h4>'; }


	else {
		echo "<h4>You are logged in.</h4>";
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta name="google-site-verification" content="V-MlKGWPoAzJ_-vfEsEZFg92geK_hsIr53hVfcy8ydg" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestChat Page</title>
    
    <link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="ol.css" type="text/css" />
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript" src="chat.js"></script>
    <script type="text/javascript">
    
        // ask user for name with popup prompt    
        var name = <?php echo json_encode((string)$_SESSION['username']); ?>; 
        
        // default name is 'Username'
    	if (!name || name === ' ') {
    	   name = <?php echo json_encode((string)$_SESSION['username']); ?>; 	
    	}
    	
    	// strip tags
    	name = name.replace(/(<([^>]+)>)/ig,"");
    	
    	// display name on page
    	$("#name-area").html("You are: <span>" + name + "</span>");
    	
    	// kick off chat
        var chat =  new Chat();
    	$(function() {
    	
    		 chat.getState(); 
    		 
    		 // watch textarea for key presses
             $("#sendie").keydown(function(event) {  
             
                 var key = event.which;  
           
                 //all keys including return.  
                 if (key >= 33) {
                   
                     var maxLength = $(this).attr("maxlength");  
                     var length = this.value.length;  
                     
                     // don't allow new content if length is maxed out
                     if (length >= maxLength) {  
                         event.preventDefault();  
                     }  
                  }  
    		 																																																});
    		 // watch textarea for release of key press
    		 $('#sendie').keyup(function(e) {	
    		 					 
    			  if (e.keyCode == 13) { 
    			  
                    var text = $(this).val();
    				var maxLength = $(this).attr("maxlength");  
                    var length = text.length; 
                     
                    // send 
                    if (length <= maxLength + 1) { 
                     
    			        chat.send(text, name);	
    			        $(this).val("");
    			        
                    } else {
                    
    					$(this).val(text.substring(0, maxLength));
    					
    				}	
    				
    				
    			  }
             });
            
    	});
    </script>

<meta name="Generator" content="Serif WebPlus X6 (14.0.0.020)">
<meta name="vieort" content="width=1000">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<style type="text/css">
   body{ color:black; padding:20px; font-size:14px; background-color:#454545; font-family:Arial, Helvetica, sans-serif;}
   h2 {font-weight:bold; color:#000099; margin:10px 0px; }
   h4 {color:white;}
   p span {color:#006600; font-weight:bold; }
   a, a:link, a:visited {color:#white;}
   textarea {width: 100%; padding: 10px; margin: 10px 0 15px 0; font-size: 13px; font-family: Consolas,monospace;}
   textarea.html {height: 300px;}
   p {margin: 0 0 10px 0;}
   code, pre {font-family: Consolas,monospace; color: green;}
   ol li {margin: 0 0 15px 0;}
</style>
</head>
<body onload="setInterval('chat.update()', 500)">

<br><br><br>
<?php if ($_SESSION){ ?>
    <div id="page-wrap">
           
        <p id="name-area"></p>
        
        <div id="chat-wrap"><h4></h4><div id="chat-area"></div></div>
        
        <form id="send-message-area">
            <p><font size="5">Message: </font></p>
            <textarea id="sendie" maxlength = '225' ></textarea>
        </form>
    
    </div>
<?php } ?>
<br><br>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/267585-showing-which-users-are-online/
Share on other sites

In your database track (1) the last time they did something (eg, browsed a page, sent a chat message) and (2) whether they have specifically logged in or logged out.

 

To see who is still logged in do a search based on those two fields. Decide how long it takes before a user is automatically "logged out"; for example, if a user is logged out after 15 minutes of inactivity then query for:

...WHERE last activity field 

-- if you named the fields "last_activity" and "is_logged_in" then maybe
...WHERE last_activity

 

Usual disclaimer: there are many ways to do this and the above is just one of them (though most of the others are very similar to it).

Iv found this site that shows me it http://www.oasitech.it/blogdotnet/readnews.aspx?idn=6 would i better of using that ? would it be easier ?

 

That is not written in PHP so no, it will not be easier.

 

I suggest you read requinix's reply again, he describes exactly the process you need to follow.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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