Last active
November 14, 2016 23:06
-
-
Save birowo/a9f521ac516f0059e316afd09deb0670 to your computer and use it in GitHub Desktop.
php ajax longpoll . contoh untuk broadcast message . silahkan ubah var. $rahasia , kode rahasia ini dipakai untuk mencegah merubah nilai $id terakhir di sisi client. silahkan ubah juga host, username & password mysql jika diperlukan.di browser akses : http://localhost/phplongpoll.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
$rahasia = '$r4h@5!4'; | |
$dbname = 'dbname=dbchat'; | |
include 'kueridb.php'; | |
$err = 0; | |
$rslt = kueridb($err, 'SELECT MAX(id) as id FROM chat'); | |
if($err) die("salah kueri: $err"); | |
if(empty($rslt[0]['id'])){ | |
$id = 0; | |
}else if(empty($_COOKIE['id']) || empty($_COOKIE['ck'])){ | |
$id = $rslt[0]['id']; | |
}else{ | |
$id = $_COOKIE['id']; | |
if(hash_hmac('sha1', $id, $rahasia) != $_COOKIE['ck']) die('salah id'); | |
} | |
do{ | |
$rslt = kueridb($err, 'SELECT * FROM chat WHERE id > ?', [$id]); | |
if($err) die("salah kueri: $err"); | |
sleep(1); | |
}while(empty($rslt)); | |
foreach($rslt as $row){ | |
$pesan[]=$row['pesan']; | |
} | |
setcookie('id', $row['id']); | |
setcookie('ck', hash_hmac('sha1', $row['id'], $rahasia)); | |
echo implode('\n', $pesan); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dbname = 'dbname=dbchat'; | |
include 'kueridb.php'; | |
if(empty($_POST['pesan'])) die; | |
$err = 0; | |
kueridb($err, 'INSERT INTO chat(pesan) VALUES (?)', [$_POST['pesan']]); | |
if($err) die("salah kueri: $err"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dbh = new PDO("mysql:host=localhost;$dbname", 'root', ''); | |
function kueridb(&$err, $sql, $param=null){ | |
global $dbh; | |
$sth = $dbh->prepare($sql); | |
$sth->execute($param); | |
$rslt = $sth->fetchAll(); | |
$err = $sth->errorInfo()[1]; | |
//var_dump($err); | |
return $rslt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dbname = ''; | |
include 'kueridb.php'; | |
$sql1 = 'CREATE DATABASE IF NOT EXISTS dbchat'; | |
$sql2 = 'USE dbchat'; | |
$sql3 = 'CREATE TABLE IF NOT EXISTS chat (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, pesan VARCHAR(140) NOT NULL) ENGINE=MEMORY'; | |
$err = 0; | |
kueridb($err, "$sql1;$sql2;$sql3"); | |
if($err) die("salah kueri: $err"); | |
?> | |
<html> | |
<head> | |
<script> | |
function ajax(method, url, kueri, callback){ | |
try{ var xhr=new XMLHttpRequest();}catch(err){ return alert("tidak support ajax");} | |
xhr.onreadystatechange = function(){ | |
if(4 == xhr.readyState && 200 == xhr.status){ | |
callback&&callback(xhr.responseText); | |
} | |
} | |
if(-1 != "GET,DELETE".indexOf(method)){ | |
if(kueri) url += "?"+kueri; | |
kueri=null; | |
} | |
xhr.open(method, url); | |
kueri&&xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
xhr.send(kueri); | |
} | |
</script> | |
</head> | |
<body> | |
<textarea id="daftarpesan" readonly></textarea><br> | |
<input id="pesan"><br> | |
<button onclick="kirim()">kirim</button> | |
<script> | |
daftarpesan.value=""; pesan.value=""; pesan.focus(); | |
function longpoll(){ | |
ajax("GET", "/chatlongpoll.php", "v="+(new Date()).getTime().toString(36), function(respon){ | |
daftarpesan.value = respon + "\n" + daftarpesan.value; | |
pesan.value=""; | |
pesan.focus(); | |
longpoll(); | |
}); | |
} | |
longpoll(); | |
function kirim(){ | |
ajax("POST", "/chatxhr.php", "pesan="+pesan.value); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
revisi terakhir , akses browser jadi : http://localhost/phplongpoll.php