0

I'm doing a simple chat and everything is working well, but everybody can flood the chat sending how many messages they want to...

PHP:

<div id="chat_panel"></div>
<br />
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="text" autocomplete="off" maxlength="50" placeholder="Type your message..." id="message" size="87%" checked="yes"></td>
<td><input type="button" id="message" onClick="sendMessage()" value="Send"></td>
</tr>
</table>

JAVASCRIPT:

function sendMessage() {
var message = $('#message').val();
$.post('postMessage.php', { message: message } , function() { } );
}

postMessage.PHP:

session_start();
include ("chats/connect.php");
$timname = $_SESSION['username'];
if (isset($_POST['message'])) {
mysql_query("INSERT INTO chat VALUES ('', '".$timname."', '".$_POST['message']."', '".time()."', NOW(), '');");
}

I need to set a interval to send each message like 3 seconds... Any information will be helpful!

user3050478
  • 272
  • 3
  • 19
  • 1
    [`mysql_*` is deprecated.](http://www.php.net/manual/en/intro.mysql.php), use either [mysqli](http://us1.php.net/mysqli) or [PDO](http://us3.php.net/PDO), and your code is vulnerable to [SQL injection attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). –  Dec 24 '13 at 11:43
  • Do you want to wait on server or client side? On serverside you can use the `sleep()` function. http://php.net/sleep – veelen Dec 24 '13 at 11:49
  • thanks André! I'll remember that! – user3050478 Dec 24 '13 at 11:59
  • Thanks veelen for your answer! I want just an interval to each person on chat can't send a lot of messages... An interval of 3 seconds to send each message... The sleep function could help me someway? – user3050478 Dec 24 '13 at 12:00

1 Answers1

1

Try Jquery to refresh specific page. Try something like below code

setup.js

var autorefresh=setInterval(
function()
{
    $("query.php").load('query.php');   
    e.preventDefault();
},3000);

query.php

session_start();
include ("chats/connect.php");
$timname = $_SESSION['username'];
if (isset($_POST['message'])) {
mysql_query("INSERT INTO chat VALUES ('', '".$timname."', '".$_POST['message']."', '".time()."', NOW(), '');");
}

Here 3000 in setup.js is time in miliseconds you want to refresh.

Gopal Joshi
  • 2,350
  • 22
  • 49
  • thanks for your answer! I've tried that way but it doesn't worked here :/ Does the sleep function could help me someway? – user3050478 Dec 24 '13 at 11:58
  • 1
    This it perface way to refresh perticuler php file. Have you created query.php file on the root of setup.js file? – Gopal Joshi Dec 24 '13 at 12:00
  • sorry, should I unlink the postMessage.js? – user3050478 Dec 24 '13 at 12:02
  • doesn't worked here... if I unlink the postMessage.js I can't send any message couse of the function sendMessage()... and even if the query.php is on the root of setup.js it doesn't works. – user3050478 Dec 24 '13 at 12:08