search

Home  >  Q&A  >  body text

PHP URL encoding/decoding

<p>I used the accepted solution in this question to encrypt the id, for example in <strong>/index.php?id=3</strong>. The problem is that I cannot send the encrypted value as a url like <strong>/index.php?id=dsf13f3343f23/23=</strong>. Because sometimes there are strange characters in the URL, for example, pay attention to the <code>=</code> symbol</p> at the end
P粉512363233P粉512363233510 days ago615

reply all(2)I'll reply

  • P粉214176639

    P粉2141766392023-08-15 10:44:53

    Use PHP's urlencode() function to encode the value before putting it in the URL.

    This function converts "strange" characters, such as =, into a format that is safe to put in the URL. You can use it like this:

    Header('Location: /index.php?id=' . urlencode($id))

    reply
    0
  • P粉346326040

    P粉3463260402023-08-15 09:23:14

    Strange characters in the value passed in the URL should be escaped using urlencode().

    For example, the following code snippet:

    echo urlencode('dsf13f3343f23/23=');

    will give:

    dsf13f3343f23%2F23%3D

    As a URL parameter, this is valid.

    If you want to build a query string with multiple parameters, check out the
    http_build_query() function.

    For example:

    echo http_build_query(array(
        'id' => 'dsf13f3343f23/23=',
        'a' => 'plop',
        'b' => '$^@test', 
    ));

    will give:

    id=dsf13f3343f23%2F23%3D&a=plop&b=%24%5E%40test

    This function will automatically handle escaping and parameter splicing;-)

    reply
    0
  • Cancelreply