Skip to content
Advertisement

URL set as Cookie Value Not Encoding Properly

I am trying to save a URL as the value inside a web browser cookie.

The URL is: https://www.instructables.com/json-api/getIbleStats?id=EVHUTKMJ4OFY92W

I get URL input using this code:

echo '<form action="apibell.php" method="post"><input style="position:absolute; top:0; right:0" type="submit" value="api" placeholder="URL" name="api"></form>';

I set cookie using this PHP code:

 if (isset($_POST["submit"])) {
  $api = urldecode($_POST["api"]);
  setcookie("api", $api);
}

And here’s the outputted cookie. It’s missing part of the URL: -api%2FgetIbleStats%3Fid%3DEVHUTKMJ4OFY92W

Advertisement

Answer

I solved it by re-encoding the URL before setting it as the cookie value:

<?php
    if (isset($_POST["submit"])) {
    $url = urldecode($_POST["url"]);
    $urlcookie = urlencode($url);
    setcookie("url", $urlcookie);
    }
    else if (isset($_COOKIE["url"])) {
    $link = $_COOKIE["url"];
    }
?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement