1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Developing Web Applications - Part II LG #49</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<!-- *** BEGIN navbar *** -->
<A HREF="lg_toc49.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../lg_frontpage.html"><IMG ALT="[ Front Page ]"
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="pramode.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
<A HREF="../lg_faq.html"><IMG ALT="[ Linux Gazette FAQ ]"
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="silva2.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
<!-- *** END navbar *** -->
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Developing Web Applications - Part II</font></H1>
<H4>By <a href="mailto:afsilva@liberty.edu">Anderson Silva</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
As I promised, I was going to give you guys another example of a php3 program.
This program is a very simple program, and yet somewhat useful.</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
One night, I was at the university working and I tried to call home to
talk to my wife. Unfortunately, I had left my computer connected that day,
and could not get through. So, since my computer was running Apache, I
decided to scan through the IPs of my ISP , and find out which computer
was my computer, so I could telnet to it, and remotely disconnect it.</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
The fastest, and simplest way to do this was either through php3 or java,
but since I am not very fond of java applets, I decided to do it in php3.</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; ">
And here is how it goes:</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b><?</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
//Author: Anderson Silva</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
//Date: September, 1999</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
//Opens socket and goes through a bunch of sequential IPs, and</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
//it returns all the address that have a web server running.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// This loop will go through all addresses in the block 10.0.0.x</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// The 10.0.0.x series is a fictional example, these IPs are normally</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// for intranet addresses, I am just trying to keep my ISP safe from</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// all of you guys :-)</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
for($i=1; $i < 256; $i++)</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
{</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// $path is the variable that will hold the URL you are testing.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
$path = "http://10.0.0.".$i;</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// Opens socket on server PAI, port 80.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
$fp = fsockopen("pai", 80, &$errno, &$errstr);</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// Sends the HTTP request that returns the info we need to know.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
fputs($fp,"GET $path HTTP/1.0\n\n");</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
set_socket_blocking($fp, false);</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// This is the string we wait for as a reply to the HTTP request.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
$str2 = "HTTP/1.0 200 OK";</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// Gives the program 2 seconds to try to connect to the server.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
Sleep(2);</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; ">
<br><b> // Captures the line from the
HTTP request.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
$line = fgets($fp, 16);</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// If str2 is the same line, then we have a match, and there is a web</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// server running. Then go ahead and show me the name of the server with</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
// a link to it.</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
if (strcmp($line, $str2) == 0)</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
echo "<A HREF=".$path.">".gethostbyaddr($pathhost)."</a><br>\n";</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
fclose($fp);</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>
}</b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: normal; color: Black; background-color: White; "><b>?></b></div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
One important observation, this process is very simple, but it is very
inefficient, since for every IP you check you will wait a maximum of 2
seconds. So, don't abuse this script, because you will probably get a time
out operation from your web server, or you will be stuck waiting for all
the iterations for a long time.</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
But still, it is a good example of what you as a home user can do with
php3.</div>
</div>
<div style="
padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<div style="text-indent: 0.00mm; text-align: left; line-height: 8.333333mm; color: Black; background-color: White; ">
Next month, I will write yet another example of php3 program, this time
I will show you how to create your own guest book, using php3 and mySQL.</div>
</div>
<!-- *** BEGIN copyright *** -->
<P> <hr> <P>
<H5 ALIGN=center>
Copyright © 2000, Anderson Silva<BR>
Published in Issue 49 of <i>Linux Gazette</i>, January 2000</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<P> <HR> <P>
<!-- *** BEGIN navbar *** -->
<A HREF="lg_toc49.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../lg_frontpage.html"><IMG ALT="[ Front Page ]"
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="pramode.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
<A HREF="../lg_faq.html"><IMG ALT="[ Linux Gazette FAQ ]"
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="silva2.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
<!-- *** END navbar *** -->
</BODY></HTML>
<!--endcut ============================================================-->
|