Uutiset:

Join the Facebook Fan Page.

Main Menu
Advertisement:

Using PHP to get current URL

Aloittaja hillrunr, syyskuu 20, 2007, 11:10:59 AP

« edellinen - seuraava »

hillrunr

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.

Remaker

try

echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Remaker - My blog

hillrunr

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?

karlbenson

use

print_r($_SERVER);

and see which server variable uses the necessary filename.

Note: Don't use HTTP_REFERER because it can be faked.

hillrunr

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.

karlbenson

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

hillrunr

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.

jor4

thanks, it work now  ;)
__
hxxp:djcall.com [nonactive]

Advertisement: