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 ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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..!)

Link to comment
Share on other sites

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..  :'(

Link to comment
Share on other sites

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

 

Then why don't you try it?

 

so his looks a bit more secure if I could use it..

 

How do you know it looks more secure? You don't know what it does do you? Why don't you try it as well?

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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']);
}

Link to comment
Share on other sites

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 "?"..

Link to comment
Share on other sites

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.

Link to comment
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.