Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if URL equals this then perform action

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')
like image 365
Miles Pfefferle Avatar asked Mar 01 '12 20:03

Miles Pfefferle


People also ask

How do you check if the URL contains a given string in PHP?

Method 1: strpos() Function: The strpos() function is used to find the first occurrence of a sub string in a string. If sub string exists then the function returns the starting index of the sub string else returns False if the sub string is not found in the string (URL).

Can I use or in if in PHP?

PHP If condition can be compound condition. So, we can join multiple simple conditions with logical OR operator and use it as condition for PHP If statement.

How can I get URL in PHP?

The necessary superglobal variables such as $_SERVER['HTTPS'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PORT'] are used to get full URL in PHP. The variable HTTPS can easily retrieve the protocol in the URL of a webpage. If it returns a value “on”, then the protocol is HTTPS.

What does URL mean in PHP?

Definition of PHP URL. Generally, the URL means Uniform Resource Locater. Likewise, URL in PHP Programming Language is also the same. URL is nothing but a website address. It helps in connecting the client and the server when browsed using a specific link.


2 Answers

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');
}
like image 167
Luke Shaheen Avatar answered Oct 15 '22 23:10

Luke Shaheen


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. :)

like image 37
Jack Avatar answered Oct 16 '22 01:10

Jack