dpqmu84646 2016-08-31 07:51
浏览 74

如何通过php代码获取URL中的本地IP

I need a php related help for moodle config file. I install xampp on usb flash drive, then I install a moodle version in it. Now I wish to run this in my lan with dynamic ip address, like I put my flash drive in any system of my lan and start xampp server then it automatically take that system's ip address in url. This is my config file:

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype    = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'exam';
$CFG->dbuser    = 'root';
$CFG->dbpass    = '';
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
);
$CFG->wwwroot   = "http://localhost/examination";
$CFG->dataroot  = 'F:\\xampp\\moodledata';
$CFG->admin     = 'admin';

$CFG->directorypermissions = 0777;

require_once(dirname(__FILE__) . '/lib/setup.php');

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

Now in line "$CFG->wwwroot = "http://localhost/examination";" I wish to put ip address dynamically in place of localhost and for this I found a function:

// function to get ip addresss
function getLocalIP(){
exec("ipconfig /all", $output);
foreach($output as $line){
if (preg_match("/(.*)IPv4 Address(.*)/", $line)){
$ip = $line;
$ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip);
$ip = str_replace("(Preferred)","",$ip);
}
}
return $ip;
}
//echo $ip = getLocalIP();

But when I put above function in line like $CFG->wwwroot = "http://$ip/examination"; it wont work. Can anyone help me in this??

  • 写回答

1条回答 默认 最新

  • douyabu1528 2016-08-31 08:13
    关注

    You should call function name getLocalIP() instead of $ip .

    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)