This is a Web Server entierly written in PHP. PHP 4.2.2 is needed with Sockets included in the ini file...I had a problem with images and zips but its fixed now.
By : micah
<?php
//MicahServer v. 3.2PHP
//By Micah Lansing - sw_freak@hotmail.com
//www.ForeverCode.d2g.com
//August 1st 2002
echo "<html><head><title>Micah's PHP Web server!</title></head><body>";
set_time_limit (0); //Timout, keep at 0
ob_implicit_flush (); //Output data to the web brouser or console as we go
$address = '192.168.1.3'; //your ip
$port = 80; //Port to listen
$sock = socket_create (AF_INET, SOCK_STREAM, 0) or die ("socket_create() failed.");
@socket_bind ($sock, $address, $port) or die ("socket_bind() failed");
@socket_listen ($sock, 5) or die("socket_listen() failed");
echo "<font color = 'red'>Server Started.<br><br></font>";
do {
if (($Thesocket = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
if (FALSE === ($buf = socket_read ($Thesocket, 2048))) {
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
break 2;
}
$some = substr ( $buf, 4, strlen($buf));
$rest = strstr ( $some, " ");
$request = substr ( $some, 0 , strlen($some) - strlen($rest));
if($request == "/Down") die ("<br><br><font color = 'red'>Server Shut Down.</font>");
if($request == "/"){
$request = "\index.html";
$mime = "text/html";
} else {
$rev = strrev($request);
$dot = strstr ( $rev, ".");
$GetEX = substr ( $request, strlen($dot), strlen($request) - strlen($dot));
switch ( $GetEX ) {
case $GetEX == "gif":
$mime = "image/gif";
break;
case $GetEX == "jpg":
$mime = "image/jpeg";
break;
case $GetEX == "zip":
$mime = "aplication/zip";
break;
case $GetEX == "exe":
$mime = "aplication/exe";
break;
case $GetEX == "bmp":
$mime = "image/bmp";
break;
case $GetEX == "mov":
$mime = "video/quicktime";
break;
case $GetEX == "mp3":
$mime = "video/mpeg";
break;
case $GetEX == "mpeg":
$mime = "video/mpeg";
break;
case $GetEX == "txt":
$mime = "text/plain";
break;
default:
$mime = "text/html";
break;
}
}
$request = ereg_replace ( "/", "//", $request);
$content = "";
$filename = "C:\html\phpserver" .$request;
$fd = @fopen ($filename, "rb");
if ($fd){
$content = fread ($fd, filesize ($filename));
fclose ($fd);
}
if( $content == "" ){
$lens = strlen($content);
$datas = "HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: MicahServer 3.2PHP\r\nDate: 8/01/02\r\nConnection: close\r\nContent-Length: $lens\r\nContent-Type: text/html\r\n\r\n$content";
socket_write ($Thesocket, $datas, strlen ($datas));
} else {
$lens = filesize ($filename);
$today = date("m.d.y");
$datas = "HTTP/1.0 200 OK\r\nServer: MicahServer 3.2PHP\r\nDate: $today\r\nConnection: close\r\nContent-Length: $lens\r\nContent-Type: $mime\r\n\r\n";
socket_send ($Thesocket, $datas, strlen ($datas), 0);
$S = 0;
$copyD = "";
$DD = filesize ($filename);
while($S <= $DD){
$TempD = substr( $content, $S, 2048);
$S += 2048;
socket_write ($Thesocket, $TempD, strlen ($TempD));
}
}
echo "<pre>$buf</pre>\r\n<br>\r\n";
socket_close ($Thesocket);
} while (true);
socket_close ($sock);
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |