我有错误"未定义索引:主机"当我试图在我的localhost中练习这段代码时

时间:2016-04-01 18:02:59

标签: php

我是php的新手。这是我的代码:

$domain = $_SERVER['HTTP_HOST'];
$uri = parse_url($_SERVER['HTTP_HOST']);
$r_domain = substr(['host'], strpos($uri['host'],"."), strlen($uri['host']));

if ( $domain == $r_domain ) {

/*Open the connection to our database use the info from the config file.*/
$link = f_sqlConnect('root', '', 'group_project');

1 个答案:

答案 0 :(得分:1)

parse_url旨在以这种形式解析字符串:

scheme://host/path?query#fragment

所以,如果你解析这个字符串:

https://www.example.com/pages/mypage.php?one=two#wow

parse_url的结果将是:

Array
(
    [scheme] => https
    [host] => www.example.com
    [path] => /pages/mypage.php
    [query] => one=two
    [fragment] => wow
)

但你解析$_SERVER['HTTP_HOST'],就像'localhost','www.mysite.com','127.0.0.1'。换句话说,一个字符串对parse_url没有特别的意义,因为没有任何元素可以将它与URL相关联。

解析(即)'localhost'将仅获得此:

Array
(
    [path] => localhost
)