My online chat uses a php script. There`s the part I need to correct

if(isset($_SESSION['name']) && $_POST['text'] != ""){
	$search = "";
	$aname = $_SESSION['name'];
	function find_value($input) {
		$handle = fopen("online.html", "r");
		if ($handle) {
 		 while (!feof($handle)) {
  		  $entry_array = explode(" ",fgets($handle));
   		 if ($entry_array[0] == $input) {
    			  $search = $entry_array[0];
     		 }
   		 }
 		 fclose($handle);
  		}
RETURN NULL;
}
	find_value($aname);
	if($search == "") {
	$handle = fopen("online.html",'r');
	$contents = fread($handle,filesize("online.html"));
	fclose($handle);
	$handle = fopen("online.html",'w');
	fwrite($handle,$contents." <BR> ".$_SESSION['name']);
	fclose($handle);
	}

My problem is, so when somebody submits a message, his name is written into online.html as many times, as he writes something. But i need to write it only ones, because all that names in online.html will appear on my site as logged in users. Please help me to correct it. I need to all names appear in online.html only once.

Member Avatar for diafol

I don't really follow, but if you have an array of users, you can check to see if they're in it with in_array(). If not you can add an user with array_push().

Member Avatar for Zagga

Hi Tomashqooo,

Are you adding them to the online list every time they post something? If you add them to the list when they login, they will only get added once.

Like ardav said you could use array where username is key and his status be value like evstevemd=>idle
and use

if(!array_key_exists($username)){
    array[$key] = "active";
}

Nothing tested, but just to give an idea!

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.