Jump to content

Display active users


robert_gsfame

Recommended Posts

I have my simple ajax based chat room, but i don't know how to display all active users on the right side of the chat window

 

Below is the code:

 

index.php

<?
session_start();

if(isset($_GET['logout'])){   
   
   //Simple exit message
   $fp = fopen("log.html", 'a');
   fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
   fclose($fp);
   
   session_destroy();
   header("Location: index.php"); //Redirect the user
}

function loginForm(){
   echo'
   <div id="loginform">
   <form action="index.php" method="post">
      <p>Please enter your name to continue:</p>
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" />
      <input type="submit" name="enter" id="enter" value="Enter" />
   </form>
   </div>
   ';
}

if(isset($_POST['enter'])){
   if($_POST['name'] != ""){
      $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
   }
   else{
      echo '<span class="error">Please type in a name</span>';
   }
}
?>
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>

<?php
if(!isset($_SESSION['name'])){
   loginForm();
}
else{
?>
<div id="wrapper">
   <div id="menu">
      <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
      <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
      <div style="clear:both"></div>
   </div>   
   <div id="chatbox"><?php
   if(file_exists("log.html") && filesize("log.html") > 0){
      $handle = fopen("log.html", "r");
      $contents = fread($handle, filesize("log.html"));
      fclose($handle);
      
      echo $contents;
   }
   ?></div>
   
   <form name="message" action="">
      <input name="usermsg" type="text" id="usermsg" size="63" />
      <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
   </form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
   //If user submits the form
   $("#submitmsg").click(function(){   
      var clientmsg = $("#usermsg").val();
      $.post("post.php", {text: clientmsg});            
      $("#usermsg").attr("value", "");
      return false;
   });
   
   
   function loadLog(){      
      var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
      $.ajax({
         url: "log.html",
         cache: false,
         success: function(html){      
            $("#chatbox").html(html);

            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            if(newscrollHeight > oldscrollHeight){
               $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }            
           },
      });
   }
   setInterval (loadLog, 2500);   //Reload file every 2.5 seconds
   
   //If user wants to end session
   $("#exit").click(function(){
      var exit = confirm("Are you sure you want to exit?");
      if(exit==true){window.location = 'index.php?logout=true';}      
   });
});
</script>
<?php
}
?>
</body>
-----------------------------------------------------------------------

post.php

<?
session_start();
if(isset($_SESSION['name'])){
   $text = $_POST['text'];
   
   $fp = fopen("log.html", 'a');
   fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
   fclose($fp);
}
?>

--------------------------------

log.html to put all the text

 

EDIT: use code tags next time

Link to comment
Share on other sites

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.