Created
July 6, 2017 16:02
-
-
Save melbahja/fe680b36a22c2cb00e6c0e2e22022fdf to your computer and use it in GitHub Desktop.
Safe way to block XSS attacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$q = $_GET['query'] ?? null; | |
if ($q !== null) { | |
$q = htmlspecialchars(strip_tags($q), ENT_QUOTES, "UTF-8"); | |
} | |
// data to browser | |
$data = json_encode(['q' => $q]); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>test</title> | |
</head> | |
<body> | |
<h2 id="searchFor"> results for: <span></span> </h2> | |
<script type="text/javascript" src="jquery.js"></script> | |
<script type="text/javascript"> | |
$('#searchFor span').text( | |
$.parseJSON('<?=$data;?>').q | |
); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment