I'm making a chat app, and I'm using a .htaccess
file to prevent external sources from reading messages. However, this also blocks the JavaScript from sending messages. My JavaScript is:
$("#sendMsg").onclick(function(){
$.post('post.php',
{ msg : $("#msgBox").val(), user : getCookie('username')},
function(result) {alert(result);});
});
My PHP for post.php
is:
<?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;
?>
EDIT: The .htaccess
file has:
order deny, allow
<FilesMatch "post.php">
deny from all
</FilesMatch>
If there is no .htaccess
file, anyone can send a POST request to the post.php
file to add messages, but if there is a .htaccess
file, the JavaScript can't send the POST requests either. Does anyone have a good solution for this?