+ Reply to Thread
Results 1 to 3 of 3

Thread: IF/THEN statement to pick which site to navigate to?

  1. #1
    MikeHGA79's Avatar
    MikeHGA79 is offline x10Hosting Member MikeHGA79 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    24

    IF/THEN statement to pick which site to navigate to?

    Ok...I'll do my best to explain. Essentially I have two identical websites (mirrors I guess) that are with two different hosts. Let's call them Site A & Site B. I'd like to have a 3rd site to serve as a portal. We'll call that "Portal". All users will access Portal....at that time I'd like to have some type of script that would look at Site A, if Site A is up, then proceed to Site A. If Site A is down, go to Site B. How is this possible? Currently I just have a iFrame sitting in the Portal and it's pointing to whatever site is up and/or running faster. Of course I am manually updating this throughout the week. I'm using some free website monitoring services that I thought I could use in this whole formula I'm just not sure how to implement it.

    Any ideas? And oh yeah...the goal is to do all this for zero $$.

    Thanks!
    cPanel- MikeHGA7

  2. #2
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: IF/THEN statement to pick which site to navigate to?

    Ok, keep in mind this is ABSOLUTELY POSITIVELY UNTESTED AND I DO NOT TAKE ANY RESPONSIBILITY FOR WHAT THIS CODE MAY OR MAY NOT DO PROPERLY!

    Thank you.


    What I would try is using something similar to this:

    Code:
    $site_up=fopen("www.site.com/index.php","r");
    if ($site_up != false) {
    fclose($site_up); //close this out so its not tying up resources.
    header("Location: www.site.com");
    }
    else {
    header("Location: www.site2.com");
    }

    Basically, fopen will try to open that website address. If it does open, great - the site must be up, so close the newly opened file handler and redirect the browser to that website.

    If it doesn't open (fopen returns false on error), then send the browser to the other site.


    It's kinda ugly though; I've never used fopen, so I've got no clue how it handles opening a page like that - in theory it should work, but I've got no testing on that, and I don't have any knowledge on how to change the headers being sent to the browser so it changes the location - I think I have it right, but I wouldn't be surprised to find it wrong >_<


    In any case, the basic idea should work just fine:

    1) Try to open a file on the main site.
    2) If we can open it, send the user to the main site (if we can access the file, then the site must be up). If we can't, send them to the backup.


    That could even be expanded so it'd go down a list of multiple backups - each time it can't open the site, have it try another one.


    Hope that helps - please don't try it publically first, try it somewhere private so if it does implode, it doesn't do it to your websites >_<

    Might not hurt to wait for someone else to see this and see what they say too.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  3. #3
    Hazirak's Avatar
    Hazirak is offline x10 Sophmore Hazirak is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Georgia
    Posts
    197

    Re: IF/THEN statement to pick which site to navigate to?

    Tested the script. It works with a few minor (and I truly mean minor) modifications.

    Code:
    <?php
    $site_up=@fopen("http://www.site1.com/index.php","r");
    if ($site_up != false) {
    	fclose($site_up); //close this out so its not tying up resources.
    	header("Location: http://www.site1.com/index.php");
    }
    else {
    	header("Location: http://www.site2.com/index.php");
    }
    ?>
    First of all, you NEED that 'http://' part infront of the URLs, otherwise it thinks you're looking for a file or directory at the current server named "www.site.com". Obviously that's not the effect we want.

    Second of all, while the script works if SiteA is up, PHP will throw an error and cancel execution of the script instead of redirecting to SiteB if SiteA is not up. Thus, we need to include an @ symbol in front of the call to fopen() to suppress the warning and allow the script to continue as intended.

+ Reply to Thread

Similar Threads

  1. a site without frames and a site with frame
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 18
    Last Post: 12-31-2007, 02:20 PM
  2. Link Exchange
    By Conor in forum Scripts & 3rd Party Apps
    Replies: 117
    Last Post: 12-09-2007, 12:20 PM
  3. My site.. Need staff... maybe...
    By dsfreak in forum Scripts & 3rd Party Apps
    Replies: 6
    Last Post: 08-05-2005, 10:07 PM
  4. Same site, diff sections
    By RollerC in forum Free Hosting
    Replies: 2
    Last Post: 07-28-2005, 03:10 PM
  5. New Site Design- Reward
    By NewFuture in forum Scripts & 3rd Party Apps
    Replies: 6
    Last Post: 07-26-2005, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers