Use urlencode() or rawurlencode() .
You wrote:
I am trying to get the URL as a parameter from a user and redirect the user to that URL.
But you did not answer the question - how to get this URL? How does the user provide this to you? Is this written in <input type='text'/> ? Or does the user click on the link containing the URL as one of the parameters? Or is it passed as the identifier of some URL that is stored in the database?
One case that comes to my mind is replacing the URLs in text form and sending the user to some โredirect pageโ before opening the real page, so the last page does not display the HTTP referrer, which may contain some protected data (for example, the identifier session), in which case you should write
<a href='redirect.php?link=<?php echo rawurlencode($url); ?>'> <?php echo htmlspecialchars($url); ?> </a>
instead
<a href='redirect.php?link=<?php echo $url; ?>'> <?php echo htmlspecialchars($url); ?> </a>
binaryLV
source share