PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
SFADuncan
Forum Newbie
Posts: 23 Joined: Mon Aug 11, 2003 5:06 pm
Location: uk
Post
by SFADuncan » Tue Jun 08, 2004 6:27 am
How do I take the URL of a current page and strip it into different parts
I'm trying to isolate the directory path of the url of a current page
eg
http://www.domain.co.uk/directory
I wish to isolate "directory" and make it equal a variable.
Simon
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Tue Jun 08, 2004 6:40 am
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jun 08, 2004 6:41 am
[php_man]parse_url[/php_man]()
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jun 08, 2004 6:41 am
phooey!
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Tue Jun 08, 2004 6:45 am
Beat yah! by at least.. ohhh.. 40 seconds
SFADuncan
Forum Newbie
Posts: 23 Joined: Mon Aug 11, 2003 5:06 pm
Location: uk
Post
by SFADuncan » Tue Jun 08, 2004 7:49 am
Hi thanks for these links... I had seen them... but was put off by the very first comment in the User Contributed Notes
"Its all very well being able to split and reconstruct a url. BUT how on earth do you get the FULL url of the current page."
since it is exactly this which I am trying to do.... but I will ignore this comment and attempt to use what is listed on this page
SFADuncan
Forum Newbie
Posts: 23 Joined: Mon Aug 11, 2003 5:06 pm
Location: uk
Post
by SFADuncan » Tue Jun 08, 2004 8:21 am
No luck... all the scripts I see involve typing in the URL (within the script) ... I want to use the URL of the current page
SFADuncan
Forum Newbie
Posts: 23 Joined: Mon Aug 11, 2003 5:06 pm
Location: uk
Post
by SFADuncan » Tue Jun 08, 2004 8:51 am
This script almost works:
<?php
echo "Current directory is " . getcwd();
?>
Except that I get:
Current directory is /home/virtual/site214/fst/var/www/html/test
when all I want is:
Current directory is test
Any ideas?
Simon
Grim...
DevNet Resident
Posts: 1445 Joined: Tue May 18, 2004 5:32 am
Location: London, UK
Post
by Grim... » Tue Jun 08, 2004 9:17 am
Ugly but quick:
Code: Select all
$url = getcwd();
$array = explode ("/", $url);
$arrayB = array_reverse($array);
$folder = $arrayB[0];
jason
Site Admin
Posts: 1767 Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:
Post
by jason » Tue Jun 08, 2004 9:41 am
Okay, how is the cwd the same thing as trying to get the URL and parsing that? I am sorry, but if you are going to ask the question, ask the right question.
What are you trying to accomplish? Don't tell me what you think the solution is, tell me what you want.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Jun 08, 2004 9:42 am
Look at the $_SERVER variables. There's one in there about the script_uri.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jason
Site Admin
Posts: 1767 Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:
Post
by jason » Tue Jun 08, 2004 9:43 am
Grim...
DevNet Resident
Posts: 1445 Joined: Tue May 18, 2004 5:32 am
Location: London, UK
Post
by Grim... » Tue Jun 08, 2004 9:58 am
jason wrote: I am sorry, but if you are going to ask the question, ask the right question.
To be fair, he did ask the right question.
He said:
SFADuncan wrote: I'm trying to isolate the directory path of the url of a current page
getcwd() is one way of achieving this.
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Tue Jun 08, 2004 10:31 am
You could try...
Code: Select all
<?php
$pwd = pathinfo(getcwd());
$cwd = $pwd['basename'];
echo $cwd;
?>
Although this will probably give undesireable results if run in your www root.
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Tue Jun 08, 2004 11:51 am
What the hell? Why mess with cwd? To get the "full" URL of the current page on your site, just combine two values:
$_SERVER['HTTP_HOST'] and either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] if you need all the query string values.
Merge the two and kick the result to parse_url.