1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with redirecting a domain based on time of day

Discussion in 'Web Design' started by ppcgod, Dec 2, 2010.

  1. ppcgod

    ppcgod Registered Member

    Joined:
    Feb 18, 2009
    Messages:
    54
    Likes Received:
    32
    I have a domain that I want to redirect to site A between 7amEST-7pmEST and redirect conversely to site B from 7pm-7am. (hiding from someone lol) Right now I just have to log in and edit my meta refresh manually every day (gets quite annoying).
    Is there a script to make it change redirect automatically based on a server side timing? I tried to gogle it but it was confusing sorry I am a noob with php and html helppp lol... or if anyone knows a good site to read up on this would help me too. i tried to goog the problem did not help much.
     
  2. Strygwyr

    Strygwyr Junior Member

    Joined:
    Apr 23, 2010
    Messages:
    154
    Likes Received:
    1,462
    Location:
    Canada
    This will redirect them based on THEIR timezone. So if it's 7am-7pm their timezone then this is it. Adjust the code as needed depending on the timezone the visitor is in. This isn't the only way to do this though.

    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function ampmRedirect(dayURL, nightURL)
    {
        var currentTime;
        var currentHour;
    
        // get the current date and time
        currentTime = new Date();
    
        // returns the hour in military time
        currentHour = currentTime.getHours();
        // check if the hour is between 6pm and 6am
        if ((currentHour < 7) || (currentHour > 19)) {
            window.location = nightURL;
        } else {
            window.location = dayURL;
        }
    }
    // -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM>
    Click here to go to the next page:<BR>
    <INPUT TYPE="button" VALUE="Next"
    onClick="ampmRedirect('day.htm','night.htm')">
    </FORM>
    </BODY>
    </HTML>
    
     
    Last edited: Dec 4, 2010
  3. Strygwyr

    Strygwyr Junior Member

    Joined:
    Apr 23, 2010
    Messages:
    154
    Likes Received:
    1,462
    Location:
    Canada
    EDIT: Double post - browser lagged :(
     
  4. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    And you can do it in automatically PHP as well (off the top of my head, so this hasn't been tested):

    Code:
    <?php
       $hour = date('G');
       if ( $hour >= 7 && $hour <= 19 )   // If its between 7am and 7pm display site1.com
       {
          header("Location: http://site1.com");
       }
       else
       {
          header("Location: http://site2.com");
       }
    ?>
    
     
  5. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Ooops - forum said there was a problem with my posting - this is a dup.
     
    Last edited: Dec 4, 2010
  6. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Ooops - forum said there was a problem with my posting - this is a dup.
     
    Last edited: Dec 4, 2010
  7. beolion

    beolion Junior Member

    Joined:
    Aug 10, 2010
    Messages:
    113
    Likes Received:
    9
    Fatboy do you have any idea how to redirect based on referr domain .tld ?

    If the referrer tld is .com go site 1 else site 2

    Thanks
     
  8. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Again, off the top of my head try something like:

    Code:
    <?php
       if ( strpos($_SERVER['HTTP_REFERER'], ".com"))   
       {
          header("Location: http://site1.com");
       }
       else
       {
          header("Location: http://site2.com");
       }
    ?>
    
    If that doesn't work give me a shout and I will get on my laptop and figure it out :)
     
    • Thanks Thanks x 1
  9. beolion

    beolion Junior Member

    Joined:
    Aug 10, 2010
    Messages:
    113
    Likes Received:
    9
    Very simple but effective, is working.
    One more thing, how to add multiple TLDs or ccTLDs? if .com, .net, .co.uk ?
     
  10. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Drop me a PM and I will sort that out - its very easy :)
     
  11. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Sorry, was a bit late coming back to this one.
    Try something like:

    Code:
    <?php
       if ( strpos($_SERVER['HTTP_REFERER'], ".com"))   
       {
          header("Location: http://site1.com");
       }
    
       if( strpos($_SERVER['HTTP_REFERER'], ".net"))
       {
          header("Location: http://site1.com");
       }
    
       if( strpos($_SERVER['HTTP_REFERER'], ".co.uk"))
       {
          header("Location: http://site3.com");
       }
    
    ?>
    
    FB
     
    • Thanks Thanks x 1
  12. beolion

    beolion Junior Member

    Joined:
    Aug 10, 2010
    Messages:
    113
    Likes Received:
    9
    Once again thanks for the help, the second function works as well. :D