Reading the current dir from the URL
Moderator: General Moderators
Reading the current dir from the URL
Hi everyone,
I am creating a site and I want to use the current directory as a variable for setting page color and a bunch of other things.
How would I extract dir1 from http://www.mywebsite.nl/dir1/pagename.php?
I have done some reading at php.net and I understand there are two steps to do this.
1. Get the actual URL
2. Read the right piece from that URL
I assume PHP needs to know the basedir of my site on the server before it can extract dir1?
Could you help me do this? I am a bit confused by all the URL and dir functions PHP offers.
I am creating a site and I want to use the current directory as a variable for setting page color and a bunch of other things.
How would I extract dir1 from http://www.mywebsite.nl/dir1/pagename.php?
I have done some reading at php.net and I understand there are two steps to do this.
1. Get the actual URL
2. Read the right piece from that URL
I assume PHP needs to know the basedir of my site on the server before it can extract dir1?
Could you help me do this? I am a bit confused by all the URL and dir functions PHP offers.
-
Breckenridge
- Forum Commoner
- Posts: 62
- Joined: Thu Sep 09, 2004 11:10 pm
- Location: Breckenridge, Colorado
Here is a url that should help you understand $_SERVER[xyz] variables. Hope it helps
http://us2.php.net/reserved.variables
http://us2.php.net/reserved.variables
Thanks Breckenridge. I figured it out pretty quickly.
I will now match $directory against a predefined list of directory names in an if else structure. Do you think this is a good way to go if I want to set the page color, a link menu and some graphics based on the directory name?
Code: Select all
<?php
$path = pathinfo($_SERVER['PHP_SELF']);
$directory=$path["dirname"];
echo "$directory";
?>Thanks all,
I've got it figured out. Perhaps others that find this thread will benefit from this piece of code:
(Note I am not a superduper programmer. This code might not be maximum efficient or logical, but it's serves my purposes)
I've got it figured out. Perhaps others that find this thread will benefit from this piece of code:
(Note I am not a superduper programmer. This code might not be maximum efficient or logical, but it's serves my purposes)
Code: Select all
// get the current directory name
$path = pathinfo($_SERVER['PHP_SELF']);
$directory=$path["dirname"];
// create an array of categories (dirs) and things related to them (top_color)
$multi_array = array(
array(1, cat1, top_yellow),
array(2, cat2, top_green),
array(3, cat3, top_blue),
array(4, cat4, top_purple),
array(5, cat5, top_darkblue),
array(6, cat6, top_orange),
);
// Match every cat from $multi_array against the current dirname
for ($i = 0; $i < sizeof($multi_array); $i++)
{
if(ereg($multi_array[$i][1],$directory))
{
//if they match carry out actions specific to the current directory
$name_of_top_image = "{$multi_array["$i"][2]}";
}
}Strange, I receive a parse error (Parse error: parse error, unexpected T_STRING, expecting ')' in ....) when I try to add hex color codes to the array.
This works:
But with a second color added it returns the parse error:
How is that possible?? The parse error points to line 4, the one with 47B809 in it.
This works:
Code: Select all
$multi_array = array(
array(1, hh, top_geel, F2C213),
array(2, decanen, top_groen, ggg),
array(3, werkgevers, top_geel, ggg),
array(4, opleidingen, top_geel, ggg),
array(5, studeren, top_geel, ggg),
array(6, den_haag, top_geel, ggg),
);Code: Select all
$multi_array = array(
array(1, hh, top_geel, F2C213),
array(2, decanen, top_groen, 47B809),
array(3, werkgevers, top_geel, ggg),
array(4, opleidingen, top_geel, ggg),
array(5, studeren, top_geel, ggg),
array(6, den_haag, top_geel, ggg),
);- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK