+ Post New Thread
Results 1 to 10 of 10
Coding Thread, PHP to check url and if local goto one address if external goto another in Coding and Web Development; hi there currently working away on Moodle VLE for my School just trying to tidy up some parts. and i ...
  1. #1
    tri_94's Avatar
    Join Date
    Feb 2007
    Location
    Lincolnshire
    Posts
    117
    Thank Post
    2
    Thanked 2 Times in 2 Posts
    Rep Power
    17

    PHP to check url and if local goto one address if external goto another

    hi there currently working away on Moodle VLE for my School just trying to tidy up some parts. and i would really like to loss some of the links i currently have.

    so im after a bit of magic in the form of php coding to enable me to say

    if local url then goto one address if external goto a different address

    please help
    thanks
    nick

  2. #2
    danbuntu's Avatar
    Join Date
    Dec 2009
    Location
    Maidstone, Kent
    Posts
    305
    Thank Post
    0
    Thanked 56 Times in 53 Posts
    Rep Power
    19
    You're probably better doing this kind of think in the .htacess file.

    htaccess Redirect

  3. #3
    tri_94's Avatar
    Join Date
    Feb 2007
    Location
    Lincolnshire
    Posts
    117
    Thank Post
    2
    Thanked 2 Times in 2 Posts
    Rep Power
    17
    hi there
    what it is that we have drop down menus with hyperlinks, but rather then have loads one for external and one for internal everything, we wanted to replace them with a single one for each link.

    and use the power or php/html to check which url the user is on and there direct them accordly.

    any ideas?

  4. #4
    danbuntu's Avatar
    Join Date
    Dec 2009
    Location
    Maidstone, Kent
    Posts
    305
    Thank Post
    0
    Thanked 56 Times in 53 Posts
    Rep Power
    19
    are the internal and external links linking to the same things? ie are you using something like moodle.college.ac.uk for one and moodle.local for the other?

    or are they going to completely separate sites? ie moodle.college.ac.uk for one and google.co.uk for the other?

  5. #5
    tri_94's Avatar
    Join Date
    Feb 2007
    Location
    Lincolnshire
    Posts
    117
    Thank Post
    2
    Thanked 2 Times in 2 Posts
    Rep Power
    17
    they are separte sites.

  6. #6
    tri_94's Avatar
    Join Date
    Feb 2007
    Location
    Lincolnshire
    Posts
    117
    Thank Post
    2
    Thanked 2 Times in 2 Posts
    Rep Power
    17
    Think i've got it sorted now. using the following php code.


    <ul><li><a title="E-mail" href="<?php if ($CFG->wwwroot == 'http://ourdomain') { ?> https://email.ourdomain/ <?php } else { ?> https://mail-server <?php }; ?>" <a align="left" >E-mail</a></a></li>



    thanks
    nick

  7. #7
    mdench's Avatar
    Join Date
    Oct 2011
    Location
    Billingshurst
    Posts
    809
    Thank Post
    40
    Thanked 104 Times in 94 Posts
    Rep Power
    39
    Could not use a DNS record to redirect the users internally instead of them going out and then back in?

  8. #8
    danbuntu's Avatar
    Join Date
    Dec 2009
    Location
    Maidstone, Kent
    Posts
    305
    Thank Post
    0
    Thanked 56 Times in 53 Posts
    Rep Power
    19
    Quote Originally Posted by tri_94 View Post
    Think i've got it sorted now. using the following php code.


    <ul><li><a title="E-mail" href="<?php if ($CFG->wwwroot == 'http://ourdomain') { ?> https://email.ourdomain/ <?php } else { ?> https://mail-server <?php }; ?>" <a align="left" >E-mail</a></a></li>



    thanks
    nick

    $CFG->wwwroot is the moodle url as set in your config file, so that code will always return true.

    you want to use the global $_SERVER variable: PHP: $_SERVER - Manual ; something like

    if ($_SERVER['SERVER_NAME'] == 'mydomian.co.uk') {
    echo 'url1';
    } else {
    echo 'url2';
    }

  9. #9

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    169
    Have a look at the <base> tag HTML base tag

    You can use the PHP $_SERVER to check the server that a user is accessing from and then set the default base path accordingly.
    This will then affect all relative links on the page.

    Code:
    if ($_SERVER['SERVER_NAME'] == 'mydomain.co.uk') {
    echo "<base href=\"http://mydomain.co.uk/\">";
    } else {
    echo "<base href=\"http://alternativedomain.co.uk/\">";
    }
    This should be in the <HEAD> section of the page.

  10. #10
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    896
    Thank Post
    84
    Thanked 235 Times in 194 Posts
    Rep Power
    82
    Install Net_IPv4 via Pear (Net_IPv4), then whack the following somewhere before where you want the link... (eg: in Moodle's config.php)

    Code:
    require 'Net/IPv4.php';
    $ip   = $_SERVER['REMOTE_ADDR'];   // Get user's IPv4
    
    // Define all LAN/LOCAL IP ranges...
    $net1 = '172.16.96.0/21';   // Curriculum Range
    $net2 = '10.113.140.0/24';  // Admin Range
    
    $CFG->localuser=4;
    
    if ((Net_IPv4::ipInNetwork($ip, $net1)=='1') || (Net_IPv4::ipInNetwork($ip, $net2)=='1')){
        $CFG->localuser=1;
    }
    then, for the link...

    Code:
    echo '<a href="'.($CFG->localuser==1 ? 'http://locallinky/' : 'http://www.notlocallinky.com/').'">Linky Text</a>';

    (I think)
    Last edited by Marci; 31st August 2012 at 09:01 AM.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 7
    Last Post: 16th December 2011, 05:54 AM
  2. Replies: 17
    Last Post: 19th March 2010, 11:51 PM
  3. Replies: 4
    Last Post: 25th December 2006, 09:15 PM
  4. Replies: 4
    Last Post: 23rd March 2006, 06:27 AM
  5. Replies: 3
    Last Post: 1st March 2006, 12:08 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •