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

时间:2013-12-04 01:20:16

标签: php subdomain

好的我有2个文件 - index.php有if和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);
}

2 个答案:

答案 0 :(得分:0)

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

例如,目录“subdomains”:

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

我不知道,附加写入文件有什么意义。

答案 1 :(得分:0)

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

// 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中的值。但对我而言,您的设置中最大的缺陷是缺少默认页面,这就是为什么我添加else来加载建议的`default.php