Reputation: 303
I have a multisite based on wordpress. The sites use the same theme and are located under the same domain. And I want them to display some code depending on with site it is.
So I have something like this
<?php
$currentUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ($currentUrl == 'http://example.com/institution/') {
do something
} else {
do something different
}
?>
now this works when ever I'm on http://example.com/institution/
or http://example.com/institution2/
(and sub pages of institution2 http://example.com/institution2/contact
) but when I go to sub pages of institution for example: http://example.com/institution/gallery
the code isn't working. What am I missing here ?
Upvotes: 1
Views: 943
Reputation: 69
<?php
$currentPath = $_SERVER['SCRIPT_NAME'];
$currentPath = explode("/",$currentPath);
if ($currentPath[1] == 'institution') {
do something
} else {
do something different
}
?>
Upvotes: 0
Reputation: 6592
As arkascha already mentioned in his comment you are checking for equality of the provided url and the expected url. What you really want is to check whether the (from the user) provided url starts with your expected url.
You can achieve this by using the stripos function.
stripos — Find the position of the first occurrence of a case-insensitive substring in a string
int stripos ( string $haystack , mixed $needle [, int $offset = 0 ] )
haystack The string to search in.
needle Note that the needle may be a string of one or more characters. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
offset If specified, search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string.
Returns the position of where the needle exists relative to the beginnning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found.
So you can change your code to
if (stripos($currentUrl, '//example.com/institution') >= 0)
{
...
}
else
{
...
}
or with inverse logic (check if it is NOT)
if (stripos($currentUrl, '//example.com/institution') == FALSE)
{
...
}
else
{
...
}
Upvotes: 2