Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a string safe for an href attribute using PHP?

Would encoding quotation marks and removing eventual javascript: prefixes be enough?

P.S. Safe enough to defeat XSS attacks that is.

like image 448
Emanuil Rusev Avatar asked Mar 25 '26 04:03

Emanuil Rusev


1 Answers

you can use the php function to validate urls

$url = "http://google.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
  echo "URL is valid";
}
else {
  echo "URL is invalid";
}
like image 64
Michael Valentino Avatar answered Mar 26 '26 17:03

Michael Valentino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!