PHP如果URL等于此,则执行操作 [英] PHP if URL equals this then perform action

查看:175
本文介绍了PHP如果URL等于此,则执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个页面标题,是Magento模板的一部分;我希望它显示2个选项中的1个,具体取决于URL。如果URL是选项1,则显示标题1.如果URL是其他任何内容,则显示标题2.这是我提出的,但它使我的页面崩溃:

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:

<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>

任何人都有任何想法?

编辑:所以看起来应该是这样的?

So it should look like this?

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


推荐答案

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

Are you looking for the URL that the page is currently on? You are using parse_url the 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`.

相反,设置<使用 $ _SERVER $ host / a>变量:

Instead, set $host with $_SERVER variables:

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

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

You will also need to remove the "http://" from your checking - $host will only contain everything after "http://"

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

As Aron Cederholm suggested, you need to add semicolons (;) to the end of your echo statements.

因此,您的PHP代码应如下所示:

So, your PHP code should look like this:

$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');
}

这篇关于PHP如果URL等于此,则执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