-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made the system less complex and reduced the code size #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,55 +3,57 @@ | |
* minimicrochat.php - the simplest chat server I could write in two hours | ||
*/ | ||
$db = createdb(); | ||
if (isset($_POST['yo']) && $_POST['yo']) { sendmessage($db,$_POST['yo']); } | ||
$res = getmessages($db, 25); | ||
$msgs = $res->fetchAll(); | ||
if ( ! empty($_POST['yo'])) { sendmessage($_POST['yo']); } | ||
?><!doctype html> | ||
<html lang="en"> | ||
<head><meta charset="utf-8"><title>yo</title></head> | ||
<body onload="document.yo.yo.focus();" style="font-size: 12px; font-family: helvetica, sans-serif;"> | ||
<ul> | ||
<?php if (count($msgs)) { foreach ($msgs as $msg) { ?> | ||
<?php foreach (getmessages(25) as $msg) { ?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You sure this will work ok in the empty dataset case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it works fine. |
||
<li> | ||
<span style="color: rgb(<?php print($msg['rgb']); ?>);">Yo: </span> | ||
<?php print(htmlspecialchars($msg['message'])) ?> | ||
<span style="color: rgb(<?php print($msg['rgb']); ?>);">Yo: </span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation in the rest of the file is 4 spaces... are you changing to tabs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this whole file is now tabs. I'm used to other languages so PHP's spaces got converted by my editor. |
||
<?php print(htmlspecialchars($msg['message'])) ?> | ||
</li> | ||
<?php }} ?> | ||
<?php } ?> | ||
</ul> | ||
<form method="post" name="yo"> | ||
<input style="width: 80%;" type="text" autocomplete="off" name="yo" /> | ||
<button type="submit">yo</button> | ||
</form></body></html> | ||
<?php | ||
// lib | ||
|
||
function db() { | ||
static $db; | ||
$db = $db ?: (new PDO('sqlite:yo.sqlite3', 0, 0, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION))); | ||
return $db; | ||
} | ||
|
||
function query($sql, $params = NULL) { | ||
$s = db()->prepare($sql); | ||
$s->execute(array_values((array) $params)); | ||
return $s; | ||
} | ||
|
||
function createdb() { | ||
$dayhash = md5(date('Ymd')); | ||
$db = new PDO('sqlite:yo.sqlite3'); | ||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
$db->exec("CREATE TABLE IF NOT EXISTS messages_$dayhash ( | ||
id INTEGER PRIMARY KEY, | ||
rgb TEXT, | ||
message TEXT)"); | ||
return $db; | ||
$dayhash = md5(date('Ymd')); | ||
db()->exec("CREATE TABLE IF NOT EXISTS messages_$dayhash ( | ||
id INTEGER PRIMARY KEY, | ||
rgb TEXT, | ||
message TEXT)"); | ||
} | ||
function sendmessage($db, $msg) { | ||
$dayhash = md5(date('Ymd')); | ||
$insert = "INSERT INTO messages_$dayhash (rgb, message) | ||
VALUES (:rgb, :message)"; | ||
$stmt = $db->prepare($insert); | ||
$stmt->bindParam(':message', $msg); | ||
$rgb = rgbfromip(); | ||
$stmt->bindParam(':rgb', $rgb); | ||
$stmt->execute(); | ||
|
||
function sendmessage($msg) { | ||
$dayhash = md5(date('Ymd')); | ||
query("INSERT INTO messages_$dayhash (rgb, message) | ||
VALUES (?, ?)", array(rgbfromip(), $msg)); | ||
} | ||
|
||
function getmessages($db, $num=25) { | ||
$dayhash = md5(date('Ymd')); | ||
$stmt = $db->prepare("SELECT * FROM messages_$dayhash | ||
ORDER BY id DESC LIMIT :num"); | ||
$stmt->bindParam(':num', $num); | ||
return $stmt->execute(); | ||
$dayhash = md5(date('Ymd')); | ||
return query("SELECT * FROM messages_$dayhash ORDER BY id DESC LIMIT :num", array($num)); | ||
} | ||
|
||
function rgbfromip() { | ||
$h = array_map('ord', str_split(md5($_SERVER['REMOTE_ADDR'], true))); | ||
return "$h[0],$h[1],$h[2]"; | ||
} | ||
$h = array_map('ord', str_split(md5($_SERVER['REMOTE_ADDR'], true))); | ||
return "$h[0],$h[1],$h[2]"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does using empty() prevent both a PHP notice if the value isn't set AND prevent against calling sendmessage on "" values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, empty does that.