简体   繁体   English

PHP检查对IP地址敏感的子域

[英]PHP Check subdomain sensitive to IP address

I'm having some issue with something that seemed simple to me at first, but is now proving very difficult. 一开始我觉得有些简单的问题,但现在证明非常困难。 Maybe I'm over thinking it - would love your help. 也许我想不通-希望您的帮助。

Overview: I have a web application with two interfaces (1 for clients and 1 for customers ). 概述:我有一个带有两个界面的Web应用程序(1个用于客户端 ,1个用于客户 )。 All customers get routed one way and all clients get routed another. 所有客户都以一种方式被路由,所有客户都以另一种方式被路由。 I determine which login screen to show based on their subdomain. 我根据其子域确定要显示的登录屏幕。 customers get the base domain (+www) and clients go to [ clientName ].example.com 客户获得基本域(+ www), 客户转到[ clientName ] .example.com

Problem: I was determining this before by doing string manipulations on env("HTTP_HOST") -- see code below. 问题:我之前是通过对env(“ HTTP_HOST”)进行字符串操作来确定的-请参见下面的代码。 However, this now poses an issue when using local IPs (for testing with other devices/ people). 但是,这现在在使用本地IP(用于与其他设备/人员进行测试)时引起问题。 My code, which looks for '.' 我的代码,查找“。”。 in the host environment fails because IPs have 3 dots and ' localhost ' has 0 在主机环境中失败,因为IP有3个点,而' localhost '有0

These are the different naming parameters I've established and how I'd like the route... 这些是我建立的不同的命名参数,以及我想要的路线...

CUSTOMER ----------------------------- CLIENT
                xyz.example.com ------>
        <------ www.example.com
        <------ example.com

                xyz.localhost   ------>
        <------ www.localhost
        <------ localhost

                xyz.192.168.X.X ------>
        <------ www.192.168.X.X
        <------ 192.168.X.X

I could be really over thinking this and am hoping there's some simple Php function like "get_subdomain()" that will do this for me. 我可能真的想得太过分了,希望有一些简单的Php函数,例如“ get_subdomain()”会为我完成此任务。 But I'm not seeing it and would really love some help. 但是我没有看到它,真的会喜欢一些帮助。

Thanks! 谢谢!

Current Code: 当前代码:

$host = env("HTTP_HOST");
$group = null;

// if there is exactly one dot IE example[dot]com then there is no subdomain
if(substr_count($host,'.')==1){
    $group = 'customer'
}else{ // there is a subdomain
    $subdomain = substr($host, 0, strpos($host, "."));
    if($subdomain=='www'){
        $group = 'customer'
    }else{
        $group = 'client'
    }
}

CakePhp if that helps? CakePhp是否有帮助?

I hope this answer does not seem to avoid your specific question, but it seems to me that you are inviting alternatives in your question. 我希望这个答案似乎不会避免您的特定问题,但在我看来,您正在邀请其他人提出问题。 I do think you are over-thinking the solution, especially since it is primarily to assist you in your test environment. 我确实认为您在考虑解决方案,特别是因为它主要是为了在您的测试环境中提供帮助。

What I suggest is that you define your own local domains for testing instead of using IP Addresses. 我建议您定义自己的本地域进行测试,而不要使用IP地址。 So for example you could (in your hosts file or equivalent) define: 因此,例如,您可以(在您的主机文件或等效文件中)定义:

xyzclient.mylocaldomain  192.168.1.1 [or whatever your local ip is]
www.mylocaldomain        192.168.1.1 
xyz.localhost            192.168.1.1
www.localhost            192.168.1.1

Then you don't need to code any differently for local or live environments. 这样,您就无需为本地或实时环境进行任何不同的编码。 Just in case: this guide to setting up and configuring local domains may be useful. 以防万一: 本指南来设置和配置本地域名可能是有用的。

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

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