我在製作一個聊天應用程序,並使用.htaccess
文件來阻止外部消息來源閱讀消息。但是,這也阻止了JavaScript發送消息。我的JavaScript是:使用JavaScript安全地發佈消息
$("#sendMsg").onclick(function(){
$.post('post.php',
{ msg : $("#msgBox").val(), user : getCookie('username')},
function(result) {alert(result);});
});
我對post.php
PHP是:
<?php
$message = $_POST['msg'];
$user = $_POST['user'];
$servername = "localhost:3306";
$username = "user";
$password = "pass";
$dbname = "dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "INSERT INTO tablename (username, message) VALUES ('" . $user . "', '" . $message . "'); SELECT * FROM tablename";
$result = mysqli_query($conn, $sql);
$numofmessages = 0
while($row = mysqli_fetch_assoc($result)) {
$numofmessages++;
echo $numofmessages;
?>
編輯:的.htaccess
文件有:
order deny, allow
<FilesMatch "post.php">
deny from all
</FilesMatch>
如果沒有.htaccess
文件,任何人都可以發送POST請求到post.php
文件以添加消息,但是如果有一個.htaccess
文件,JavaScript也不能發送POST請求。有沒有人有這個好的解決方案?
那麼,沒有你在'.htaccess'風格文件中發佈規則,我們就無能爲力...... – arkascha
你如何驗證用戶?你爲什麼不呢? –
不相關,但您的代碼易受sql注入影響。見[這裏](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)如何保護自己免受它。 – Sumurai8