Jaja.
<?php
//http://www.eksperten.dk/spm/835426
session_start();
//Forbinder til databasen
require_once('connect.php');
//Indstillinger
$md5_salt = 'XXX'; // Tilføj noget tekst...
if(isset($_POST['action'])){
switch(strtolower($_POST['action'])){
case 'login':
sleep(1);
if(isset($_POST['username']) && isset($_POST['password'])){
$username = mysql_real_escape_string($_POST['username']);
$password = md5($md5_salt . $_POST['password'] . $md5_salt);
$query = mysql_query("SELECT id FROM tbl_835426_user WHERE username = '".$username."' AND password = '".$password."' AND active = 1 LIMIT 1;");
if(mysql_num_rows($query) == 1){
$row = mysql_fetch_assoc($query);
$_SESSION['chat_logged_in'] = true;
$_SESSION['chat_user_id'] = $row['id'];
$_SESSION['chat_username'] = $username;
}
}
break;
case 'logout':
$_SESSION = array();
session_destroy();
break;
case 'update':
$response = array();
if(isset($_SESSION['chat_logged_in']) && $_SESSION['chat_logged_in']){
$response['authorized'] = true;
if(isset($_SESSION['chat_last_updated'])){
$query = mysql_query("SELECT u.username, c.message, c.added FROM tbl_835426_user u INNER JOIN tbl_835426_chat c ON u.id = c.user WHERE UNIX_TIMESTAMP(c.added) > '".$_SESSION['chat_last_updated']."' ORDER BY c.added DESC LIMIT 50") or die(mysql_error());
}else{
$query = mysql_query("SELECT u.username, c.message, c.added FROM tbl_835426_user u INNER JOIN tbl_835426_chat c ON u.id = c.user ORDER BY c.added DESC LIMIT 10") or die(mysql_error());
}
$messages = array();
if(mysql_num_rows($query) > 0){
while($row = mysql_fetch_assoc($query)){
$messages[] = array('user' => $row['username'], 'time' => $row['added'], 'message' => $row['message']);
}
$response['messages'] = array_reverse($messages);
$_SESSION['chat_last_updated'] = time();
}
}else{
$response['authorized'] = false;
}
echo json_encode($response);
exit();
break;
case 'insert':
$response = array();
if(isset($_SESSION['chat_logged_in']) && $_SESSION['chat_logged_in']){
$response['authorized'] = true;
$message = mysql_real_escape_string($_POST['message']);
$query = mysql_query("INSERT INTO tbl_835426_chat(user,message,added) VALUES('".$_SESSION['chat_user_id']."','".$message."',NOW())");
if(mysql_affected_rows() == 1){
$response['inserted'] = true;
}else{
$response['inserted'] = false;
}
}else{
$response['authorized'] = false;
}
echo json_encode($response);
exit();
break;
default:
$_SESSION['status'] = 'Wrong Action, FOOL...';
}
header('Location: 835426exp.php');
exit();
}
//Er vi logget ind ?
if($_SESSION['chat_logged_in']){
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; echo '<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
echo '<head>';
echo '<title>Jakobdo Chat v0.1</title>';
echo '<script type="text/JavaScript" src="835426exp.js"></script>';
echo '<style type="text/css">';
echo 'div#content {background-color: #cc9; border: 1px dotted #333; padding: 2px; margin: 0px auto;}';
echo 'div#content form {margin:0px; padding:0px;}';
echo 'div#chatWindow {width: 500px; height: 300px; background-color: white; color: black; border: 2px solid black;overflow: auto;}';
echo 'div.row {clear: both;}';
echo 'div.row span.label {float: left; width: 100px; text-align: right;}';
echo 'div.row span.formw {float: right; width: 235px; text-align: left;}';
echo 'div.spacer {clear: both;}';
echo '</style>';
echo '</head>';
echo '<body onload="startUpdate();">';
echo '<div id="content">';
echo '<form action="835426exp.php" method="post">';
echo '<div class="row">';
echo '<span>Jakobdo Chat v0.1</span>';
echo '</div>';
echo '<div class="row">';
echo '<div id="chatWindow"></div>';
echo '</div>';
echo '<div class="row">';
echo '<div id="chatText"><input type="text" id="message" /> <input id="insert" type="button" accesskey="s" onclick="insertMessage()" value="Send" size="25" /></div>';
echo '</div>';
echo '<div class="row">';
echo '<input type="submit" name="action" value="Logout" size="25" />';
echo '</div>';
echo '<div class="spacer"> </div>';
echo '</form>';
echo '</div>';
echo '</body>';
echo '</html>';
}else{
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; echo '<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
echo '<head>';
echo '<title>Jakobdo Chat v0.1</title>';
echo '<style type="text/css">';
echo 'div#content {width: 350px; background-color: #cc9; border: 1px dotted #333; padding: 5px; margin: 0px auto;}';
echo 'div.row {clear: both; padding-top: 10px;}';
echo 'div.row span.label {float: left; width: 100px; text-align: right;}';
echo 'div.row span.formw {float: right; width: 235px; text-align: left;}';
echo 'div.spacer {clear: both;}';
echo '</style>';
echo '</head>';
echo '<body>';
echo '<div id="content">';
echo '<form action="835426exp.php" method="post">';
echo '<div class="row">';
echo '<span class="label">Username:</span>';
echo '<span class="formw"><input type="text" name="username" size="25" /></span>';
echo '</div>';
echo '<div class="row">';
echo '<span class="label">Password:</span>';
echo '<span class="formw"><input type="password" name="password" size="25" /></span>';
echo '</div>';
echo '<div class="row">';
echo '<span class="label"> </span>';
echo '<span class="formw"><input type="submit" name="action" value="Login" size="25" /></span>';
echo '</div>';
echo '<div class="spacer"> </div>';
echo '</form>';
echo '</div>';
echo '</body>';
echo '</html>';
}
?>