Home > Enterprise >  get params querys from get request php
get params querys from get request php

Time:12-03

I send this request

http://localhost/login.php/auth/SigIn?username=1

enter image description here

and my code to get values is this

 public function SigInAction(){
        $strErrorDesc = '';
        $requestMethod = $_SERVER["REQUEST_METHOD"];
        $arrQueryStringParams = $this->getQueryStringParams();
        if (strtoupper($requestMethod) == 'GET') {
            try {
                $p_userName = "";
                $p_password = "";
                
                if (isset($arrQueryStringParams['username']) && $arrQueryStringParams['username']) {
                $p_userName = $arrQueryStringParams['username'];                    
                }
} catch (Error $e) {
                $strErrorDesc = $e->getMessage().'Something went wrong! Please contact support.';
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
            }
        } else {
            $strErrorDesc = 'Method not supported';
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
        } 
}

and my method getQueryStringParams is

protected function getQueryStringParams()
    {
        return parse_str($_SERVER['QUERY_STRING'], $query);
    }

when intent acces to variable i have null value

echo $arrQueryStringParams

enter image description here

try this ,

$p_userName = $_GET['username'] 

enter image description here

CodePudding user response:

Try this:

protected function getQueryStringParams()
    {
        return $_GET;
    }

CodePudding user response:

I recommend using $_REQUEST. It gets not only GET, but even POST, PUT etc.

You can get the details here.

Code will look something like this:

protected function getQueryStringParams()
{
    return $_REQUEST;
}
  •  Tags:  
  • php
  • Related