Page 1 of 1

PHP Redirect unless site down

Posted: Sat Feb 12, 2011 8:39 pm
by kanen
(for those who have been asking)

I created this because I am redirecting all traffic from port 80 to port 8080. Port 8080 runs a newLisp web server and DragonFly, but it could be running anything.

Sometimes, for whatever reason, my ISP decides to kill the newLisp process.

So, this redirects people to a "maint.html" page when that happens.

I hope this is as useful for everyone else as it has been for me.

Plus, it's super fast and simple PHP code.

Code: Select all

<?php
        $site = "www.mysite.com";
        $port = 8080;
        $fp = fsockopen($site,$port,$errno,$errstr,10);
        if(!$fp)
        {
                $fd = fopen("maint.html", r);
                $content = fread($fd, filesize("maint.html"));
                fclose($fd);
                echo $content;
        }
        else{
                header("Location: http://www.mysite.com:8080");
                //echo "Connect was successful - no errors on Port ".$port." at ".$site;
                fclose($fp);
                exit;
        }
        //
?>