[HELP] fwrite and delete function

Ask about a PHP problem here.
Post Reply
TeeGee
Posts: 5
Joined: Tue Jan 31, 2012 5:32 am

[HELP] fwrite and delete function

Post by TeeGee »

hello there..
im planning to create a simple Online Users that is integrated to Facebook sdk. :)

So far, i have the ff codes:

index.php (this is where the online users listed)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Online Users</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ouser.js"></script>
</head>
<body>

<table id="online">

</table>

</body>
</html>
javascript file:
$(document).ready(
	function() {	
		setInterval (loadLog, 2500);		
	});

	
	function loadLog(){		
		$.ajax({
			url: "onlineusers.html",
			cache: false,
			success: function(html){	
				$("#online").html(html); 			
		  	},
		});
}
test.php (This is my temporary file to test the Login authentication for FB.. THIS IS WORKING)
<?php
  session_start();
  require_once("src/facebook.php");
  $config = array(
    'appId' => '285873338131549',
    'secret' => '56b9f71b9fd307a033aeeb725893fc89',
   );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>
<?php
    if($user_id) {
      try {
        $user_profile = $facebook->api('/me','GET');
		$profile = $user_profile['name'];
		$_SESSION['user'] = $profile; //PUT USER TO SESSION
		$_SESSION['user_id'] = $user_id; 

$line = file_get_contents("onlineusers.html");
$line = trim($line); // removes leading/trailing blank lines
$line = explode("\n", $line);
$line[] = '<tr><td><img src="https://graph.facebook.com/' . $_SESSION['user_id'] . '/picture"width="25"height="25"/></td><td>' . $_SESSION['user'] . '</td><td><a href="http://www.fb.com/' . $_SESSION['user_id'] . '" target="_blank">View Profile</a></td></tr>';
$line = array_unique($line);
$line = implode("\n", $line);
file_put_contents("onlineusers.html", $line);
echo 'OK';


} catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl(); 
echo '<a href="' . $login_url . '">LOGIN</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {
      $login_url = $facebook->getLoginUrl();
echo '<a href="' . $login_url . '">LOGIN</a>';
    }
echo "<a href='logout.php'>Logout</a>";
  ?>
What it does is, it simple insert the picture and name of the user in a flat html file..
http://www.barkadafm.org/onlineusers/index.php

Now, my problem is,.
I dont know how to delete the pic and name if a user logs out or disconnect..

Example, my name is Name5 and i will connect / login using http://www.barkadafm.org/onlineusers/test.php
for the first connect, my name and picture will be inserted to my flat html file..
Let's assume that there'are already 4 users online.. and the 5th data will be mine.

Pic1 Name1
Pic2 Name2
Pic3 Name3
Pic4 Name4
Pic5 Name5

what i want is,
if i click Logout, i want my picture and name be deleted from the flat file (onlineusers.html)
and destroy my 2 variables set at test.php


Please help me..
Thank you!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [HELP] fwrite and delete function

Post by jacek »

Read the contents of the file, replace the bit you don't want with str_replace, then write the rest back to the file.

But why not make onlineusers.html into onlineusers.php and use a database ?
Image
TeeGee
Posts: 5
Joined: Tue Jan 31, 2012 5:32 am

Re: [HELP] fwrite and delete function

Post by TeeGee »

because i dont want to use too much CPU usage.. :D
and i use jquery to refresh onlineusers.html every 2 seconds without quering. :D
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: [HELP] fwrite and delete function

Post by bowersbros »

a database is more efficient then file writing
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [HELP] fwrite and delete function

Post by jacek »

TeeGee wrote:because i dont want to use too much CPU usage.. :D
and i use jquery to refresh onlineusers.html every 2 seconds without quering. :D
True, but all that file reading and writing with string manipulation in the middle is most likely going to use more CPU than the database.

You could look into the serialize() and unserialize() functions if you want to avoid the DB. They basically allow you to store an array in a file and read it back.
Image
Post Reply