954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Php chat issue

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.

Tomashqooo
Newbie Poster
10 posts since Jul 2011
Reputation Points: 7
Solved Threads: 1
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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.

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

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!

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: