Reading GET Variable Values Without Knowing The Variable Name / Key

I recently ran into an issue where I needed the values from GET variables, but I did not know what the variables name was. The problem was related to the Facebook API. The API used PHP to get the current URL, but the current URL was rewritten using Apaches Mod Rewrite, making it an invalid URL (technically). To overcome this I needed a way to regenerate the URL of the page without knowing how many GET variables there were going to be and what they would be called. After a bit of messing around this is what I came up with.

E.G. You have a rewritten URL that looks like the following
http://example.net/page/123/345.html

This URL works with Apache, but it is not a proper URL that another website can understand without having the information to map the variable names.  The code snippet below will take a URL that you have rewritten using mod rewrite or any other method and output it as a URL with the GET variables present.

You will need to replace the name of the URL to the url of your website. If you run this on any page it will generate the full URL of your page.

http://example.net/page/123/345.html   =>   http://example.net/page.php?var1=123&var2=345

$filename = $_SERVER['PHP_SELF'];
$url = $filename;
if(isset($_GET))
{
  $url = $url."?";
  $counter = 0;
  foreach($_GET as $var => $value)
  {
    if($counter != 0)
    {
      $url = $url."&";
    }
    $url = $url."&".$var."=".$value;
    $counter ++;
  }
}
$currentUrl = "http://example.net".$url;

Related Articles

Related Questions

What Hobbies Have Helped You in Your Programming Career?

I'm curious about the hobbies that have contributed to your success in programming jobs. Personally, I've found that photo and video editing really helped...

How Can We Track User Engagement When We Don’t Own the Frontend?

I'm looking to optimize the content provided by my company's APIs, specifically to customize it based on user interactions. For instance, if a user...

Is It Safe to Log Into Microsoft Office After Cracking It?

I recently cracked Microsoft Office and I'm wondering if I can safely log into my account without getting caught. Is it risky? Will Microsoft...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions