Szenvedek egy ideje egy AJAX chatel de valami nem stimmel valamiért.
HTML kód:<!--
//Browser Support Code
var c = 0;
var start = 0;
var last = "";
function ajaxFunction(msg){
if ( msg !== undefined ) {
last = msg;
}
document.getElementById("x") == "";
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
if (msg == undefined) {
document.getElementById('chat').innerHTML = ajaxRequest.responseText;
var objDiv = document.getElementById("chat");
objDiv.scrollTop = objDiv.scrollHeight;
}
if (msg !== undefined) {
if (ajaxRequest.responseText == last) {start=0; }
}
}
}
if (msg == undefined ) {
ajaxRequest.open("GET", "chat.php?p="+Math.random(), true);
ajaxRequest.send(null);
}
else
{
var params = "msg="+encodeURIComponent(msg);
ajaxRequest.open("POST", "chat.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(params);
ajaxFunction();
start=1;
}
setTimeout("ajaxFunction()", 6000);
}
function sure(){
if (confirm("Posting Failed, Resend last message?")) {
ajaxFunction(last);
} else {
document.getElementById("x").value=last;
}
}
function timer(){
if (start == 1) {
c = c+1;
if (c > 5 ) { c=0; start=0; sure(); }
}
}
setInterval("timer()", 1000);
function online(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById('online').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "chat.php?do=online&x="+Math.random(), true);
ajaxRequest.send(null);
setTimeout("online()", 10000);
}
//-->
function enter_pressed(e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return false;
return (keycode == 13);
}
ajaxFunction();
online();
A PHP része meg ha POST ot kap akkor simán vissza küldi amit kapot, az ajax a response ben össze hasonlítja a küldöttet a kapottal és ha passzolnak akkor az azt jelenti, hogy megkapta, ha meg nem passzol akkor rákérdez, hogy megpróbálja-e még1x.
A php rész meg ez lenne
PHP kód:
<?php
ob_start("ob_gzhandler");
session_start();
if (!isset($_SESSION['user'])) {
die('NOT LOGGED IN!');
}
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
// MySQL Settings
$dbhost = 'localhost';
$dbuser = '******';
$dbpass = '******';
$db = '******';
$dbtable = 'chat';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Database Erorr');
mysql_select_db($db);
// END Mysql
//Variables
$do = $_GET["do"];
$num = $_GET["n"];
$msg = $_POST["msg"];
$user = $_SESSION["user"];
// END Variables
mysql_query("UPDATE userso SET time = now() WHERE user = '".$user."'");
$query = "Select user from userso where time > now()-30";
$result = mysql_query($query);
while( $row = mysql_fetch_array($result)){
$online = $online ." ". $row["user"];
}
if ($do == 'online' )
{
echo "Online: $online ";
mysql_close($conn);
die();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) { echo "$msg"; }
if (isset($msg)) {
$mit = array(
':)',
':(',
';)',
':D',
';;)',
'>:D<',
':-/',
':x',
':">',
':P',
':-*',
'=((',
':-O',
'X(',
':>',
'B-)',
':-S',
'#:-S',
'>:)',
':((',
':))'
);
$mire = array(
'<img src="sm/1.gif">',
'<img src="sm/2.gif">',
'<img src="sm/3.gif">',
'<img src="sm/4.gif">',
'<img src="sm/5.gif">',
'<img src="sm/6.gif">',
'<img src="sm/7.gif">',
'<img src="sm/8.gif">',
'<img src="sm/9.gif">',
'<img src="sm/10.gif">',
'<img src="sm/11.gif">',
'<img src="sm/12.gif">',
'<img src="sm/13.gif">',
'<img src="sm/14.gif">',
'<img src="sm/15.gif">',
'<img src="sm/16.gif">',
'<img src="sm/17.gif">',
'<img src="sm/18.gif">',
'<img src="sm/19.gif">',
'<img src="sm/20.gif">',
'<img src="sm/21.gif">'
);
$msg= str_replace($mit, $mire, $msg);
mysql_query("INSERT INTO $dbtable (nick,message) VALUES ('".$_SESSION["user"]."','$msg')");
if ($msg == '/clear' ) { mysql_query("TRUNCATE TABLE $dbtable;"); }
}
function lst()
{
$query = "SELECT * FROM (SELECT * FROM chat ORDER BY time DESC LIMIT 25 ) AS ttbl ORDER BY time ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
if ($_SESSION['user'] == $row['nick'] ) $color = "white"; else $color= "black";
echo "<span style=\"font-size:18px; color:$color\">[" .substr($row['time'],11,8). "] " . $row['nick']. " : ". $row['message'] . "</span>";
echo "<br />";
}
}
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) { lst(); }
mysql_close($conn);
?>
ADSL kapcsolattal tökéletesen működik, viszont ezt éppen 3G re szeretném ami nagyon ingadozik és sokszor nem ér el a POST, de érdekes módon 5-6x is elküldi ugyan azt, és nem látom hogy mi a hiba.
[OFF] PHP : Hogyan lehet azt elérni, hogy a smileikat helyesen cserélje ki, es ne keljen átrendezni pl:
Ez így kéne kinézzen:Kód:Hello :))
Hello http://l.yimg.com/us.yimg.com/i/mesg/emoticons7/21.gif
Viszont egyszer kicseréli a
erre : http://l.yimg.com/us.yimg.com/i/mesg/emoticons7/1.gifKód::)
és a ) marad, eredmény:
Hello http://l.yimg.com/us.yimg.com/i/mesg/emoticons7/1.gif)
Köszönöm
* XSS / SQLi most még nem fontos.