web page redirecting

We are making a website for our client.
what my intentions are is to open the html version of the webpage page stored locally on the clients machine.
<this is where i need some help>
if a connection to the clients server is available, reload the page from the server. (i will do this via IP address)failing that if the local page cant connect to the clients server i want it to redirect the page to myself where i will store the same page as the clients server.
If neither can be reached i want the clients page to say "please contact your administrator"

What do i need to add to my source code to make this work if possible? or alternatively how can i acheive this result?
Thanks so much for any help posted!
Wizard_ITAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexanderRConnect With a Mentor Commented:
It works fine on my computer. Lets try this again.  Erase all you had before (or even start a new file).  Put this in:

<?php

$clientsite = "http://www.experts-exchange.com";
$mydevsite = "google.coms";


$clientsite = str_replace("http://","",strtolower($clientsite));
$mydevsite = str_replace("http://","",strtolower($mydevsite));

function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

if (pingDomain($clientsite) != -1) {
echo '<font color="green">'.$clientsite.' is Active</font>';
} else {
echo '<font color="red">'.$clientsite.' is unavailable</font>';
}
echo '<br /><br />';
if (pingDomain($mydevsite) != -1) {
echo '<font color="green">'.$mydevsite.' is Active</font>';
} else {
echo '<font color="red">'.$mydevsite.' is unavailable</font>';
}

?>

Remember to properly replace
$clientsite = "http://www.experts-exchange.com";
$mydevsite = "google.coms";

Your IP or url or whatever must go inbetween those quotes like so
$clientsite = "24.231.61.123";
$mydevsite = "google.com";
0
 
netsmithcentralCommented:
As far as I know, you cannot force a user to use a cached version of you site.  You CAN set some headers to force the client NOT to use a cached page (cache-control and pragma).  Otherwise, it's up to the user's settings whether or not to access your page directly or from the cache.
0
 
AlexanderRCommented:
If this action is to be performed from local (non server) machine from an HTML file, the this is the wrong zone.  JavaScript and AJAX can only do that (link verification).
Now if that "starter" page is on a server it would be very easy in php.

I'll try to work on user browser side.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AlexanderRCommented:
netsmithcentral
What he wants is for user to open up an  .html file from his desktop for example that will redirect him to either real web server or his developer machine.

I believe thats it.
0
 
AlexanderRCommented:
I dont think it can be done on regular browser...
The best thing i could come up with is at http://krijnhoetmer.nl/stuff/javascript/xmlhttprequest-link-checker/  and even in that, AJAX code passes the links on to their php application on their server that does the work and then returns results back to browser through xmlhttp.

That seems unnecessary complecated.  What if you are to make a php page on your dev server that does the work and have your users go that page and then be redirected to appropriate place.
0
 
netsmithcentralCommented:
Sorry if I misunderstood the question.  Here's a solution using javascript that will work from a single HTML file on the client's actual computer (edit the clientsite and mysite variables with the appropriate sites):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Site Redirector</title>
<script type="text/javascript">
<!--
window.onload = function() {
    var clientsite = "http://www.clientsite.com";
    var mysite = "http://www.mysite.com";
    var error = "";

    var req = null;
    if(window.XMLHttpRequest) req = new XMLHttpRequest();
    else if(window.ActiveXObject){
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
       }
    }

    if(req==null){
        error="<p>Browser does not support AJAX!</p>" +
                  "<p>You must use an AJAX compliant browser to view this site.  Try:" +
                  "<ul><li>Internet Explorer 5+<li>Firefox 1+</ul>" +
                  "</p>";
        return;
    }

    req.open("GET", clientsite, true);
    req.send(null);
    if(req.status==200) document.location.replace(clientsite);
   
    req.open("GET", mysite, true);
    req.send(null);
    if(req.status==200) document.location.replace(mysite);

    error = "Please contact your system administrator.";
}
//-->
</script>
</head>
<body>
<script type="text/javascript">document.write(error);</script>
</body>
</html>
0
 
