Reputation: 13
I'm looking for a way to get this into PHP:
If the user is on "contact.php", then insert "..."
Right now, I have this:
<?php
if(stripos($_SERVER['REQUEST_URI'], '/contact.php'))
{echo '<li class="current">';}
else{echo '<li>';} ?>
However, for some reason this doesn't seem to work.
Also, I was wondering if there is a way to have it apply to multiple pages (in a directory) without using 'array', so basically saying:
If the user is on a file in directory A, then echo "...". If not, then echo "...".
EDIT: I figured out why it is not working, but I do not understand. Basically, I edited an HTML5 template, and replaced the common script of every page with a header.php and footer.php. However, the class="current" was in a different place on every seperate HTML page, which is now 1 common PHP header. The function in my header.php works when I link to the header like this:
<?php
require "header.php";
?>
But it does not work when I link to my header like this:
<?php
require "http://localhost/header.php";
?>
However, all the other information in my header.php is coming through.. I believe it's just this one function that stops working. Am I missing something really simple here? Please forgive me if that is the case...
Upvotes: 1
Views: 103
Reputation: 184
Your code might not work because stripos will return 0 if the string "/contact.php" is found at the beginning, which is then interpreted as false. You should check the result of stripos with the !== operator against false.
if(stripos($_SERVER['REQUEST_URI'], '/contact.php') !== false) { ... }
Regarding your other question: Just split the REQUEST_URI string into its parts and check if the penultimate element matches your directory. Although there might be a better approach in general to your problem.
EDIT: Based on Can Celik's comment I would suggest you use the parse_url() function [1] to decompose the entire request URI. Then you can access any part of the URL from the returned array and split the "path" part as I suggested before:
$parts = parse_url($_SERVER['REQUEST_URI']);
$segments = explode("/", $parts["path"]);
$parent_dir = ""
if(count($segments) > 1) {
$parent_dir = $segments[count($segments)-2]
}
if($parent_dir == "my_parent_dir") {
echo '<li class="current">';
} else {
echo '<li>';
}
[1] http://php.net/manual/en/function.parse-url.php
Upvotes: 1
Reputation: 825
stripos() should be hard-compared with FALSE to determine whether it really found the item or not.
Like arrays, the first character in a string is 0 (zero), not 1. If the position of the needle string is at the beginning of the haystack string, then stripos() will return 0 since 0 is the first position of the haystack. But if() will treat 0 as the same as false.
So you should be saying
if(stripos($_SERVER['REQUEST_URI'], '/contact.php') !== false)
http://php.net/manual/en/function.stripos.php
Upvotes: 1
Reputation: 2087
Try this:
if($_SERVER['REQUEST_URI'] == 'contact.php'){
echo '<li class="current">';
} else{
echo '<li>';
}
If your contact page will have any parameters than you can strip out the parameters.
$urlParts = explode('?', $_SERVER['REQUEST_URI'], 2);
$page = $urlParts[0]; // <-- This will give you just contact.php for your example.
Upvotes: 0