_get param from url php

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_GETHTTP GET variables

Description

An associative array of variables passed to the current script via the URL parameters (aka. query string). Note that the array is not only populated for GET requests, but rather for all requests with a query string.

Examples

Example #1 $_GET example

echo 'Hello ' htmlspecialchars($_GET["name"]) . '!';
?>

Assuming the user entered http://example.com/?name=Hannes

The above example will output something similar to:

Notes

Note:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Note:

The GET variables are passed through urldecode().

CleverUser123

9 months ago

If you're tired of typing $var = $_GET['var'] to get variables, don't forget that you can also use:

extract($_GET, EXTR_PREFIX_ALL, "g")

So if you have $_GET['under'], you can do $g_under. It's way shorter if you have more get elements! If you don't want prefix, do

extract($_GET)

to get normal. So $_GET['under'] would become $under. Might not extract under if it already exists, however.

An Anonymous User

1 year ago

// It is important to sanitize
// input! Otherwise, a bad actor
// could enter ''
// in a URL parameter. Assuming you echo it, this
// would inject scripts in an XSS attack.
//
// The solution:
$NAME = $_GET['NAME'];
// Bad:
echo $NAME;
// that one is vulnerable to XSS
// Good:
echo htmlspecialchars($NAME);
// Sanitizes input thoroughly.
?>

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions.

    Note: Page URL and the parameters are separated by the ? character.

    parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it. It parse an URL and return an associative array which contains its various components.

    Syntax: 

    parse_url( $url, $component = -1 )

    parse_str() Function: The parse_str() function is used to parse a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.

    Syntax:  

    parse_str( $string, $array )

    Approach: Parse the URL string using parse_url() function which will return an associative array that contains its (passed URL) various components. The query of the array returned by parse_url() function which contains a query string of URL.

    Below examples uses parse_url() and parse_str() function to get the parameters from URL string. 

    Example 1:  

    PHP

    Example 2: 

    PHP

    Output:

    Hi Amit your emailID is 

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    I have an HTML form field $_POST["url"], having some URL strings as the value.

    Example values are:

    https://example.com/test/1234?email=
    https://example.com/test/1234?basic=2&email=
    https://example.com/test/1234?email=
    https://example.com/test/1234?email=&testin=123
    https://example.com/test/the-page-here/1234?someurl=key&email=
    

    etc.

    How can I get only the email parameter from these URLs/values?

    Please note that I am not getting these strings from the browser address bar.

    _get param from url php

    asked Jul 14, 2012 at 3:27

    _get param from url php

    Asim ZaidiAsim Zaidi

    25.8k48 gold badges129 silver badges219 bronze badges

    5

    You can use the parse_url() and parse_str() for that.

    $parts = parse_url($url);
    parse_str($parts['query'], $query);
    echo $query['email'];
    

    If you want to get the $url dynamically with PHP, take a look at this question:

    Get the full URL in PHP

    answered Jul 14, 2012 at 3:47

    2

    All the parameters after ? can be accessed using $_GET array. So,

    echo $_GET['email'];
    

    will extract the emails from urls.

    answered Jul 14, 2012 at 3:29

    hjpotter92hjpotter92

    76.2k34 gold badges137 silver badges176 bronze badges

    3

    Use the parse_url() and parse_str() methods. parse_url() will parse a URL string into an associative array of its parts. Since you only want a single part of the URL, you can use a shortcut to return a string value with just the part you want. Next, parse_str() will create variables for each of the parameters in the query string. I don't like polluting the current context, so providing a second parameter puts all the variables into an associative array.

    $url = "https://mysite.com/test/1234?email=&testin=123";
    $query_str = parse_url($url, PHP_URL_QUERY);
    parse_str($query_str, $query_params);
    print_r($query_params);
    
    //Output: Array ( [email] =>  [testin] => 123 ) 
    

    answered Jul 14, 2012 at 4:01

    _get param from url php

    JCottonJCotton

    11.5k5 gold badges53 silver badges57 bronze badges

    As mentioned in another answer, the best solution is using parse_url().

    You need to use a combination of parse_url() and parse_str().

    The parse_url() parses the URL and return its components that you can get the query string using the query key. Then you should use parse_str() that parses the query string and returns values into a variable.

    $url = "https://example.com/test/1234?basic=2&email=";
    parse_str(parse_url($url)['query'], $params);
    echo $params['email']; // 
    

    Also you can do this work using regex: preg_match()

    You can use preg_match() to get a specific value of the query string from a URL.

    preg_match("/&?email=([^&]+)/", $url, $matches);
    echo $matches[1]; // 
    

    preg_replace()

    Also you can use preg_replace() to do this work in one line!

    $email = preg_replace("/^https?:\/\/.*\?.*email=([^&]+).*$/", "$1", $url);
    // 
    

    _get param from url php

    answered Nov 11, 2018 at 9:49

    MohammadMohammad

    20.6k15 gold badges53 silver badges80 bronze badges

    1

    Use $_GET['email'] for parameters in URL. Use $_POST['email'] for posted data to script. Or use _$REQUEST for both. Also, as mentioned, you can use parse_url() function that returns all parts of URL. Use a part called 'query' - there you can find your email parameter. More info: http://php.net/manual/en/function.parse-url.php

    _get param from url php

    answered Jul 14, 2012 at 3:56

    1

    You can use the below code to get the email address after ? in the URL:

    _get param from url php

    answered Feb 8, 2016 at 10:18

    1

    I a created function from Ruel's answer.

    You can use this:

    function get_valueFromStringUrl($url , $parameter_name)
    {
        $parts = parse_url($url);
        if(isset($parts['query']))
        {
            parse_str($parts['query'], $query);
            if(isset($query[$parameter_name]))
            {
                return $query[$parameter_name];
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }
    

    Example:

    $url = "https://example.com/test/the-page-here/1234?someurl=key&email=";
    echo get_valueFromStringUrl($url , "email");
    

    Thanks to @Ruel.

    _get param from url php

    answered Mar 14, 2017 at 12:53

    mghhgmmghhgm

    1,55722 silver badges43 bronze badges

    $web_url = 'http://www.writephponline.com?name=shubham&email=';
    $query = parse_url($web_url, PHP_URL_QUERY);
    parse_str($query, $queryArray);
    
    echo "Name: " . $queryArray['name'];  // Result: shubham
    echo "EMail: " . $queryArray['email']; // Result:
    

    Nik

    2,5952 gold badges22 silver badges24 bronze badges

    answered Jul 12, 2019 at 15:46

    _get param from url php

    1

    A much more secure answer that I'm surprised is not mentioned here yet:

    filter_input

    So in the case of the question you can use this to get an email value from the URL get parameters:

    $email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL );

    For other types of variables, you would want to choose a different/appropriate filter such as FILTER_SANITIZE_STRING.

    I suppose this answer does more than exactly what the question asks for - getting the raw data from the URL parameter. But this is a one-line shortcut that is the same result as this:

    $email = $_GET['email'];
    $email = filter_var( $email, FILTER_SANITIZE_EMAIL );
    

    Might as well get into the habit of grabbing variables this way.

    answered Feb 18, 2020 at 17:39

    squarecandysquarecandy

    4,6262 gold badges33 silver badges43 bronze badges

    1

    $uri = $_SERVER["REQUEST_URI"];
    $uriArray = explode('/', $uri);
    $page_url = $uriArray[1];
    $page_url2 = $uriArray[2];
    echo $page_url; <- See the value
    

    This is working great for me using PHP.

    _get param from url php

    answered Jun 27, 2016 at 10:20

    Asesha GeorgeAsesha George

    2,1381 gold badge29 silver badges65 bronze badges

    2

    In Laravel, I'm using:

    private function getValueFromString(string $string, string $key)
    {
        parse_str(parse_url($string, PHP_URL_QUERY), $result);
    
        return isset($result[$key]) ? $result[$key] : null;
    }
    

    _get param from url php

    answered Jul 12, 2019 at 17:14

    Ronald AraújoRonald Araújo

    1,2474 gold badges18 silver badges29 bronze badges

    A dynamic function which parses string URL and gets the value of the query parameter passed in the URL:

    function getParamFromUrl($url, $paramName){
      parse_str(parse_url($url, PHP_URL_QUERY), $op); // Fetch query parameters from a string and convert to an associative array
      return array_key_exists($paramName, $op) ? $op[$paramName] : "Not Found"; // Check if the key exists in this array
    }
    

    Call the function to get a result:

    echo getParamFromUrl('https://google.co.in?name=james&surname=bond', 'surname'); // "bond" will be output here
    

    _get param from url php

    answered Oct 9, 2019 at 10:14

    _get param from url php

    0

    How can I get params in PHP?

    The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

    How do you find the variable in a URL?

    To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.

    What is $_ GET in PHP?

    PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters:

    How do you access the data sent through the URL with the GET method in PHP?

    The data sent by GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using GET method.