Jump to content

[SOLVED] (VERY EASY!!!!) Help getting current url..


Cory94bailly

Recommended Posts

Hi.

 

My current code:

$page     = basename($_SERVER['PHP_SELF']); //Current page user is on

 

I am using that am I am getting "index.php" and that is basically it.. But I would like it to include stuff like "index.php?page=links"...

 

I tried every link I have on my site with the log and I just get "index.php"...

 

I DO know of another way IF NEEDED... But this would be great if it could be improvised ;)

Look at print_r($_SERVER).

 

Sorry, do you want me to try that out or to use it instead of what I had?

 

I just got it now, I think Corbin meant to look at the output of print_r($_SERVER), literally putting in on a page and seeing what it shows, because it has the things necessary to make the full url within its array.

echo $_SERVER[php_SELF] ."?". $_SERVER[QUERY_STRING];

Should give you the entire thing.

 

Ok.. But I don't want http://www.site.com/page.php?action=whatever I wannt page.php?action=whatever...

 

Which will work?

$page     = basename($_SERVER['PHP_SELF']) ."?". $_SERVER[QUERY_STRING]; //Current page user is on

$page     = basename($_SERVER['PHP_SELF'] ."?". $_SERVER[QUERY_STRING]); //Current page user is on

I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER)

 


<?php

$_SERVER = array_merge ( $_SERVER, $_ENV );

$url  = $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://';

$url .= $_SERVER['SERVER_NAME'];

if ( ! empty ( $_SERVER['SCRIPT_NAME'] ) )
{
$url .= $_SERVER['SCRIPT_NAME'];
}
else
{
$url .= str_replace ( str_replace ( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ), '', $_SERVER['PATH_TRANSLATED'] );
}

if ( ! empty ( $_SERVER['QUERY_STRING'] ) )
{
$url .= '?' . $_SERVER['QUERY_STRING'];
}

echo $url;

?>

I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER)

 


<?php

$_SERVER = array_merge ( $_SERVER, $_ENV );

$url  = $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://';

$url .= $_SERVER['SERVER_NAME'];

if ( ! empty ( $_SERVER['SCRIPT_NAME'] ) )
{
$url .= $_SERVER['SCRIPT_NAME'];
}
else
{
$url .= str_replace ( str_replace ( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ), '', $_SERVER['PATH_TRANSLATED'] );
}

if ( ! empty ( $_SERVER['QUERY_STRING'] ) )
{
$url .= '?' . $_SERVER['QUERY_STRING'];
}

echo $url;

?>

 

Wow! Thanks alot.. But I am setting it in my config file so what would I do for the equivalent of this?

$page     = basename($_SERVER['PHP_SELF']) ."?". $_SERVER[QUERY_STRING]; //Current page user is on

 

(Sorry btw, it's 5:48 AM so I don't even know what's happening..!)

But I am setting it in my config file so what would I do for the equivalent of this?

 

Just use...

 

$page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];

 

if printf's code isn't working for you.

 

That's the thing, I don't know if it works or not..

 

Also:

I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER)

 

That kinda scares me so his looks a bit more secure if I could use it..  :'(

I used this:

$page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];

 

And it seemed to work fine, the only thing is that it puts a ? at the end of index.php even if the user is not on a page with "?" in the url. Is there any way to check if the user is on a page with "?"??? And if they are, add the "?", if not.. Don't add one ;)

Is there any way to check if the user is on a page with "?" And if they are, add the "?", if not.. Don't add one

 

Yes. A little logical thinking and this is what I came up with.

 

if (count($_SERVER['QUERY_STRING'])) {
  $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
} else {
  $page = basename($_SERVER['PHP_SELF']);
}

Is there any way to check if the user is on a page with "?" And if they are, add the "?", if not.. Don't add one

 

Yes. A little logical thinking and this is what I came up with.

 

if (count($_SERVER['QUERY_STRING'])) {
  $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
} else {
  $page = basename($_SERVER['PHP_SELF']);
}

 

Tried, still has the "?"..

Sorry, should be....

 

if (strlen($_SERVER['QUERY_STRING'])) {
  $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
} else {
  $page = basename($_SERVER['PHP_SELF']);
}

 

Wooh, after 3 days and 2 pages, we got it ;)

 

Thanks.. Solved..

Sorry, should be....

 

if (strlen($_SERVER['QUERY_STRING'])) {
  $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
} else {
  $page = basename($_SERVER['PHP_SELF']);
}

 

Wooh, after 3 days and 2 pages, we got it ;)

 

Thanks.. Solved..

 

Would have been solved alot quicker if you were to actually try things.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.