2015-08-24 41 views
0

考慮我們有一個login.php文件,如下所示:使用echo顯示數據爲javascript危險嗎?

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 
// check fields and if they were correct and they are secure against injection attacks anf if there was the username and password 
echo '<script> javascript code </script>'; 
?> 

可以使用XSS攻擊黑客攻擊的簡單php代碼?

+0

Google這些關鍵字。 –

+0

您無法在php –

+0

中回顯腳本如果您想在_php_和_JavaScript_之間傳遞數據,請使用_JSON_,即使它是生成的源代碼。 –

回答

0
  1. 通過使用php echo或只是一個純html文件顯示javascript/html之間沒有不同。

  2. XSS是你的榜樣風險,如果您在 '迴響' 將直接形式輸入(如後):

WRONG: echo '<script> javascript '.$_POST['username'].' code </script>';

更好: echo '<script> javascript '.aEscapeFuntion($_POST['username']).' code </script>';

有關構建'aEscapeFunction'的更多信息,請參閱this link