Der fik jeg dagens smil
(13 nye indlæg - i en tråd.)
Jeg overgår i øvrigt til Flash med en .NET klient som jeg havde snakket om (har demoen kørende her), så hvis det kan slå lidt koldt vand i blodet hos nogle af parterne, så smider jeg hele koden ind her. Også selvom det sikkert koster mig min ære
Index.php
<?php
require_once("inc/db_open.php");
if($_SESSION['userID']=="") {
header("location: login.php");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HSP Chat</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<div id="wrapper">
<div id="chatContent"></div>
<div id="clientsContent">Vent.... Indlæser...</div>
<div style="clear:both;"></div>
<div id="message">
<form name="message">
<input type="text" name="usrMessage" id="usrMessage" size="50" />
<input type="submit" name="usrSubmit" id="usrSubmit" value="Send" />
</form>
</div>
<div id="errormessage"></div>
<div id="config"><a id="logoutLink" title="Log out" href="#">Logout</a></div>
</div>
<script type="text/javascript">
function get_clients(){
$.ajax({
url: "clientswindow.php",
cache: false,
success: function(html){
$("#clientsContent").html(html);
},
});
}
function get_chat(){
$.ajax({
url: "chatwindow.php",
cache: false,
success: function(html){
if(html.length > 3) {
$("#chatContent").append(html);
}
},
});
}
$(document).ready(function(){
$("#usrSubmit").click(function(){
var clientmsg = $("#usrMessage").val();
$.post("post.php", {message: clientmsg},
function(data){
data = $.trim(data);
if(data=="OK") {
$("#usrMessage").attr("value", "");
$("#errormessage").html("");
} else {
$("#errormessage").html(data);
}
});
get_chat();
return false;
});
$("#logoutLink").click(function(){
$.post("login.php", {logout: "true"},
function(){
window.location.replace("login.php");
});
return false;
});
get_chat();
get_clients();
setInterval (get_chat, 2000);
setInterval (get_clients, 5000);
});
</script>
</body>
</html>
<?php require_once("inc/db_close.php"); ?>
Login.php
<?php
require_once("inc/db_open.php");
if(isset($_POST['logout'])) {
mysql_query("UPDATE Users SET login = 'false' WHERE id = '" . safe_string_escape($_SESSION['userID']) . "'");
session_destroy();
echo "OK";
/*Close database and exit now! */
require_once("inc/db_close.php");
exit;
}
if(isset($_SESSION['userID'])) {
header("location: index.php");
}
if(isset($_POST['user'])) {
$username = safe_string_escape($_POST['user']);
$password = sha1(safe_string_escape($_POST['pass']));
$sql = "SELECT * FROM Users WHERE nickname='". $username . "' AND password='". $password ."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
$cUser = mysql_fetch_assoc($result);
$_SESSION['userID'] = $cUser['id'];
$_SESSION['userNick'] = $cUser['nickname'];
mysql_query("UPDATE Users SET login = 'true' WHERE id = '" . $cUser['id'] . "'");
mysql_query("UPDATE Users SET date = '". date("Ymd H:i:s") . "' WHERE id = '" . $cUser['id'] . "'");
session_write_close();
echo "OK";
$sql = "INSERT INTO Chat (userID, date, toName, fromName, message, style) VALUES ('" . $cUser['id'] . "', '". date("Ymd H:i:s") . "', '', 'Chat', 'Velkommen " . $cUser['nickname'] . "', 'normal')";
mysql_query($sql);
} else {
sleep(1);
echo "Error";
}
/*Close database and exit now! */
require_once("inc/db_close.php");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HSP Chat</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<div id="wrapper">
<div id="login">
<form name="login">
<table border="0">
<tr><td>Brugernavn:</td><td>Password:</td><td></td></tr>
<tr><td><input type="text" name="user" id="user" /></td><td>
<input type="password" name="pass" id="pass" /></td><td><input type="submit" value="Log in" id="btnLogin" /></td></tr>
</table>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#btnLogin").click(function(){
var username = $("#user").val();
var password = $("#pass").val();
$.post("login.php", {user: username, pass: password},
function(data){
data = $.trim(data);
if(data=="OK") {
window.location.replace("index.php");
} else {
alert("Forkert brugernavn / password. Prøv igen ;)");
}
});
return false;
});
});
</script>
</body>
</html>
<?php require_once("inc/db_close.php"); ?>
Post.php
<?php
require_once("inc/db_open.php");
if(!$_SESSION['userID']=="") {
$message = strip_tags($_POST['message'], '<b><i>');
$message = safe_string_escape($message);
$sql = "INSERT INTO Chat (userID, date, toName, fromName, message, style) VALUES ('". $_SESSION['userID'] . "', '". date("Ymd H:i:s") . "', '', '" . $_SESSION['userNick'] . "', '" . $message . "', 'normal')";
mysql_query($sql);
echo "OK";
} else {
echo "Error";
}
require_once("inc/db_close.php");
?>
clientswindow.php
<?php
require_once("inc/db_open.php");
$sql = "SELECT * FROM Users WHERE login = 'true' ORDER BY nickname";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
if(strtotime(date("Ymd H:i:s")) - strtotime($row['date']) > 10) {
$sql = "INSERT INTO Chat (userID, date, toName, fromName, message, style) VALUES ('" . $cUser['id'] . "', '". date("Ymd H:i:s") . "', '', 'Chat', '". $row['nickname'] . " er logget ud.', 'normal')";
mysql_query("UPDATE Users SET login = 'false' WHERE id = '" . $row['id'] . "'");
mysql_query($sql);
} else {
echo $row['nickname'] . "<br />";
}
}
require_once("inc/db_close.php");
?>
Chatwindow.php
<?php
require_once("inc/db_open.php");
mysql_query("UPDATE Users SET date = '". date("Ymd H:i:s") . "' WHERE id = '" . safe_string_escape($_SESSION['userID']) . "'");
if(!isset($_SESSION['messageID'])) {
$row_count = mysql_num_rows(mysql_query("SELECT * FROM Chat"));
if($row_count > 25)
{
$row_count = $row_count - 25;
$sql = "SELECT * FROM Chat ORDER BY id LIMIT ".$row_count.",25";
}
else
{
$sql = "SELECT * FROM Chat ORDER BY id";
}
$result = mysql_query($sql);
$hID = 0;
while($row = mysql_fetch_array($result)) {
echo "<b>" . $row['fromName'] . "</b>: " . $row['message'] . " <br />";
if($row['id'] > $hID) {
$hID = $row['id'];
}
}
$_SESSION['messageID'] = $hID;
} else {
$sql = "SELECT * FROM Chat WHERE id > " . $_SESSION['messageID'] . "";
$result = mysql_query($sql);
$hID = 0;
$count = 0;
while($row = mysql_fetch_array($result)) {
echo "<b>" . $row['fromName'] . "</b>: " . $row['message'] . " <br />";
if($row['id'] > $hID) {
$hID = $row['id'];
}
$count++;
}
if($count > 0) {
$_SESSION['messageID'] = $hID;
}
}
require_once("inc/db_close.php");
?>
Jeg har udeladt db_open og db_close.. Der er ikke noget i dem andet end at åbne databasen, sætte utf-8 og så har db_open en php funktion til at lave en string MySQL sikker.