Given this PHP code:
<a onclick="javascript:window.location.href='<?php echo $url;?>'"
What if there is a '
in $url?
I tried using json_encode($url)
but it won't be able to handle this.
Given this PHP code:
<a onclick="javascript:window.location.href='<?php echo $url;?>'"
What if there is a '
in $url?
I tried using json_encode($url)
but it won't be able to handle this.
json_encode
will work. You just have to use it the right way:
<a onclick="javascript:window.location.href=<?php echo htmlspecialchars(json_encode($url)); ?>">
This will work since json_encode
already returns an JavaScript expression with quotes. And htmlspecialchars
is needed to escape possible HTML meta characters.