Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/175391-url-manipulation/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924265
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924331
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/175391-url-manipulation/#findComment-924369
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.