Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Urls with a value - without a parameter? (Get request)

Tags:

http

url

php

I may be completely off base, but I'm pretty sure I've seen URLs structured like this before:

http://www.example.com/dir/?=1 with no visible parameter being passed, but a value given.

If this is a valid url structure, how would one get the parameter value 1 in PHP?

like image 712
Avicinnian Avatar asked Feb 23 '23 22:02

Avicinnian


2 Answers

This value won't be available through $_GET. I found it only in $_SERVER["QUERY_STRING"] or $_SERVER["REQUEST_URI"]. So I assume you have to parse these variables on your own.

like image 200
rabudde Avatar answered Feb 25 '23 11:02

rabudde


Here's a quick and dirty function I use to read such URLs

function get_url_params() {
    $param_count = 0;
    $params = array();
    foreach (explode('&', $_SERVER['QUERY_STRING']) as $param) {
        $param = explode('=', $param);

        if ($param[0] == '') {
            $params[$param_count] = $param[1];
        } else {
            $params[$param[0]] = $param[1];
        }

        $param_count++;
    }
    return $params;
}

print_r(get_url_params());
like image 37
Nexerus Avatar answered Feb 25 '23 12:02

Nexerus