Feb 15 2010

Mixing bots to gain new services

Category: Php,Spiders & web botsGiulio Pons @ 12:39 pm

Spiders and bots let you take services from other web sites, this could be very cool, but also this could become a problem (you are using stuff made from other people, is it correct? they know what you’re doing, are there any bandwidth problems you can cause? are your bots ok with copyright?).

Well let’s go over all this problems and try to make spider’s work even more cool: you can mix two or more spiders to create something new, in this example I’ve mixed geographic ip reference bot and meteo bot to get a meteo service localized for the user who connect at your site.
This is a geographic meteo as the ones you can find on smartphones.

Have you any ideas about other mix you can do? You can grab restourants and show localized restourants, shops… There are many applications that do this on iPhone… the problem is, how much good is the result, and this depends on how good are the sources. But this geo mixes already sounds old, we have to find new mixex.

Share

Tags: , , , , ,


Dec 03 2009

How many users are connected?

Category: PhpGiulio Pons @ 11:08 am

This two function returns the number of users connected to your site.
Since HTTP is asyncronous, the only way to do this is to store the IPS and the moment when every user retrive a page.
And whenever a page is called, check if the user is already in the stored IPS, if he is already present update his timer else add him to the list.
You have to call the howManyIps() function in every page to keep the counter updated.
Watch demo, whatch source.

The first function get the IP of the connected user:

function getIP() {
	$ip="";
	if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
		else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
		else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
		else $ip = "";
	return $ip;
}

This function makes the job: store the IPS, count, store the timers.

function howManyIps() {
	$filename = "./howmanyip.log";
	$seconds = 300;
	$yourIP = getIP();

	if (file_exists($filename.".lock")) $readonly = true; else $readonly=false;

	$count = 0;
	//lock the file
	if (!$readonly) $fpLock = fopen($filename.".lock", "w");

	//read data ips
	$fp = @fopen($filename, "r");
	$arIPS=explode ("\n", @fread($fp,filesize($filename)) );
	@fclose($fp);

	//if file is locked get out
	if ($readonly) return count($arIPS);

	$s = "";
	$already=false;
	//update data and search user ip
	for ($i=0;$i<count($arIPS);$i++) {

		$arData= explode (" ", $arIPS[$i]);

		//update your user timer
		if ($yourIP==$arData[0]) {
			$already=true;
			$arData[1]=time();
		}

		// check if user is old
		if ( time()- (integer)$arData[1] < $seconds ){
			$s.=$arData[0]." ".$arData[1]."\n";
			$count++;
		}

	}

	if (!$already) {
		//your user is new, add it to the list
		$s.=$yourIP." ".time()."\n";
		$count++;
	}

	//save the list
	$fp = fopen($filename, "w");
	fwrite($fp,$s);
	fclose($fp);

	//remove thr lock
	fclose($fpLock);
	unlink($filename.".lock");

	return $count;
}
Share

Tags: , , , , ,