15

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>

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')
Miles Pfefferle
  • 1,187
  • 6
  • 20
  • 36

3 Answers3

26

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

Instead, set $host with $_SERVER variables:

$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://"

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

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');
}
Community
  • 1
  • 1
Luke Shaheen
  • 4,262
  • 12
  • 52
  • 82
5

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.

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.

That if, else statement seems legit to me, so I would try the above and see how that goes. ;)

Edit: Oops, John beat me to it. :)

Jack
  • 740
  • 5
  • 21
5

You should add semicolons to your statements inside your 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');
}
Xyz
  • 5,955
  • 5
  • 40
  • 58
  • The page is no longer crashing, but the statement isn't working. I'm still getting the second 'Create an Account' headline – Miles Pfefferle Mar 01 '12 at 20:39
  • 1
    Try echoing $host to see for your self why it does not equal to your desired url. – Xyz Mar 01 '12 at 20:42
  • @miles Did you try setting $host as I suggested? – Luke Shaheen Mar 01 '12 at 20:42
  • Like this: if ($host == '...') { echo 'Success: ', $host, PHP_EOL; echo $this->__('...'); } else { echo 'Fail: ', $host, PHP_EOL; echo $this->__('Create an Account'); } – Xyz Mar 01 '12 at 20:44
  • Also, try with setting $host in the way @john and Jack BeePee described. – Xyz Mar 01 '12 at 20:45