netsmithcentralCommented:
FYI, you'll need to have the user adjust their security settings to allows XMLHTTP requests to other servers.
0
 
AlexanderRCommented:
That was nice! status of 200 -should have guessed.
Yes that security setting is preventing it to work.    How would you disable that? I cant find it in any security settings (opera, firefox, IE)
0
 
netsmithcentralCommented:
The privilege is called "UniversalBrowserRead".  For a local IE page, you'll get the information bar telling you "This page is trying to run unsafe content".  For a local FF page, you need to edit the script as follows, and then you'll get a "allow potentially unsafe operation" message.  Unfortunately, even with "good" code, there's no way to avoid the confirmation messages (except server side scripting).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Site Redirector</title>
<script type="text/javascript">
<!--
window.onload = function() {
    var clientsite = "http://www.clientsite.com";
    var mysite = "http://www.mysite.com";
    var error = "";

    var req = null;
    if(window.XMLHttpRequest){
        req = new XMLHttpRequest();
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        } catch(e) {
            alert(e);
        }
    } else if(window.ActiveXObject){
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
       }
    }

    if(req==null){
        error="<p>Browser does not support AJAX!</p>" +
                  "<p>You must use an AJAX compliant browser to view this site.  Try:" +
                  "<ul><li>Internet Explorer 5+<li>Firefox 1+</ul>" +
                  "</p>";
        return;
    }

    req.open("GET", clientsite, true);
    req.send(null);
    if(req.status==200) document.location.replace(clientsite);
   
    req.open("GET", mysite, true);
    req.send(null);
    if(req.status==200) document.location.replace(mysite);

    error = "Please contact your system administrator.";
}
//-->
</script>
</head>
<body>
<script type="text/javascript">document.write(error);</script>
</body>
</html>
0
 
Wizard_ITAuthor Commented:
Wow thanks guys!! you guys are amazing!  
ok, taking your advice onboard im gonna host the whole thing on my web domain and cut the local files out. (best option.)
what i want is for the client to open the page (online). and it to be able to tell me if a certain IP address are "pingable" (the clients server and then my server) i beleive this will acheive the same result that im looking for.
I want the results to say "CLServer Active"  and have a Green light  if the ping is returned or a red light if the ping times out.

Thanks!



0
 
AlexanderRCommented:
<?php

$clientsite = "http://www.experts-exchangessssssssssssss.com";
$mydevsite = "google.com";


$clientsite = str_replace("http://","",strtolower($clientsite));
$mydevsite = str_replace("http://","",strtolower($mydevsite));
echo $clientsite;
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}


if (pingDomain($clientsite) != -1) { ?>
   
   <font color="green"> Client site is active!</font>
   
   You can also use this javascript to redirect automatically.
   <script type="text/javascript">
      window.location='http://<?php echo $clientsite;?>';
   </script> <?php
} elseif(pingDomain($mydevsite) != 1) {?>
   <font color="red">Looks like main site is down!</font>
   <font color="green">But my developer is still there.</font>
   Use same technique for redirection
      <script type="text/javascript">
      window.location='http://<?php echo $mydevsite;?>';
   </script><?php
} else { ?>
   <font color="red">Looks like neither of the sites are on</font> <?php
}


?>


Function is borrowed from hotscripts.com.  How i used it is my idea.  If javascript is disabled then send the header similar to header('Location: <url of page>');
exit();

Of course you can replace my output with whatever you want, like regular links or a gif with green or red light ;).
0
 
AlexanderRCommented:
BTW i still cant get that AJAX to work.  In FF i get:
uncaught exception:[Exception..."Component returned failure code: 0x8004011(NS_ERROR_NOT_AVAILABLE)[nsIXMLHttpRequest.status]" and so on.
I did allowe the unsecured content.
0
 
netsmithcentralCommented:
It's possible FF doesn't like me reusing the same XMLHttpRequestObject for both domain calls.  I didn't want to have to write the try/catch logic to make a valid Request object twice so I did it to save time.  If you enclose that in a function that returns a request and make a new request before calling it you should be fine.
0
 
Wizard_ITAuthor Commented:
you both deserve the points! thank you so much for your help
im going to give the points to Alex as hes come a long long way with his php amongst other thigns and doesnt hold an experts rank yet.
and he made the last script for me :-)
thank you both
0
 
