SystemOverload Posted September 24, 2009 Share Posted September 24, 2009 PHP 5.2.5 I want to create a function to manipulate the current URL. How is the best way to retrieve the separate parts of the URL to a variable. 1 - "HTTP" or "HTTPS" etc 2 - www.domain.com 3 - directory/2nddirectory 4 - index.php 5 - id=10&mode=qv I want to be able to insert and/or remove 'variables' to facilitate passing more information thru _GET Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/ Share on other sites More sharing options...
mattal999 Posted September 24, 2009 Share Posted September 24, 2009 the parse_url() function is exactly what you need. Example: <?php $url = 'http://username:password@hostname/path?arg=value#anchor'; print_r(parse_url($url)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924234 Share on other sites More sharing options...
SystemOverload Posted September 24, 2009 Author Share Posted September 24, 2009 I'm sorry, I didn't make myself clear. How do I retrieve the seperate parts of the url, parse_url() is just a function, how do I get the URL itself into a variable so I can start manipulating it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924251 Share on other sites More sharing options...
knsito Posted September 24, 2009 Share Posted September 24, 2009 Your asking about the URL your script resides at right? Look at the superglobal $_SERVER variable which holds various url/server data http://us3.php.net/reserved.variables.server print_r($_SERVER) to see whats in there. That + parse_url() will get what you need I'm sorry, I didn't make myself clear. How do I retrieve the seperate parts of the url, parse_url() is just a function, how do I get the URL itself into a variable so I can start manipulating it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924265 Share on other sites More sharing options...
SystemOverload Posted September 24, 2009 Author Share Posted September 24, 2009 Trouble is _SERVER is an array and parse_url takes a string... How do i get from _SERVER to "http://www.domain.com/dir1/dir2/something.php?id=1&mode=pwc" Sorry for being a pain, lol Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924276 Share on other sites More sharing options...
mikesta707 Posted September 24, 2009 Share Posted September 24, 2009 you can get strings from the server array by accessing its index's with its keys. for example: //the script name $name = $_SERVER['SCRIPT_NAME']; //request method (IE get or post) $method = $_SERVER['REQUEST_METHOD']; //the protocol (LIKE HTTP, HTTPS, etc.) //btw for HTTP it will return the version also, IE HTTP/1.1 $protocol = $_SERVER['SERVER_PROTOCOL']; //query string, ie what the ?get=value part of the URL $query = $_SERVER['QUERY_STRING']; if you check out the manual it has all of them Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924331 Share on other sites More sharing options...
SystemOverload Posted September 24, 2009 Author Share Posted September 24, 2009 ok, thanks for everyone's input, especially the last guy, upto the point where he posted, I didn't get exactly what i asked for... If you read my original request: How is the best way to retrieve the separate parts of the URL to a variable. 1 - "HTTP" or "HTTPS" etc 2 - www.domain.com 3 - directory/2nddirectory 4 - index.php 5 - id=10&mode=qv Everyone except the last guy dropped titbits, but didn't give me exactly what I needed, finally I finished up with the following: (credit for the substr() bit goes to a random tutorial I found thru google) $varQuery = ""; if (strlen($_SERVER['QUERY_STRING']) != 0) {$varQuery = "?";} $varURL = substr($_SERVER['SERVER_PROTOCOL'],0,strpos($_SERVER['SERVER_PROTOCOL'],'/')) . "://" . $_SERVER["HTTP_HOST"] . $_SERVER['SCRIPT_NAME'] . $varQuery . $_SERVER['QUERY_STRING'] ; Thanks for everyones help. Quote Link to comment https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924369 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.