chat.php
Index.php:
Postchat.php
SQL:
Old code, used on my old website.
Code:
? ? ? ?<?php
? ?$servername="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
? ? ? ? ? ?// Create connection
$conn = new mysqli($servername, $username, $password, "dbname");
// Check connection
if ($conn->connect_error) {
? ?die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"UTF-8");
$sql2 = "SELECT * FROM `chat` ORDER BY id ASC";
$result3 = $conn->query($sql2);
if ($result3->num_rows > 0) {
? ?// output data of each row
? ?while($row = $result3->fetch_assoc()) {
? ? ? ?if($row['is_admin']=="TRUE"){ $admin="<img src='../img/1.png' width='12px'/>"; } else { $admin=""; }
? ?echo "<div class='message'>
? ?$admin <font style='" . htmlentities("$row[color]") . "'>" . htmlentities("$row[by_user]") . "</font>: " . htmlentities($row['message']) . "
</div>";
}
? ?}
? ? ? ??>
Code:
<div class="chatbox">
? ? ? ?<div class="messages">
? ? ? ? ? ?
? ? ? ?</div>
? ? ? ?<form id="chat" method="post">
? ? ? ? ? ?<input type="text" name="message" id="cmsg" required placeholder="Your message here" autocomplete="off"/><input type="submit" style="display:none;" id="submithid">
? ? ? ?</form>
? ?</div>
? ?<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
? ?<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
? ?<script type="text/javascript" >
? ? ?var auto_refresh = setInterval(
function() {
?var wtf ? ?= $('.messages');
?var height = wtf[0].scrollHeight;
?wtf.scrollTop(height);
}, 500);
? ? ? ?var auto_refresh = setInterval(
function ()
{
$('.messages').load('chat.php').fadeIn("slow");
}, 200);
? ? ?$(function () {
? ? ? ?$('#chat').bind('submit', function () {
? ? ? ? ?$.ajax({
? ? ? ? ? ?type: 'post',
? ? ? ? ? ?url: 'postchat.php',
? ? ? ? ? ?data: $('#chat').serialize(),
? ? ? ? ? ?success: function () {
? ? ? ? ? ? ? ?document.getElementById('cmsg').value='';
? ? ? ? ? ?}
? ? ? ? ?});
? ? ? ? ?return false;
? ? ? ?});
? ? ?});
? ? ?</script>
Postchat.php
Code:
<?php
? ? ? ? ? ? ? ? ? ?$rand=rand(1, 255);
? ? ? ? ? ? ? ? ? $rand2=rand(1, 255);
? ? ? ? ? ? ? ? ? $rand3=rand(1, 255);
? ? ? ? ? ? ? ?$servername="localhost"; // Host name
? ? ? ? ? ? ? ?$username="root"; // Mysql username
? ? ? ? ? ? ? ?$password=""; // Mysql password
? ? ? ? ? ? ? ?$db_name="dbname"; // Database name ?
? ? ? ? ? ? ? ?// Connect to server and select databse.
? ? ? ? ? ? ? ?mysql_connect("$host", "$username", "$password")or die("cannot connect");
? ? ? ? ? ? ? ?mysql_select_db("$db_name")or die("cannot select DB");
? ? ? ? ? ? ? ?$sql="INSERT INTO `chat` (`message`,`by_user`,`is_admin`,`color`) VALUES ('$_POST[message]','username','false','color:rgba($rand,$rand2,$rand3,1.0);')";
? ? ? ? ? ? ? ?mysql_query($sql);
? ? ? ? ? ? ? ?mysql_close();
?>
SQL:
Code:
-- phpMyAdmin SQL Dump
-- version 4.0.10.14
-- http://www.phpmyadmin.net
--
-- V?rt: localhost:3306
-- Genereringstid: 30. 07 2016 kl. 18:24:21
-- Serverversion: 5.5.45-cll-lve
-- PHP-version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `dbname`
--
-- --------------------------------------------------------
--
-- Struktur-dump for tabellen `chat`
--
CREATE TABLE IF NOT EXISTS `chat` (
?`id` int(255) NOT NULL AUTO_INCREMENT,
?`message` longtext NOT NULL,
?`by_user` longtext NOT NULL,
?`is_admin` longtext NOT NULL,
?`color` longtext NOT NULL,
?PRIMARY KEY (`id`)
) ENGINE=MyISAM ?DEFAULT CHARSET=latin1 AUTO_INCREMENT=198 ;
--
-- Data dump for tabellen `chat`
--
INSERT INTO `chat` (`id`, `message`, `by_user`, `is_admin`, `color`) VALUES
(197, 'HALLO', 'KinJacob', 'TRUE', 'color:rgba(195,167,168,1.0);');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Old code, used on my old website.