PHP如果URL等于此,则执行操作

时间:2012-03-01 20:01:31

标签: php if-statement

所以我有一个页面标题,是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>

有人有什么想法吗?

编辑:那应该是这样的吗?

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'http://domain.com/customer/account/create/?student=1')

3 个答案:

答案 0 :(得分:22)

您是否正在寻找该网页目前所在的网址?你正在以错误的方式使用parse_url;也就是说,如果你只想获得主机或域名,即只有“dev.obtura.com”。看起来你想要更多。此外,您永远不会设置$domain变量,因此parse_url()不知道如何处理它。就像现在一样,您的if语句将始终返回“创建帐户”。

相反,请将$host设置为$_SERVER个变量:

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

您还需要从检查中删除“http://” - $host只包含“http://”之后的所有内容

建议Aron Cederholm,您需要在echo语句的末尾添加分号(;)。

因此,您的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');
}

答案 1 :(得分:4)

我不确定你是否正确提取域名。我并不太了解parse_url,你没有向我们展示$domain被定义为什么。

通常,如果我想获取域名,我会这样做:$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']然后是其余的代码。

如果,其他声明对我来说似乎是合法的,那么我会尝试以上看看它是如何发生的。 ;)

编辑:哎呀,约翰打败了我。 :)

答案 2 :(得分:4)

您应该在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');
}
相关问题