I have a statement that checks the page's url and marks up a page accordingly, but it only works when my if statement has one option to check for.
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? } ?>
If I modify it to check for two possible urls...
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? }else if (strpos($url, 'events/othereventname')!= false) { ?>
~markup~
<? } ?>
... the page won't load. I must be missing something obvious- can someone tell me what is wrong with this function?
*edit: Since it was requested I have included the $url variable and more specific url examples
strpos returns 0 when search substring is in the beginning of the query string. You can replace != to !== to make it work - otherwise php is internally transforming false to zero, which leads to incorrect comparison result.
For example:
<?php
var_dump(strpos('aaa', 'a'));
echo var_dump(strpos('aaa', 'a') === false);
echo var_dump(strpos('aaa', 'a') == false);
Try to use !== comparison just just in case string is at position 0.
Another syntax problem is else if, while you should use elseif.
Try also changing short php tag <? to full one <?php.
Rather than using the strpos() you can get the request uri which is anything after the domain name (ie: www.example.com/foo/bar would give you /foo/bar).
$url = $_SERVER['REQUEST_URI'];
if($url == "/foo/bar") {
// markup
} elseif($url == "/bar/foo") {
// markup
} else {
// markup
}
Related
I have two type of links which are strings taken from database:
http://www.website.com/anything-else.html
www.website.com/anything-else.html
I need ALL links to be displayed with http:// no matter what so Im using this simple code to determine whether link has http in it and if not add it:
if (strpos($links, 'http') !== true) {
$linkai = 'http://'.$links;
}
The problem is, it is adding http:// to any link no matter if it has it or not.
I tried ==false ect. Nothing works. Any ideas?
Try this
if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}
In strpos documentation says return value not Boolean always.
"Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."
$arrParsedUrl = parse_url($links);
if (!empty($arrParsedUrl['scheme']))
{
// Contains http:// schema
if ($arrParsedUrl['scheme'] === "http")
{
}
// Contains https:// schema
else if ($arrParsedUrl['scheme'] === "https")
{
}
}
// Don't contains http:// or https://
else
{
$links = 'http://'.$links;
}
echo $links;
Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
I'm trying to compare value of HTTP_REFERER and my base url . How to do that? If I write it in this way, it doesn't show back button. If I use whole url of my project:
http://localhost/myproject/index.php/home/index
It works, but I want to compare base url - not to write many if conditions for each page. How could I do that?
<?php
if ((isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) )) {
if ($_SERVER['HTTP_REFERER'] == 'http://localhost:/myproject/') {
echo '<a type="button" onclick="history.back(-1);">Back</a>';
}
}
Edited: In this way it'working but it's showing this warning:
Message: strtolower() expects parameter 1 to be string, array given
How to fix it?
<?php
if ((isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) )) { $referer = $_SERVER['HTTP_REFERER']; $current = 'localhost:/myproject/';
$ref =parse_url($referer); $my=parse_url($current);
if (strtolower($ref) === strtolower($my)) { echo '<a type="button" onclick="history.back(-1);">Back</a>'; } }
check if in HTTP_REFERER its constains your domain.
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'localhost/myproject' !== false))
{
echo '<a type="button" onclick="history.back(-1);">Back</a>';
}
Try the following:
if ((isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) )) {
$referer = $_SERVER['HTTP_REFERER'];
$current = 'http://localhost:/myproject/'; // Do you mean for the : to be here?
$refererBaseUrl = trim(preg_replace('/\?.*/', '', $referer), '/');
$currentBaseUrl = trim(preg_replace('/\?.*/', '', $current), '/');
if (strtolower($refererBaseUrl) === strtolower($currentBaseUrl)) {
echo '<a type="button" onclick="history.back(-1);">Back</a>';
}
}
This is the basic technique that I use to compare base URLs.
Edit:
How about using parse_url (http://php.net/manual/en/function.parse-url.php) to parse both URLs and compare results?
You can get the base URL using $_SERVER['HTTP_HOST'] you may need to append http/https to the string.
You can also then use $_SERVER['REQUEST_URI'] to get the remainder of the URL.
I have page and I don't show a specific block to a specific urls.
So my urls are www.example.com/products/product1.html, www.example.com/products/product2.html etc..
So what I want to do. I want to find all the urls that starts with www.example.com/products/ and in those urls, exclude the block.
So far my code for one url is:
<?php
$a = "www.example.com/products/product2";
$p = curPageURL(); ?>
<?php
if($a == $p ){
Dont show the block
?>
But I have 100 urls that I don't show the block.
Is there any change to do for all the urls without write 200 lines of code?
Use PHP's strpos
This function will check if the specified string exists in the URL and if it exists, do not show the block.
$findme = 'www.example.com/products/';
$pos = strpos($mystring, $findme);
if ($pos === FALSE) {
// SHOW BLOCK
}
Try with strpos()
$cururl= "http://.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$pos = strpos($cururl, '/products/');
if ($pos !== FALSE) {
// products found do your stuff
}
I think you can do this with the in_array()-function like this:
$blockedUrlArray[] = "www.example.com/products/product2";
$blockedUrlArray[] = "www.example.com/products/product3";
.
.
.
$searchUrl = curPageURL();
//if the current Url is not in blocked Urls
if(!in_array($searchUrl, $blockedUrlArray){
//do something
}
//if the url is a blocked url
else{
//do something
}
Basically I am coding a script where it simply redirects the user to the destination page. And I want to be able to check if multiple websites are not equal to the value; if this is so, it will run a error, else it will proceed.
I can't seem to get this to work though, although I am sure there's a way to check multiple values.
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co", "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>
You can make an array of items to check for and then check if the url is in the array:
if (!in_array($url, array("***.co", "***.net")))
{
}
You can also use multiple conditions like #wrigby showed, but the solution using an array makes it easier to add more (or a dynamic number of) urls. But if there are always two, his is better.
You'll need two complete conditionals, connected with a logical and (&&) operator:
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co" && $url != "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>