Forum Moderators: coopster

Message Too Old, No Replies

Check for current URI - and include if NOT matched

URL checking with PHP

         

espmartin

9:36 pm on Nov 12, 2007 (gmt 0)

10+ Year Member



Hello,

I've been using this bit of PHP code within my "sidebar.php" file to insert
menu items (external links) depending on what URI is viewed, and and it
works great so far:

<?php
$homepage = "/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
include('homepage-only-stuff.php');
}
?>
<?php
$antivirus = "/anti-virus/";
$antivirussub = "/anti-virus/anti-virus-articles.php";
$currentpage = $_SERVER['REQUEST_URI'];
if(($antivirus==$currentpage) ¦¦ ($antivirussub==$currentpage)) {
include('antivirus-only-stuff.php');
}
?>
ETC... (you get the picture :D )

But now I want to exclude my blog pages. I can if I use:

 <?php
$homepage = "/weblog/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage!=$currentpage) {
include('non-blog-stuff.php');
}
?>
But that does not match all the specific post pages, nor do I want to
hard-code every post URI I make anyway :eek:

So, how can I check for /weblog/AnyPostPage-URI/ (as a "wildcard" kind of
match?) and exclude them?

PHP_Chimp

11:38 pm on Nov 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



could you use regular expressions to search for the pages you want included?
As if you are using naming like
$antivirus = "/anti-virus/";
$antivirussub = "/anti-virus/anti-virus-articles.php";
Then you should be able to use preg_replace to sort out what files should be attached.

$request = '%^/(.*)/.*%';
$attach = '/$1/$1-articles.php';
$input = $_SERVER['REQUEST_URL'];
preg_replace [uk2.php.net]($request, $attach, $input);