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.

Dani AI

Generated

The problem here is not the file append itself but how the script checks for existing names. In the original snippet the search variable is changed inside a function that never returns it (and thus the outer check always thinks the name is missing). Reading the file with space-based explode() while storing names as <BR> (and using filesize()/fread() on an empty file) makes matching fragile. Combine the ideas already suggested by (use in_array()), (add on login rather than every post) and (store status per user) for a robust solution.

A practical, minimal fix is: store one username per line (not HTML tags), sanitize the name, read the file into an array, check membership, and append with an exclusive lock. Example:

// run this when the user logs in (or when first detected)
if (isset($_SESSION['name'])) {
    $user = trim(strip_tags($_SESSION['name']));
    if ($user !== '') {
        $file = __DIR__ . '/online.txt';
        $lines = file_exists($file) ? file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : [];
        if (!in_array($user, $lines, true)) {
            file_put_contents($file, $user . PHP_EOL, FILE_APPEND | LOCK_EX);
        }
    }
}

Notes and hardening: always escape output with htmlspecialchars() when rendering. To avoid stale entries, store a JSON/DB mapping of username => last_activity and prune entries older than a timeout (heartbeat AJAX update). For concurrent writes use LOCK_EX or flock(). For production, prefer a small table (SQLite/MySQL) or JSON file with timestamps instead of a flat HTML file; that makes deduplication, status, and cleanup much simpler.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

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 Member #671080

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.