AlexanderRCommented:
thank you kindly :).  In that case should give netsmithcentral an assist.  Does my script work for you?
0
 
Wizard_ITAuthor Commented:
How can i give him an assist? i would like to
the script works brilliant thank you so much!  however i have a few questions.
1: how can i stop the redirect within the javascript (what do i delete?)
2:will it tell me if the "client site" is unavailable? <it tells me "client site is Active" but would it tell me in red if the site is unavailable? >

could you make the script so one says active and one says inactive
i should be able to take it from there :-)
thanks again
0
 
Wizard_ITAuthor Commented:
disregard the part about stopping the redirect, sussed that out -  just me being stupid  
0
 
AlexanderRCommented:
redirect is kept in javascript block like so  <script type="text/javascript">
      window.location='http://<?php echo $mydevsite;?>';
   </script>.

If i understand you want a summary of whats online and what isnt and then have a user click on the link of the one thats online?

echo "Client site is "
if (pingDomain($clientsite) != -1) {
echo '<a href="http://'.$clientsite.'"><font color="green">Active</font>';
} else {
echo '<font color="red">unavailable</font>';
}
echo '<br /><br />
My development site is '
if (pingDomain($mydevsite) != -1) {
echo '<a href="http://'.$mydevsite.'"><font color="green">active</font>';
} else {
echo '<font color="red">unavailable</font>';
}
0
 
AlexanderRCommented:
My development site is '
is out to have a semicolon on end
My development site is ';

and so is echo "Client site is ";
0
 
Wizard_ITAuthor Commented:
yes i just need a summery of 2 IP addresses, to tell the client if they are online or offline
i dont need them to link anywhere just display if certain IP's are online or off(and i hope to modify this to have little coloured status lights)


thanks


0
 
Wizard_ITAuthor Commented:
thanks for your help i really appreciate this!
0
 
AlexanderRCommented:
if (pingDomain($clientsite) != -1) {
echo '<font color="green">'.$clientsite.' is Active</font>';
} else {
echo '<font color="red">'.$clientsite.' is unavailable</font>';
}
echo '<br /><br />';
if (pingDomain($mydevsite) != -1) {
echo '<font color="green">'.$mydevsite.' is Active</font>';
} else {
echo '<font color="red">'.$mydevsite.' is unavailable</font>';
}
0
 
Wizard_ITAuthor Commented:
im sorry im really struggling now:

when i load the page this is what is displayed:

'; } else { echo ''.$clientsite.' is unavailable'; } echo '

'; if (pingDomain($mydevsite) != -1) { echo ''.$mydevsite.' is Active'; } else { echo ''.$mydevsite.' is unavailable'; } ?>

