Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 6

How to get the last part of a URL in PHP?

I have got a link to a page which adds a user inputted code to the end of the URL, for example:

http://example.com/code.php?ABC123

What I need to know for my webpage is how (using PHP) can I extrcact the "ABC123" from the page's current URL so it can be used again later?

The background reason for wanting this is that I am using a one-time-use download script and when the user has a correct code, I want the page to first redirect to a "Thank you" page and then for the download to initiate. In order for the one-time-use script to check the entered code, the entered code must be transferred from the first page to the redirected page.

If the reasoning doesn't make sense or it could be done much easier I am at a stage where that no longer bothers me as this website is only a small project due to be completed for Thursday, I would just like to know how to do the above URL extraction.

Thanks,

TMX boxburn

1 Answer

Relevance
  • ?
    Lv 6
    8 years ago
    Favourite answer

    First off, in PHP, use $_SERVER['REQUEST_URI'] to get current URL.

    Then, if it was /mysite/myproject/mydetail/myd2/myd3 for example

    If you do this...

    $array = explode('/', $_SERVER['REQUEST_URI']);

    your array should look like this...

    echo $array [0]; // mysite

    echo $array [1]; // myproject

    echo $array [2]; // mydetail

    echo $array [3]; // myd2

    echo $array [4]; // myd3

Still have questions? Get answers by asking now.