繁体   English   中英

使用文本文件确定子域包括php

[英]using a text file to determine subdomain includes php

好吧,我有2个文件-具有index和.if语句的文件如下:

    $sub = array_shift(explode(".",$_SERVER['HTTP_HOST']));
    if ($sub == 'localhost') include 'home.php';
    if ($sub == 'whateversubdomain') include 'correspondingphpfile .php';

我还有一个文本文件,其中包含:

subdomain = subdomain.php
nextsub = nextsub.php
.... and so on

问题是我该怎么做,以便当我在文本文件中添加新行时说nextsub,然后有人访问nextsub.sitename.com,他们将被定向到正确的php文件。

我正在考虑打开文本文件并在index.php文件中创建一个变量,然后说$ sub == $ newVar是否包含$ subName。 .php。

这可能吗-像-

//open file
$fp = @fopen ($some_file, "r");

if ($fp) {
     //for each line in file
     while(!feof($fp)) {
          //push lines into array
          $this_line = fgets($fp);                 
          array_push($some_array,$this_line);
     }
     //close file
     fclose($fp);
}

(您必须手动处理不存在的)

例如,目录“子域”:

$sub = array_shift(explode(".",$_SERVER['HTTP_HOST']));
include 'subdomains/'.basename($sub).'.php';

我不知道,额外写入文件有什么意义?

如果您坚持要使用不具有易于解析名称方案的页面的子域,那么最好的选择是:

// Create an array that maps subdomains to pages.
$sub_page_map = array();
$sub_page_map['localhost'] = 'home.php';
$sub_page_map['whateversubdomain'] = 'correspondingphpfile.php';

// Get the subdomain.  
$sub = array_shift(explode(".",$_SERVER['HTTP_HOST']));

// If the `$sub` exists in `$sub_page_map` then include the corresponding file.
if (array_key_exists($sub, $sub_page_map)) {
  include $sub_page_map[$sub];
}
else {
  include 'default.php';
}

如果要将其存储在外部文本文件中,只需修改文件解析代码以在$sub_page_map生成值$sub_page_map 但是对我来说,您设置中的最大缺陷是缺少默认页面,这就是为什么我包括了else加载建议的`default.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM