Développement d'applications Web - 2ème Partie <author>Anderson Silva <!-- Compléter: Linux Gazette n°47 - Traducteur: Carine Bournez --> <!-- L'article sera coupé ici... --> <sect>Développement d'applications Web - 2ème Partie <p>Par Anderson Silva <tt>afsilva@liberty.edu</tt> <!-- La traduction --> <!-- <HR> --> <p>Comme promis, je vais vous donner, les amis, un autre exemple de programme php3. Ce programme est très simple, et quand même assez utile. <p> Une nuit, j'étais à l'université en train de travailler et j'ai essayé d'appeler ma femme à la maison. Malheureusement, j'avais laissé mon ordinateur connecté ce jour-là, et je ne pouvais pas m'en sortir. Donc, comme Apache tourne sur mon ordinateur, j'ai décidé de scanner les IPs de mon FAI et trouver quel ordinateur était le mien, afin de faire un telnet dessus et le déconnecter à distance. <p> La manière la plus rapide et la plus simple de faire ceci est soit php3, soit java, mais je ne suis pas très fan d'applets java, j'ai décidé de le faire en php3. <p> Et voilà comment ça se présente : <p><tscreen><verb> <? //Author: Anderson Silva //Date: September, 1999 //Opens socket and goes through a bunch of sequential IPs, and //it returns all the address that have a web server running. // This loop will go through all addresses in the block 10.0.0.x // The 10.0.0.x series is a fictional example, these IPs are normally // for intranet addresses, I am just trying to keep my ISP safe from // all of you guys :-) for($i=1; $i < 256; $i++) { // $path is the variable that will hold the URL you are testing. $path = "http://10.0.0.".$i; // Opens socket on server PAI, port 80. $fp = fsockopen("pai", 80, &$errno, &$errstr); // Sends the HTTP request that returns the info we need to know. fputs($fp,"GET $path HTTP/1.0\n\n"); set_socket_blocking($fp, false); // This is the string we wait for as a reply to the HTTP request. $str2 = "HTTP/1.0 200 OK"; // Gives the program 2 seconds to try to connect to the server. Sleep(2); // Captures the line from the HTTP request. $line = fgets($fp, 16); // If str2 is the same line, then we have a match, and there is a web // server running. Then go ahead and show me the name of the server with // a link to it. if (strcmp($line, $str2) == 0) echo "<A HREF=".$path.">".gethostbyaddr($pathhost)."</a><br>\n"; fclose($fp); } ?> </verb></tscreen> <p> Une remarque importante : ce procédé est très simple, mais très inefficace, <!-- ndt : inefficient, c'est pas vraiment inefficace, mais peu optimisé est une aussi mauvaise traduc, amha--> car pour chaque IP on attend au maximum 2 secondes. Donc, n'abusez pas de ce script, vous récupèreriez probablement une opération de dépassement de temps maximum (time out) de votre serveur web, ou bien vous resteriez bloqué en attente pendant toutes les itérations pour un bon moment. <p> Cela dit, c'est un bon exemple de ce que vous pouvez faire comme simple utilisateur avec php3. <p> Le mois prochain, j'écrirai encore un autre exemple de programme php3, cette fois-ci je vous montrerai comment créer votre propre livre d'or, en utilisant php3 et mySQL. <!-- <HR> --> <p>Copyright 1999, Anderson Silva. Paru dans le numéro 49 de la Linux Gazette de Janvier 2000. <p>Traduction française de Carine Bournez. </sect> <!-- ...et ici --> </article> <!-- Et enfin: * pour vérifier que vous n'avez pas fait d'erreur: sgmlcheck nomfichier * vous pouvez me l'envoyer gzipé ou bzipé, mais PAS UUENCODE SVP --> <!-- MERCI :-) -->