How to get parameters from a URL string?

前端 未结 13 1486
孤城傲影
孤城傲影 2020-11-22 04:05

I have a HTML form field $_POST[\"url\"] having some URL strings as the value. Example values are:

https://example.com/test/1234?email=xyz@test.com
         


        
13条回答
  •  旧时难觅i
    2020-11-22 04:27

    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.

提交回复
热议问题