1

I wrote a function getQuery($param) which returns data from the

function getQuery($param){
    if(!empty(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY))){
    $queries = parse_url(strtok($_SERVER['REQUEST_URI'], PHP_URL_QUERY))['query'];
    parse_str($queries, $query);
    return $query[$param];
    }
}

//url = https://example.com/test?name=Arun&email=arun@example.com

echo getQuery("name"); // Output will be:Arun

But if the URL parameters contain "6" this function is only returning query data until that character

//i.e. if URL = https://example.com/test?name=Arun&email=arun056@example.com
echo getQuery("email");   //  Expected Output: arun056@example.com
                        //  Original Output: arun05

Is there any solution to fix this bug? Someone, please help

Edit:

Thanks for the reply. I found another method to write this function

<?php
function testgetQuery($param){
    $queries = explode("&",explode("?",$_SERVER['REQUEST_URI'])[1]);
    $getQuery = [];
    foreach($queries as $query){
        $getQuery[explode("=", $query)[0]] = explode("=", $query)[1];
    }
    return $getQuery[$param];
}

This worked pretty well

2 Answers2

0

Your function is a very complicated way to get query parameters. You can simply use $_GET['email'] to get the email. You should also apply a default in case it is not set.

$email = $_GET['email'] ?? false;

If you wanted to turn this into a helper function,

function getQuery($key, $default = ''): string 
{
    return $_GET[$key] ?? $default;
}

$email = getQuery('email');

The reason it is being truncated if you have a 6 in the query string is because of this line. strtok will tokenize a string by a delimiter but you have provided PHP_URL_QUERY as the delimiter. That is a predefined PHP constant and has a value of 6. So strtok will split the string on 6.

strtok($_SERVER['REQUEST_URI'], PHP_URL_QUERY)

enter image description here


Yes, I can use $_GET. but, I developed a simple routing system. In this $GET is not working. So, I had to write this getQuery() function.

That doesn't make a whole lotta sense to me, but if you want to use parse_url you can do something like this. But know that $_GET works to get everything after the ? too.

function getQuery($key, $default = ''): string
{
    $parts = parse_url($_SERVER['REQUEST_URI']);
    parse_str($parts['query'], $query);
    
    return $query[$key] ?? $default;
}

$email = getQuery('email');
Tim
  • 2,563
  • 1
  • 23
  • 31
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • Yes, I can use $_GET. but, I developed a simple routing system. In this $GET is not working. So, I had to write this getQuery() function. – Radhe Shyam Salopanthula Feb 16 '22 at 02:30
  • Thanks for the help. I tried another method to fix it. ```function testgetQuery($param){ $queries = explode("&",explode("?",$_SERVER['REQUEST_URI'])[1]); $getQuery = []; foreach($queries as $query){ $getQuery[explode("=", $query)[0]] = explode("=", $query)[1]; } return $getQuery[$param]; }``` – Radhe Shyam Salopanthula Feb 16 '22 at 02:39
  • I added an edit. – waterloomatt Feb 16 '22 at 02:41
  • _"but, I developed a simple routing system. In this $GET is not working."_ - why not, how did you manage to break it? You are still taking the original query string contents from `$_SERVER['REQUEST_URI']`, only you are parsing it yourself - so why should PHP's default mechanism, that would be doing pretty much the same thing, not work? – CBroe Feb 16 '22 at 08:02
0

First, you must know what exactly your code doing. Like why you need a function to use?

But if the URL parameters contain "6" this function is only returning query data until that character

Why that happened?

By the definition of strtok from w3schools.

The strtok() function splits a string into smaller strings (tokens).

Where the second parameter is used to:

Specifies one or more split characters

So, why the email only returned before the character of "6"? It is because the value of PHP_URL_QUERY is 6 reference.

So this is like we wrote:

strtok($_SERVER['REQUEST_URI'], 6)

And then from the URL you give, it going to this:

strtok('https://example.com/test?name=Arun&email=arun@example.com', 6)
// You got https://example.com/test?name=Arun&email=arun05

It splitted the URL by the character of 6.


So, for the final words, if you just want to get the query parameter from a URL string. I think it is easy to find already answered questions.

Please check the link below:

  1. Get URL query string parameters
  2. How can I get parameters from a URL string?
Nur Muhammad
  • 174
  • 10