Skip to content

Instantly share code, notes, and snippets.

@melbahja
Created July 6, 2017 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melbahja/fe680b36a22c2cb00e6c0e2e22022fdf to your computer and use it in GitHub Desktop.
Save melbahja/fe680b36a22c2cb00e6c0e2e22022fdf to your computer and use it in GitHub Desktop.
Safe way to block XSS attacks
<?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