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

mil*_*les 12 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>
Run Code Online (Sandbox Code Playgroud)

有人有主意吗?

编辑:那应该是这样的?

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

Luk*_*een 23

您在寻找该页面当前所在的URL吗?你正在以错误的方式使用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');
}
Run Code Online (Sandbox Code Playgroud)


Jac*_*ack 5

我不确定您是否正确获取了域。我不太了解 parse_url,你也没有向我们展示什么$domain是定义。

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

if, else 语句对我来说似乎是合法的,所以我会尝试上面的方法,看看效果如何。;)

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


Aro*_*olm 5

您应该在 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');
}
Run Code Online (Sandbox Code Playgroud)