Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

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


Recommended Posts

Posted

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

Posted

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?

Posted

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?

Posted
Think i've got it sorted now. using the following php code.

 

 

 

 

$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';

}

Posted

Have a look at the 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.

 

if ($_SERVER['SERVER_NAME'] == 'mydomain.co.uk') {
echo "";
} else {
echo "";
}

 

This should be in the

section of the page.
Posted (edited)

Install Net_IPv4 via Pear (Net_IPv4), then whack the following somewhere before where you want the link... (eg: in Moodle's config.php)

 

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...

 

echo 'Linky Text';

 

 

(I think)

Edited by Marci

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...