Script chat sederhana cara mem buat chatingan di web
langkah awal buat page "index.php" and isi dengan script nie:
<?php
$fileName = 'data.txt';
$bbCode = false;
$perPage = 10;
date_default_timezone_set('gmt +7');
// Show the source
if (isset($_GET['source'])) die(highlight_file(__FILE__));
$author = isset($_POST['author']) ? $_POST['author'] : '_null_';
$message = isset($_POST['message']) ? $_POST['message'] : '_null_';
$curPage = isset($_GET['page']) ? $_GET['page'] : 1;
// Create data file
if (!file_exists($fileName))
{
$createFile = fopen($fileName, 'w');
fclose($createFile);
chmod($fileName, 0640);
}
class writePosts
{
function inputPost($author, $message)
{
global $fileName;
global $bbCode;
$date = time();
// Clean it up
$find = array("'\|'", "'\r\n'");
$replace = array('|', '<br />');
$author = preg_replace($find, $replace, $author);
if ($bbCode)
{
$find = array_merge($find, array("'\[b\](.*?)\[/b\]'i", "'\[i\](.*?)\[/i\]'i", "'\[u\](.*?)\[/u\]'i"));
$replace = array_merge($replace, array('<strong>\\1</strong>', '<em>\\1</em>', '<u>\\1</u>'));
}
$author = htmlentities(trim($author), ENT_NOQUOTES);
$message = htmlentities(trim($message), ENT_NOQUOTES);
$message = preg_replace($find, $replace, $message);
$fh = fopen($fileName, 'r+');
fread($fh, filesize($fileName));
fwrite($fh, $author.'|'.$message.'|'.$date."|\n");
fclose($fh);
}
function getPosts()
{
global $author;
global $message;
if (($author != '' && $message != '') && (strlen($author) < 21 && strlen($message) < 251))
{
if ($author != '_null_' && $message != '_null_')
{
$this->inputPost($author, $message);
header('Location: ?page=1');
}
}
else
{
setcookie('error', 't', time() + 5);
header('Location: '.$_SERVER['REQUEST_URI']);
}
}
}
class readPosts
{
function setDate($t)
{
$s = time() - $t;
if ($s < 60) return 'Baru saja';
$m = floor($s / 60);
if ($m < 60) return $m.' menit'.($m <= 1 ? '' : 's').' Yang lalu';
$h = floor($m / 60);
if ($h < 24) return $h.' Jam'.($h <= 1 ? '' : 's').' Yang lalu';
$d = floor($h / 24);
if ($d < 2) return 'Kemarin, '.(date('h:i A', $t));
if ($d < 7) return $d.' Hari'.($d <= 1 ? '' : 's').' Yang lalu';
$w = round($d / 7);
if ($w <= 2) return $w.' Minggu'.($w <= 1 ? '' : 's').' Yang lalu';
return date('jS M, Y', $t);
}
function readAll()
{
global $fileName;
global $perPage;
global $curPage;
$posts = array_reverse(file($fileName));
$postCount = count($posts);
$amount = ($perPage * $curPage) - $perPage;
$i = $postCount - $amount;
while ($amount < $perPage * $curPage && $amount < $postCount)
{
list($author, $message, $date) = explode('|', $posts[$amount]);
$this->outPost($author, $message, $date, $i);
$amount++;
$i--;
}
}
function showPagination()
{
global $fileName;
global $perPage;
global $curPage;
$postCount = count(file($fileName));
$pages = $postCount != 0 ? ceil($postCount / $perPage) : 1;
$curPage < $pages ? $curPage = $curPage : $curPage = $pages;
$navi = '';
for ($i = 1; $i <= $pages; $i++)
{
$i != $pages ? $sep = ' | ' : $sep = '';
$navi .= $i != $curPage ? '<a href="?page='.$i.'">'.$i.'</a>'.$sep : $i.$sep;
}
echo $navi;
}
function outPost($author, $message, $date, $postNum)
{
$date = $this->setDate($date);
echo '<div class="cont">',"\n";
echo ' <span class="headTxt"><strong>'.$author.'</strong> - '.$date.'</span><span class="subTxt">'.$postNum.'</span>',"\n";
echo ' <br/>',"\n";
echo ' <div class="txt">'.$message.'</div>',"\n";
echo '</div>',"\n";
}
}
$write = new writePosts;
$read = new readPosts;
$write->getPosts();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html >
<head>
<title>Simple Chat X4med Kaskuses</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
<!--
body {
background: url(bg.jpg) 380px no-repeat fixed;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
cursor: default;
}
form {
margin: 0;
}
input[type=text], textarea {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
}
textarea {
width: 95%;
height: 50px;
}
div#error {
color: #ff0000;
padding: 5px;
}
.cont {
border: 2px solid black;
width: 340px;
margin: 0 8px 8px 0;
overflow:hidden
}
.headTxt {
float: left;
padding: 5px;
background-color: #0F0;
width: 100%;
}
.subTxt {
background: #000000 url(corner.gif) no-repeat bottom left;
color: #ffffff;
float: right;
font-weight: bold;
padding: 2px 2px 4px 4px;
}
.txt {
padding: 18px 5px 5px 5px;
}
div.clear {
clear: both;
}
-->
</style>
</head>
<body>
<div class="cont">
<span class="subTxt">X4med Kaskus</span>
<div style="padding: 5px;">
<form action="" method="post">
Nama:<br />
<input name="author" maxlength="20" /><br />
Pesan:<br />
<textarea name="message" rows="1" cols="1" maxlength="250"></textarea><br />
<input type="submit" value="Go" />
</form>
</div>
<?php
if (isset($_COOKIE['error']))
{
echo
'<div id="error">
* Harus Masukan Nama Dan Pesan.<br />
* Nama Hanya 20 Karakter.<br />
* Pesan Hanya 250 Karakter. <br/>
* Piece Gan
</div>',"\n";
}
?>
</div>
<?php $read->readAll() ?>
<div class="cont">
<div style="padding: 5px;">
<strong>
Page: <?php $read->showPagination() ?>
</strong>
</div>
</div>
</body>
</html>1
langkah 2 trus bikin page data.txt pake notepad juga bisa gan n biarin kosong ja
3. index.php n data.txt jadiin satu folder
trus coba deh buka index.php nya and walah.. jadi deh
langkah awal buat page "index.php" and isi dengan script nie:
<?php
$fileName = 'data.txt';
$bbCode = false;
$perPage = 10;
date_default_timezone_set('gmt +7');
// Show the source
if (isset($_GET['source'])) die(highlight_file(__FILE__));
$author = isset($_POST['author']) ? $_POST['author'] : '_null_';
$message = isset($_POST['message']) ? $_POST['message'] : '_null_';
$curPage = isset($_GET['page']) ? $_GET['page'] : 1;
// Create data file
if (!file_exists($fileName))
{
$createFile = fopen($fileName, 'w');
fclose($createFile);
chmod($fileName, 0640);
}
class writePosts
{
function inputPost($author, $message)
{
global $fileName;
global $bbCode;
$date = time();
// Clean it up
$find = array("'\|'", "'\r\n'");
$replace = array('|', '<br />');
$author = preg_replace($find, $replace, $author);
if ($bbCode)
{
$find = array_merge($find, array("'\[b\](.*?)\[/b\]'i", "'\[i\](.*?)\[/i\]'i", "'\[u\](.*?)\[/u\]'i"));
$replace = array_merge($replace, array('<strong>\\1</strong>', '<em>\\1</em>', '<u>\\1</u>'));
}
$author = htmlentities(trim($author), ENT_NOQUOTES);
$message = htmlentities(trim($message), ENT_NOQUOTES);
$message = preg_replace($find, $replace, $message);
$fh = fopen($fileName, 'r+');
fread($fh, filesize($fileName));
fwrite($fh, $author.'|'.$message.'|'.$date."|\n");
fclose($fh);
}
function getPosts()
{
global $author;
global $message;
if (($author != '' && $message != '') && (strlen($author) < 21 && strlen($message) < 251))
{
if ($author != '_null_' && $message != '_null_')
{
$this->inputPost($author, $message);
header('Location: ?page=1');
}
}
else
{
setcookie('error', 't', time() + 5);
header('Location: '.$_SERVER['REQUEST_URI']);
}
}
}
class readPosts
{
function setDate($t)
{
$s = time() - $t;
if ($s < 60) return 'Baru saja';
$m = floor($s / 60);
if ($m < 60) return $m.' menit'.($m <= 1 ? '' : 's').' Yang lalu';
$h = floor($m / 60);
if ($h < 24) return $h.' Jam'.($h <= 1 ? '' : 's').' Yang lalu';
$d = floor($h / 24);
if ($d < 2) return 'Kemarin, '.(date('h:i A', $t));
if ($d < 7) return $d.' Hari'.($d <= 1 ? '' : 's').' Yang lalu';
$w = round($d / 7);
if ($w <= 2) return $w.' Minggu'.($w <= 1 ? '' : 's').' Yang lalu';
return date('jS M, Y', $t);
}
function readAll()
{
global $fileName;
global $perPage;
global $curPage;
$posts = array_reverse(file($fileName));
$postCount = count($posts);
$amount = ($perPage * $curPage) - $perPage;
$i = $postCount - $amount;
while ($amount < $perPage * $curPage && $amount < $postCount)
{
list($author, $message, $date) = explode('|', $posts[$amount]);
$this->outPost($author, $message, $date, $i);
$amount++;
$i--;
}
}
function showPagination()
{
global $fileName;
global $perPage;
global $curPage;
$postCount = count(file($fileName));
$pages = $postCount != 0 ? ceil($postCount / $perPage) : 1;
$curPage < $pages ? $curPage = $curPage : $curPage = $pages;
$navi = '';
for ($i = 1; $i <= $pages; $i++)
{
$i != $pages ? $sep = ' | ' : $sep = '';
$navi .= $i != $curPage ? '<a href="?page='.$i.'">'.$i.'</a>'.$sep : $i.$sep;
}
echo $navi;
}
function outPost($author, $message, $date, $postNum)
{
$date = $this->setDate($date);
echo '<div class="cont">',"\n";
echo ' <span class="headTxt"><strong>'.$author.'</strong> - '.$date.'</span><span class="subTxt">'.$postNum.'</span>',"\n";
echo ' <br/>',"\n";
echo ' <div class="txt">'.$message.'</div>',"\n";
echo '</div>',"\n";
}
}
$write = new writePosts;
$read = new readPosts;
$write->getPosts();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html >
<head>
<title>Simple Chat X4med Kaskuses</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
<!--
body {
background: url(bg.jpg) 380px no-repeat fixed;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
cursor: default;
}
form {
margin: 0;
}
input[type=text], textarea {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
}
textarea {
width: 95%;
height: 50px;
}
div#error {
color: #ff0000;
padding: 5px;
}
.cont {
border: 2px solid black;
width: 340px;
margin: 0 8px 8px 0;
overflow:hidden
}
.headTxt {
float: left;
padding: 5px;
background-color: #0F0;
width: 100%;
}
.subTxt {
background: #000000 url(corner.gif) no-repeat bottom left;
color: #ffffff;
float: right;
font-weight: bold;
padding: 2px 2px 4px 4px;
}
.txt {
padding: 18px 5px 5px 5px;
}
div.clear {
clear: both;
}
-->
</style>
</head>
<body>
<div class="cont">
<span class="subTxt">X4med Kaskus</span>
<div style="padding: 5px;">
<form action="" method="post">
Nama:<br />
<input name="author" maxlength="20" /><br />
Pesan:<br />
<textarea name="message" rows="1" cols="1" maxlength="250"></textarea><br />
<input type="submit" value="Go" />
</form>
</div>
<?php
if (isset($_COOKIE['error']))
{
echo
'<div id="error">
* Harus Masukan Nama Dan Pesan.<br />
* Nama Hanya 20 Karakter.<br />
* Pesan Hanya 250 Karakter. <br/>
* Piece Gan
</div>',"\n";
}
?>
</div>
<?php $read->readAll() ?>
<div class="cont">
<div style="padding: 5px;">
<strong>
Page: <?php $read->showPagination() ?>
</strong>
</div>
</div>
</body>
</html>1
langkah 2 trus bikin page data.txt pake notepad juga bisa gan n biarin kosong ja
3. index.php n data.txt jadiin satu folder
trus coba deh buka index.php nya and walah.. jadi deh
Post a Comment