Author Topic: [PHP] Get the URL of the html file that called it  (Read 11559 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
[PHP] Get the URL of the html file that called it
« on: September 11, 2011, 02:33:03 pm »
Let's say I have this HTML file:

Code: [Select]
<!doctype html><html>
  <head>
    <title>My Page</title>
  </head>

  <body>
    <form action="handler.php" method="post">
      <p><input type="text" name="text" id="text" /></p>
      <p><input type="submit" value="Go" /></p>
    </form>
  <body>

</html>

And then I have this file (handler.php):

Code: [Select]
<?php
  $text 
$_POST["text"]; //gets the text from the text field
?>


Basically, it's a very simple form. How can I know the name of the file that called the .php file?

I want to get it's URL, is there a way I can do this? Thanks!
« Last Edit: September 11, 2011, 02:39:09 pm by ephan »

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6268
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: [PHP] Get the URL of the html file that called it
« Reply #1 on: September 11, 2011, 02:54:26 pm »
$_SERVER['HTTP_REFERER']; I think will work.
Try passing it as a parameter if that doesn't work ( you can figure out the url dynamically using window.location ( JavaScript ) )
/e

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Get the URL of the html file that called it
« Reply #2 on: September 11, 2011, 02:55:27 pm »
It all depends on whether the client's browser decides to give that information to your script. The variable $_SERVER['HTTP_REFERER'], if set, refers to the page that led the user there. Unfortunately, browsers aren't obligated to set it in any way, so you can never be sure.

EDIT: Ninja'd.




Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6268
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: [PHP] Get the URL of the html file that called it
« Reply #3 on: September 11, 2011, 02:58:33 pm »
It all depends on whether the client's browser decides to give that information to your script. The variable $_SERVER['HTTP_REFERER'], if set, refers to the page that led the user there. Unfortunately, browsers aren't obligated to set it in any way, so you can never be sure.
Yeah I just found that. You would probably want to use the passing the url mehod ephan since it will work 100% of the time as opposed to the other method.
/e

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Get the URL of the html file that called it
« Reply #4 on: September 11, 2011, 03:00:23 pm »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #5 on: September 11, 2011, 03:03:40 pm »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.

Yes, but I don't really like to trust Javascript this things, because the user could delete it to make my php script fail. I like to think about anything that can happen.

Either way, if that's the only way of achieving, I'll have to go for it.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #6 on: September 11, 2011, 03:30:55 pm »
There's no failproof method, since the only part that knows that is the client. If you wanted to make your HTML file PHP, you could set a session variable to store it, but even that isn't 100%.
Omnimaga Admin

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Get the URL of the html file that called it
« Reply #7 on: September 11, 2011, 03:32:19 pm »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.
Yes, but I don't really like to trust Javascript this things, because the user could delete it to make my php script fail. I like to think about anything that can happen.
Exactly, that's why I suggested using a hidden input. No JavaScript needed.




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #8 on: September 11, 2011, 03:34:03 pm »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.
Yes, but I don't really like to trust Javascript this things, because the user could delete it to make my php script fail. I like to think about anything that can happen.
Exactly, that's why I suggested using a hidden input. No JavaScript needed.

When I said "Javascript", I meant the HTML Code. The user can change it I think, right?

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #9 on: September 11, 2011, 03:35:01 pm »
The user can send anything to your server, there's not much you can do about it. They have to be trusted to some degree.
Omnimaga Admin

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [PHP] Get the URL of the html file that called it
« Reply #10 on: September 11, 2011, 03:40:38 pm »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.
Yes, but I don't really like to trust Javascript this things, because the user could delete it to make my php script fail. I like to think about anything that can happen.
Exactly, that's why I suggested using a hidden input. No JavaScript needed.
When I said "Javascript", I meant the HTML Code. The user can change it I think, right?
Only through a debugger like Firebug, but then you can do anything, so it's no different that adding a new <a> tag, for instance.




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #11 on: September 11, 2011, 04:50:52 pm »
Yes, I managed to do it in another way, but er, thanks everyone!

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6268
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: [PHP] Get the URL of the html file that called it
« Reply #12 on: September 12, 2011, 01:47:48 am »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.
That's actually what I was talking about, have the javascript grab it. Although hard coding it in would work better.
Yes, I managed to do it in another way, but er, thanks everyone!
Np, glad to be able to give you some options. How did you actually go about doing it might I ask?
/e

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [PHP] Get the URL of the html file that called it
« Reply #13 on: September 12, 2011, 05:31:47 am »
Actually, a more fail-proof method to do that (without Javascript) would be to use an <input type="hidden" name="url" value="CURRENT_URL" /> or something similar.
That's actually what I was talking about, have the javascript grab it. Although hard coding it in would work better.
Yes, I managed to do it in another way, but er, thanks everyone!
Np, glad to be able to give you some options. How did you actually go about doing it might I ask?

I just have a string with the location of the form. I wanted to make it dynamic, to work everywhere, but it's not really how I wanted.

Offline Ikkerens

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 378
  • Rating: +28/-9
  • JavaScript Magician
    • View Profile
    • Walotech
Re: [PHP] Get the URL of the html file that called it
« Reply #14 on: September 12, 2011, 06:46:35 am »
There's 1 real secure way to do this.
As soon as the form is loaded (for example: ./form.php)
You create a $_SESSION['currentpage'] = 'form.php';

And when the user submits the form, you check that variable. If it's empty (or !isset(... ), the user has deleted the session cookie.
In that case you simply return back to the form ;)
« Last Edit: September 12, 2011, 06:49:04 am by Ikkerens »

Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...