Could you incorporate that last bit into your original script and make the small changes you pointed out
iv totally messed up your very fine work :(
0
 
AlexanderRCommented:
<?php
$clientsite = str_replace("http://","",strtolower($clientsite));
$mydevsite = str_replace("http://","",strtolower($mydevsite));
echo $clientsite;
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

if (pingDomain($clientsite) != -1) {
echo '<font color="green">'.$clientsite.' is Active</font>';
} else {
echo '<font color="red">'.$clientsite.' is unavailable</font>';
}
echo '<br /><br />';
if (pingDomain($mydevsite) != -1) {
echo '<font color="green">'.$mydevsite.' is Active</font>';
} else {
echo '<font color="red">'.$mydevsite.' is unavailable</font>';
}

?>
0
 
Wizard_ITAuthor Commented:
ok i tried that -
i now receive:

'; } else { echo ''.$clientsite.' is unavailable'; } echo '

'; if (pingDomain($mydevsite) != -1) { echo ''.$mydevsite.' is Active'; } else { echo ''.$mydevsite.' is unavailable'; } ?>
0
 
Wizard_ITAuthor Commented:
PS: i hope you get your expert badge soon - i dont know how you dont have it already! hehe
i ought to give you medal of some sort lol
0
 
Wizard_ITAuthor Commented:
ok iv rebooted, (for my local php servers sake)
even tried loading it in both FF and IE
i still recieve:

'.$clientsite.' is Active'; } else { echo ''.$clientsite.' is unavailable'; } echo '

'; if (pingDomain($mydevsite) != -1) { echo ''.$mydevsite.' is Active'; } else { echo ''.$mydevsite.' is unavailable'; } ?>

i must giving you a headache :-( sorry
0
 
AlexanderRCommented:
My code works just fine on my computer without any changes.  Please post your full file for me to see.
0
 
AlexanderRCommented:
also in your php.ini enable display of all errors E_ALL
0
 
Wizard_ITAuthor Commented:
this is my full souce code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<?php

$clientsite = "192.168.0.55";
$mydevsite = "192.168.0.55";


$clientsite = str_replace("http://","",strtolower($clientsite));
$mydevsite = str_replace("http://","",strtolower($mydevsite));

function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

if (pingDomain($clientsite) != -1) {
echo '<font color="green">'.$clientsite.' is Active</font>';
} else {
echo '<font color="red">'.$clientsite.' is unavailable</font>';
}
echo '<br /><br />';
if (pingDomain($mydevsite) != -1) {
echo '<font color="green">'.$mydevsite.' is Active</font>';
} else {
echo '<font color="red">'.$mydevsite.' is unavailable</font>';
}

?>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Remember to properly replace
  $clientsite = "http://www.experts-exchange.com";
  $mydevsite = "google.coms";
 
Your IP or url or whatever must go inbetween those quotes like so
  $clientsite = "24.231.61.123";
  $mydevsite = "google.com"; </p>
</body>
</html>
0
 
AlexanderRCommented:
Just copied your whole thing to mine and works just fine.
I assume your file name has extension php.  And you said it worked with my first code??

To see what exactly is wrong you need error reporting on in php.ini file.   In that file check that
display_errors = On
error_reporting  =  E_ALL & ~E_NOTICE & ~E_WARNING

Then restart your http service and run file again.  See what errors it gives
0
 
Wizard_ITAuthor Commented:
Right!
i restarted my http
checked my php.ini and is enabled
rebooted pc

now it works?! page displayed: 127.0.0.1 is Active

Warning: fsockopen() [function.fsockopen]: unable to connect to 192.168.0.18:80 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\wamp\www\ServerStatus PHP and JAVASCRIPT\TestingFinal.php on line 22
192.168.0.18 is unavailable
 
Perfect! (/18 doesnt exist but i wanted to test the two variables obviously)
I really cannot thank you enough alex!
is there some way i can commend you or give you more points?
0
 
AlexanderRCommented:
ok, but you obviously dont want that error to be showing up to users.  I dont really see why i get to have it working without that ugly warning message.
0
 
Wizard_ITAuthor Commented:
when i roll this out to the client, if i diable error messages within php.ini on my devserver the client will not see these warnings if i understand correctly?
0
 
AlexanderRCommented:
Right.  Having error_reporting  =  E_ALL & ~E_NOTICE & ~E_WARNING should be sufficient to suppress that one kind of error.
What i also notice on mine, is that it stopped working so well on IP address basis on my computer.  Its accurate on URL though. But it seems like it works for you because your 192.168.0.18 is unavailable.
0
 
Wizard_ITAuthor Commented:
well now i have the two main ones in there i can add additional ones and test a whole range of IP's
Ill bear that in mind though if things stop working to test an Url in place of an IP.
Thanks again! only took us 6 hours! lol
hope to see you again on another question.
Thanks a million!

Jay
0
All Courses

From novice to tech pro — start learning today.