Getting full URL

Hi,

I’m trying to get the full URL not URI for the current page. I have made a menu system and want to set the relevant <li> tag as “current”.

All the urls in the menu system are using mod_rewrite so if I use $_SERVER[‘REQUEST_URI’] it will return something like - index.php?mod=user&page=dashboard. What I want to find is “http://www.domain.com/dashboard/”. Is it possible to get the URL instead of URI?

Thanks for any help.

Take a look at the output of:


var_dump($_SERVER);

Which you should embed temporarily in the page you are trying to establish the url of.

That spits out a lot of vars. I cannot remember exactly how to do what you want, hence a somewhat woolly answer… hth

thanks for your help, unfortunately it didn’t return what I need. I can get the URI but not the URL, seems stupid it’s not a $_SERVER variable.

Well if this is what you are getting back:


index.php?mod=user&page=dashboard

and you only want “dashboard” then you should be able to use something like this:


// spoofing your actual value for test purposes
$_SERVER['QUERY_STRING'] =  "mod=user&page=dashboard";

parse_str( $_SERVER['QUERY_STRING']  , $output);
echo $output['page'];  // dashboard

Then use that $output[‘page’] variable in your url string for your menu.

[fphp]parse_str[/fphp] man page contains sample I hacked that from

yea that won’t work either, otherwise I could just use $_GET[‘page’];

Basically I have a table with 100+ links in and I need to check if the link is the current active one - aka the url value equals the url for that page.

What I want to find is “http://www.domain.com/dashboard/”.

Well that is what I showed you - how to get that variable from the $_SERVER var


$url = "http://www.domain.com/" . $output['page'];

Is your question how to match that var against the other 100?

So if I use the dashboard example again -
page = http://www.domain.com/dashboard/ … translates to … http://www.domain.com/index.php?mod=user&page=dashboard

In the database there’s 3 rows -
(id, url, text)
1, /, Home
2, dashboard/, Dashboard
3, forum/, Forum

What I need to do is using for example $row[‘url’]; check something along the lines of -


<?php
$thisPage = 'Unknown function';
define( 'URL', 'http://www.domain.com/' );

#mysql query & fetch array here

if( $thisPage == URL.$row['url'] )
{
print 'this is the current page ' . $row['text'];
}
?>

What I need to find is how to get the value for “$thisPage”.


// spoofing your actual value for test purposes
$_SERVER['QUERY_STRING'] =  "mod=user&page=dashboard";

function getThisPage(){
parse_str( $_SERVER['QUERY_STRING']  , $output);
return $output['page'];  // dashboard  
}

$thisPage = getThisPage();

if ($thisPage == $row['url'] ){
// bingo , its a match

}

No?

But then on the forum it won’t work if it translates to http://www.domain.com/index.php?mod=forum.

The navigation goes 3/4 layers deep and it needs to be able to tell that. I guess my only option seems to be typing out the equivalent query string in the db as well.

Thanks for your help, I’m still open to ideas :slight_smile:


function getThisPage(){

parse_str( $_SERVER['QUERY_STRING']  , $output);

if( isset($output['page']) ){
return $output['page'];  // page if it set 
}
elseif( isset($output['mod']) ){
return $output['mod'];  // mod if it set  
}else{
return '';  // return to index page onlu
}

}

Hi, This is the function I wrote that should work, I’ve used it on several sites:

// Get url of current page, including get vars - all url encoded so they can be passed as a single argument
// $use_get - if true then add $_GET arguments to end of filename

function get_this_page($use_get=false){
	
	$files_arr = explode("/", $_SERVER["SCRIPT_NAME"]);
	$thispage = $files_arr[count($files_arr)-1];
	if (count($_GET) > 0 && $use_get == true) {
		foreach ($_GET as $key => $value) {
			$thispagevars = $key.'='.$value.'&amp;';
		}
		$thispagevars = substr($thispagevars, 0, -5);
		$thispagevars = '?'.urlencode($thispagevars);
	}
	else {
		$thispagevars = '';
	}
	return $thispage.$thispagevars;
}

probably not the neatest way, but it’s worked with everything I’ve thrown at it.

Thanks alexson for that function, unfortunately it only returned “index.php”, nothing else.

Seem’s like there is no shortcut for this, I’ll just type out each query string for each link then match it that way.

Thanks for your help.

Add this code temporarily to one of your normal pages (where you are expecting menu items to be pre-selected), and see if there is any output.


parse_str( $_SERVER['QUERY_STRING']  , $output);
var_dump( $output );
$files_arr = explode("/", $_SERVER["SCRIPT_NAME"]);
var_dump( $files_arr);

Okay that’s started to output what I need, thanks. I’ll try messing about with that and let you guys know how it goes.

When you used my function did you pass the true value? it’s set to only default to filename otherwise you should call it using:

$this_url = get_this_page(true);

I tried it with true again and it still just showed index.php. Thanks for posting though.

This is what I’ve got working thanks to help from you both -


parse_str( $_SERVER['QUERY_STRING']  , $output );
foreach( $output as $i => $bit )
{
	$output[$i] = urlencode( $bit );
}
$thisPage = implode( '/', $output );
$thisPage = URL . $thisPage;
$thisPage = ( strrpos( $thisPage, '/' ) == strlen( $thisPage ) ) ? $thisPage : $thisPage . '/';