How to use multiple URLs in PHP REQUEST_URI statement? - php

Here is my code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">HANDHELD<br />THERMAL IMAGING</div>';
}
else if($_SERVER['REQUEST_URI'] == '/shop/category/mobile-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">MOBILE IMAGING</div>';
}
Basically this code displays a different left side site navigation depending on which page you're on in the site. It detects what page you're on by the URL of the page by using the PHP REQUEST_URI feature.
My question is this, how can I make the code detect multiple URLs for the same navigation piece?
I tried this code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/' , '/shop/iphone-thermal-imaging-adapter/')
{
But that code doesn't seem to work. Basically all I'm trying to figure out here is how I can use multiple URLs for the REQUEST_URI 'if' statement. Thanks in advance!

Put the URLs in an array and then...
if(in_array($_SERVER['REQUEST_URI'],$array))

You should... switch to a switch statement, which is both neater and offers this option:
switch($_SERVER['REQUEST_URI']) {
case '/shop/category/handheld-thermal-imaging/':
// THERE IS NO CODE AT ALL HERE!
// The absence of a "break;" causes control to "fall through" to the next
case '/shop/iphone-thermal-imaging-adapter/':
echo "something common for the thermal imaging urls";
break;
case 'some other url':
echo "something for the url that stands alone";
break;
}

You can use an "OR" statement in your conditional:
if($_SERVER['REQUEST_URI'] == "whatever" || $_SERVER['REQUEST_URI'] == 'something else")
{
}

I used the OR statement for multiple URL checking
<?php
if ((strpos($_SERVER['REQUEST_URI'], 'contact.php') || strpos($_SERVER['REQUEST_URI'], 'register.php') || strpos($_SERVER['REQUEST_URI'], 'login.php')) === false) {
echo "show";
} else {
echo "hide";
}
?>

Related

PHP Redirect Not Working When In A Nested IF Statement

I've looked at several of the "PHP redirect not working ......" posts, but I haven't had any luck figuring out what is going on with my problem.
The code below always goes to teacherActivity_2.php even though the 2nd if passes and should redirect to teacherActivity_3_shadowT.php. If I comment out the teacherActivity_2.php, the page redirects (as expected) to teacherActivity_3_shadowT.php. I think I'm missing some obscure rule that I should know but don't. Any ideas?
Why does this redirect to teacherActivity_2.php instead of teacherActivity_3_shadowT.php?
if($test == 'yes') {
if($_SESSION['wblReporting']['activity'] == 'shadowT'){
header('Location: /app/do_cte-wbl/forms/teacherActivity_3_shadowT.php');
}
header('Location: /app/do_cte-wbl/forms/teacherActivity_2.php');
}
?>
Try the following:
if($test == 'yes') {
if($_SESSION['wblReporting']['activity'] == 'shadowT'){
header('Location: /app/do_cte-wbl/forms/teacherActivity_3_shadowT.php');
die();
} else {
header('Location: /app/do_cte-wbl/forms/teacherActivity_2.php');
die();
}
}
This worked no matter how many elseifs I used.

Display text if statement matches

I am a beginner when it comes to php, and I have encountered a problem that I cannot find the solution for. I have tried searching for a relevant answer but I haven't found one.
I have the following code in my index.php:
<?php if ($set_status = 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
and this a bit further down:
<?php include 'scan/a.php'; ?>
And inside my a.php I have the following code:
<?php
$a = file_get_contents
('http://www.a-random-website/text.html', NULL, NULL, 2, 1);
if ($a == "0") {
include 'fail.php';
} elseif ($a == "1") {
include 'success.php';
} else {
echo 'Offline';
}
?>
And inside my fail.php I have the following code:
<?php
$set_status = 2;
echo 'Failed';
?>
So the idea here is that "a.php" will fetch a number from the website (The correct website has either ["1"] or ["0"] displayed that the code will fetch).
Depending on the result it returns, a.php will include either "fail.php" or "success.php", each containing either a success or a fail-message. If file_get_contents return a 0 I also want fail.php to $set_status = 2; which will cause "There is one or more errors" to be displayed on the front-page (index.php).
The reason that I am using include is that there's going to be a "b.php" and "c.php" and "d.php" and so on, all doing the same thing but fetching data from different pages. I want the success or fail-message to remain easy to edit, without having to edit each and every new x.php file.
So here's where it gets problematic. Everything works beautifully, except for the "There is one or more errors"-message that's supposed to trigger if ($set_status = 2).
I can get as far as the message showing, but when I switch the 1 and 0 in "a.php" (To simulate a specific result) the message will still show. I can't seem to figure it out.
So my question is: What have I done wrong, and what is the correct way to do it?
Thank you in advance!
Best regards,
Marc
use == operator in index.php to compare
<?php if ($set_status == 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
<?php if ($set_status ==2) {
echo 'There\'s one or more errors';
} else {
echo '';
}
?>
Just change this code ... see slash\ and == in the echo line .. else of your code id fine

PHP conditional statement is not working

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
}

Determine Logo link based on URL using PHP

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>';
}
?>

Matching the url based on the page the user is on

I'm trying to right a simple if statement to basically say that if the user is on page_A then echo "i am on page A"; and if I'm on page_B then echo "i am on the page B"; and I'm using the PHP function $_SERVER['REQUEST_URI']; to check if they match what is in my string that is stored in the variable $page_A and $page_B.
my code is as follows:
<?php
$page_A = "/test.co.za/voice/";
$page_B = "/test.co.za/artistes/";
$match = $_SERVER['REQUEST_URI'];
if ( $page_A == $match ) {
echo "i am on page A";
}elseif ( $page_B == $match ) {
echo "i am on the page B";
}
?>
Where am i going wrong or is there another way i can achieve getting this to work?
Thanks team :)
if(strpos($_SERVER['REQUEST_URI'],'voice')!==false){
echo "i am on page A";
}elseif(strpos($_SERVER['REQUEST_URI'],'artistes')!==false){
echo "i am on the page B";
}
I don't quite understand how you are doing this, if you have 2 different pages you always know at which page you are.
If you are using htaccess to redirect the request to a unique file, you should do something like this:
Add this line in htaccess:
RewriteRule ^([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?$ index.php?p=$1&s=$2&d=$3&ID=$4&%{QUERY_STRING}
Now, if you open for instances: http://test.co.za/voice/ this will be redirected to /index.php.
At index.php you can read:
$page = $_REQUEST['p'];
if ($page == 'voice') { //do stuff }

Categories