TheFilmGod Posted August 30, 2007 Share Posted August 30, 2007 How can I retrive the full url of the current page and cut everything off but the end of it, like so: http://blah.website.com/news/index.php INTO --> news or http://www.website.com/index.php INTO --> index Please help! I'm a noob. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 30, 2007 Share Posted August 30, 2007 try using .htaccess to modify your pages. Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted August 30, 2007 Author Share Posted August 30, 2007 try using .htaccess to modify your pages. no thank you. .htaccess shouldn't be used for a simple php script for a various of reasons. I know you can use $_SERVER['something'] and then somehow cut out that link into just what I want. Anyone else!? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 Use a combination of the following: $_SERVER['REQUEST_URI']; substr(); strpos(); See if you can get anything with that, and if not we can help more. Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted August 30, 2007 Author Share Posted August 30, 2007 Use a combination of the following: $_SERVER['REQUEST_URI']; substr(); strpos(); See if you can get anything with that, and if not we can help more. That looks propmising. I'll try it. Thanks! You'll hear from me very soon! Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted August 31, 2007 Author Share Posted August 31, 2007 what does $_SERVER['REQUEST_URI']; do? According to the php manual it ges the url of the requested page, i.e. index.php . But I don't want the "requested page" I want the whole url and cut it short! I'm looking for the name of the first directory. So, website.com/news/index/hi/hello.php = news So I want the text after .com/ and all the way to the next "/". Sounds simple but its giving me a headache! Help!!! And wouldn't the use of substr(); be enough without the strpos();? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 Did you try printing out what it gets? How would you tell substr() where to stop without strpos()? website.com/folder/blah.php is a different length than website.com/news/blah.php. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 use the basename function http://www.php.net/manual/en/function.basename.php ;D ;D Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 That is for files, not for URL. To get the URL of the page the user is visiting, basename won't work. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 That is for files, not for URL. To get the URL of the page the user is visiting, basename won't work. i run this echo basename('http://blah.website.com/news/index.php'); and got this index.php LOL i guess your wrong buddy Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 I know my homonyms, and I can read. Go back and read the post again genius. He doesn't want index.php, he wants news. Basename does NOT do what he wants. "Buddy". Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 How can I retrive the full url of the current page and cut everything off but the end of it, like so: http://blah.website.com/news/index.php INTO --> news or http://www.website.com/index.php INTO --> index Please help! I'm a noob. that the post i answered whats wrong genius? or try http://www.php.net/manual/en/function.realpath.php Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 I'll just wait for you to either realize why what you posted gives a different result than what he wants, or for someone else to point it out to you because you're annoying me now. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 $path = 'http://blah.website.com/news/index.php'; basename($path, ".php"); //gives index realpath($path);//should give news whats wrong OK TELL ME ANYBODY WHAT IM DOING WRONG WITH THIS POST Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 $path = 'http://blah.website.com/news/index.php'; basename($path, ".php"); //gives index realpath($path);//should give news whats wrong OK TELL ME ANYBODY WHAT IM DOING WRONG WITH THIS POST your script prints nothing. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 That's just one of the problems with it. Read the docs dude. Also, I will repeat: Those functions are for working with FILES. Not the current url of the page a visitor is viewing. Not going to work here. The OP is obviously off working on the code, and if he needs help he'll post. The requirements he posted were a bit confusing but with some more url examples we can probably get it working. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 use echo LOL you cant see anything because you dont print it lol $path = 'http://blah.website.com/news/index.php'; echo basename($path, ".php"); //gives index echo realpath($path);//should give news Use a combination of the following: $_SERVER['REQUEST_URI']; substr(); strpos(); See if you can get anything with that, and if not we can help more. @josirose is that the code your talking? i guess long and not dyanmic Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 use echo LOL you cant see anything because you dont print it lol $path = 'http://blah.website.com/news/index.php'; echo basename($path, ".php"); //gives index echo realpath($path);//should give news Use a combination of the following: $_SERVER['REQUEST_URI']; substr(); strpos(); See if you can get anything with that, and if not we can help more. @josirose is that the code your talking? i guess long and not dyanmic GODDAMNIT STILL NOTHING... Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 Did you try your code? You say it "should give news" - DOES it? No. It doesn't: http://grady.us/temp/test.php If you want to fix it go ahead. My code works fine, and is perfectly dynamic. I use it as the base for my framework I use on every site I do. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 oops my bad even if my codes run still it wont work I should have read the post clearly SORRY people I thought its only the path not the URL :P :P ignore my post LOL Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 oops my bad even if my codes run still it wont work I should have read the post clearly SORRY people I thought its only the path not the URL :P :P ignore my post LOL its okay....its k... Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted August 31, 2007 Author Share Posted August 31, 2007 I love how I left for like 20 minutes and a whole argument sparked out!! Please mods don't lock this thread, I didn't do nothing!! LOL! jesirose, you were right all the time abut the argument... Anwaz, I tried $_SERVER['REQUEST_URI'];and echo the results. I got the full path except the annoying http://www.website.com thing. This is a good start. Now how do I use the string functions? I mean I know how to do it if the name was only like index.php but it may be news/extra/hello/blah.php? how do I do that? Sorry, I'm a newbie, a.k.a a NOOB! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 So you know one part of it you want to remove - anything before and including website.com. So find the position of website.com - using strpos. Then make a substring which goes from the end of website.com to the end of the string. Remember strpos gives you the start of the string so you'll need to add the strlen of website.com to the strpos to get the END of website.com Then we'll go from there. Can you give more examples of what you're trying to get? like, 5 example URLs and which part you want. I know you said always the last, but one example you gave was index.php? Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 $x=explode('?',$_SERVER['REQUEST_URI']); echo basename($x[0],'.php').'<br/>'; $x= explode ('/',$_SERVER['REQUEST_URI']); echo $x[count($x)-2]; [code] OK people this is tested and that works :P :P [/code] Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted August 31, 2007 Author Share Posted August 31, 2007 I made my own!! <?php $full = $_SERVER['REQUEST_URI']; $url = ltrim("$full", "/"); $path = substr($url, 0, strpos("$url", "/")); echo "$path"; ?> Now, $_SERVER['REQUEST_URI'] always gets whatever is after: website.com... so I don't have to worry about that, I simply then take the string and trim off the "/" from the left side. I find the offset that tells me when the next "/" is there and then I take the string from the beginning to the offset. many thanks to jesirose. She is a genius. Thanks to teng84, too. I"m sure your code works too, but I like to use mine. NOt in the mood to steal today! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.