Simple Machines Community Forum

General Community => Scripting Help => Aiheen aloitti: hillrunr - syyskuu 20, 2007, 11:10:59 AP

Otsikko: Using PHP to get current URL
Kirjoitti: hillrunr - syyskuu 20, 2007, 11:10:59 AP
This seems like a simple problem but it has me stumped. I think it's done in places within SMF, so I'm sure there is a way to do it and someone here knows how to do it.

I have a PHP webpage that calls another PHP script to do some utility functions. I want to put a form into that utility script. One of the hidden variables in the form is the URL the visitor is on so the script called by the form can return the user to where they came from.

I'm trying to get this using the $_SERVER variables but running into an issue. No matter what I try to use (REQUEST_URI, PHP_SELF, SCRIPT_NAME) it seems like I'm getting the name of the utility script, as opposed to the name of the page the user is visiting.

For example, I may have a page someone is visiting that is named:
http://www.mysite.com/index.php
This page calls a utility script at:
http://www.mysite.com/utility.php

I need to get index.php (the page the user is visiting) returned from the code within utility.php but, no matter what I have tried so far, I end up getting utility.php.

Any thoughts?

Thanks in advance for the help.
Otsikko: Re: Using PHP to get current URL
Kirjoitti: Remaker - syyskuu 20, 2007, 02:01:53 IP
try

echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Otsikko: Re: Using PHP to get current URL
Kirjoitti: hillrunr - syyskuu 20, 2007, 06:33:02 IP
When I try that, I get http://www.mysite.com/utility.php instead of http://www.mysite.com/index.php. Is it possible I'm getting this result because of some funky server setting?
Otsikko: Re: Using PHP to get current URL
Kirjoitti: karlbenson - syyskuu 20, 2007, 08:27:56 IP
use

print_r($_SERVER);

and see which server variable uses the necessary filename.

Note: Don't use HTTP_REFERER because it can be faked.
Otsikko: Re: Using PHP to get current URL
Kirjoitti: hillrunr - syyskuu 20, 2007, 08:46:55 IP
Ahh, that's the command I was looking for, hoping to see all the variables!

Unfortunately, none of the variables hold the index.php filename(?)

Is there any other way within php to get the name of the file the user requested, as opposed to the name of the file that the request is coming from? This seems like such a basic thing, I'm surprised I can't figure this one out myself but it definitely has me completely stumped.
Otsikko: Re: Using PHP to get current URL
Kirjoitti: karlbenson - syyskuu 20, 2007, 08:51:24 IP
Also
I'm not sure how safe it is having a hidden form field with the location to redirect.
That hidden field can be changed by javascript/browser extensions.

If you do use it, make sure you sanitiize it properly and make sure it only goes where you allow it.

---

Can you not call the request page in a variable in the index.php before you call the utility
set that variable as a global or pass it into the functions in your utility.php
Otsikko: Re: Using PHP to get current URL
Kirjoitti: hillrunr - syyskuu 20, 2007, 09:10:33 IP
That's a great point. I will keep that in mind.

My main concern is that I'm using the file where this code could be placed in all over the site right now. Ideally, I'd drop this code into the one file and have it throughout the site without changing dozens of other files. If I can't, though, I can't. Maybe it will force me to take the next step in cleaning up some code.
Otsikko: Re: Using PHP to get current URL
Kirjoitti: jor4 - syyskuu 27, 2007, 11:00:01 AP
thanks, it work now  ;)
__
http://djcall.com (http://djcall.com)