Miscellaneous Code

  Home arrow Miscellaneous Code arrow PHP Web Server
MISCELLANEOUS CODE

PHP Web Server
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2002-08-01

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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

    blog comments powered by Disqus
    Antalya eskort Antalya escort bayanlar

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...

    Developer Shed Affiliates

     



    © 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap