Passing base64 encoded strings in URL

前端 未结 10 1641
臣服心动
臣服心动 2020-11-22 03:11

Is it safe to pass raw base64 encoded strings via GET parameters?

10条回答
  •  既然无缘
    2020-11-22 04:02

    Introductory Note I'm inclined to post a few clarifications since some of the answers here were a little misleading (if not incorrect).

    The answer is NO, you cannot simply pass a base64 encoded parameter within a URL query string since plus signs are converted to a SPACE inside the $_GET global array. In other words, if you sent test.php?myVar=stringwith+sign to

    //test.php
    print $_GET['myVar'];
    

    the result would be:
    stringwith sign

    The easy way to solve this is to simply urlencode() your base64 string before adding it to the query string to escape the +, =, and / characters to %## codes. For instance, urlencode("stringwith+sign") returns stringwith%2Bsign

    When you process the action, PHP takes care of decoding the query string automatically when it populates the $_GET global. For example, if I sent test.php?myVar=stringwith%2Bsign to

    //test.php
    print $_GET['myVar'];
    

    the result would is:
    stringwith+sign

    You do not want to urldecode() the returned $_GET string as +'s will be converted to spaces.
    In other words if I sent the same test.php?myVar=stringwith%2Bsign to

    //test.php
    $string = urldecode($_GET['myVar']);
    print $string;
    

    the result is an unexpected:
    stringwith sign

    It would be safe to rawurldecode() the input, however, it would be redundant and therefore unnecessary.

提交回复
热议问题