PHP 如果 URL 等于 this 则执行操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9523140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP if URL equals this then perform action
提问by Miles Pfefferle
So I have a page title that is part of a Magento template; I'd like it to display 1 of 2 options, depending on what the URL is. If the URL is option 1, display headline 1. If the URL is anything else, display headline 2. This is what I came up with, but it's making my page crash:
所以我有一个页面标题,它是 Magento 模板的一部分;我希望它显示 2 个选项中的 1 个,具体取决于 URL 是什么。如果 URL 是选项 1,则显示标题 1。如果 URL 是其他内容,则显示标题 2。这是我想出的,但它使我的页面崩溃:
<div class="page-title">
<h1><?php
$host = parse_url($domain, PHP_URL_HOST);
if($host == 'http://domain.com/customer/account/create/?student=1') {
echo $this->__('Create an account if you are a Post Graduate Endodontic Resident and receive our resident pricing. Please fill in all required fields. Thank you!')
}
else
{
echo $this->__('Create an Account')
}
?></h1>
</div>
Anyone have any ideas?
谁有想法?
EDIT: So it should look like this?
编辑:所以它应该是这样的?
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'http://domain.com/customer/account/create/?student=1')
回答by Luke Shaheen
Are you looking for the URL that the page is currently on? You are using parse_urlthe wrong way; that is if you only want to get the host, or domain, i.e. only "dev.obtura.com". It looks like you want more than that. In addition, you are never setting the $domain
variable, so parse_url()
doesn't know what to do with it. So as it is now, your if
statement will always return 'Create an account`.
您是否正在查找该页面当前所在的 URL?您以错误的方式使用parse_url;也就是说,如果您只想获取主机或域,即仅“dev.obtura.com”。看起来你想要的不止这些。此外,您从不设置$domain
变量,因此parse_url()
不知道如何处理它。因此,就像现在一样,您的if
声明将始终返回“创建帐户”。
Instead, set $host
with $_SERVERvariables:
相反,$host
使用$_SERVER变量设置:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
You will also need to remove the "http://" from your checking - $host
will only contain everything after "http://"
您还需要从检查中删除“http://” -$host
将只包含“http://”之后的所有内容
As Aron Cederholmsuggested, you need to add semicolons (;
) to the end of your echo statements.
正如Aron Cederholm 所建议的,您需要;
在 echo 语句的末尾添加分号 ( )。
So, your PHP code should look like this:
所以,你的 PHP 代码应该是这样的:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'domain.com/customer/account/create/?student=1')
{
echo $this->__('Create an account if you are a Post Graduate Endodontic Resident and receive our resident pricing. Please fill in all required fields. Thank you!');
}
else
{
echo $this->__('Create an Account');
}
回答by Hyman
I'm not sure you're fetching the domain right. I don't really understand parse_url much, and you haven't shown us what $domain
is defined as.
我不确定您是否正确获取了域。我不太了解 parse_url,你也没有向我们展示什么$domain
是定义。
Normally if I want to get the domain name I would do this: $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
Then the rest of your code.
通常,如果我想获得域名,我会这样做:$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
然后是其余的代码。
That if, else statement seems legit to me, so I would try the above and see how that goes. ;)
if, else 语句对我来说似乎是合法的,所以我会尝试上面的方法,看看效果如何。;)
Edit: Oops, John beat me to it. :)
编辑:哎呀,约翰打败了我。:)
回答by Aron Cederholm
You should add semicolons to your statements inside your if-else.
您应该在 if-else 中的语句中添加分号。
if($host == 'http://dev.obtura.com/customer/account/create/?student=1') {
echo $this->__('Create an account if you are a Post Graduate Endodontic Resident and receive our resident pricing. Please fill in all required fields. Thank you!');
}
else
{
echo $this->__('Create an Account');
}