Assignment: Create a Simple Chat Application using AJAX, PHP, MYSQL, SQL and Jav
ID: 3816481 • Letter: A
Question
Assignment: Create a Simple Chat Application using AJAX, PHP, MYSQL, SQL and Javascript. Does not matter what kind of chat.
thank you in advance
Explanation / Answer
class noscript_chat { // Change these values to match your MySQL database details. const noscript_host = "localhost"; const noscript_username = "root"; const noscript_password = "password"; // If the details are correct it will install the database and table. // No need to edit below this line. // Sorry for lack of comments in some parts const noscript_database = "noscript"; const noscript_table = "chat"; public $name; public $message; public function noscript_connect() { return mysql_connect(self::noscript_host,self::noscript_username,self::noscript_password); } public function noscript_select() { return mysql_select_db(self::noscript_database,$this->noscript_connect()); } // Gets the latest message position ($number) private function start_position() { $pointer = mysql_fetch_array(mysql_query("SELECT `number` FROM `".self::noscript_table."` ORDER BY `number` DESC LIMIT 0,1;")); return $pointer["number"]; } // Uses the latest known message position ($number) and checks if there are any newer messages, // if so it echos them, and returns the latest message position. private function get_chat($number) { $latest = @mysql_query("SELECT `number`,`name`,`message` FROM `".self::noscript_table."` WHERE `number` > ".$number.";"); while($stream = @mysql_fetch_array($latest)) { echo "".$stream["name"].": ".htmlspecialchars($stream["message"],ENT_QUOTES).""; $number = $stream["number"]; } return $number; } public function stream_chat() { @apache_setenv('no-gzip', 1); @ini_set('zlib.output_compression', 0); @ini_set('implicit_flush', 1); for ($i = 0; $i start_position(); // Infinite loop while(1) { // Get the latest messages and update the position $number = $this->get_chat($number); // Flush (send) the messages to the browser flush(); // Wait 0.25 seconds usleep(250000); } } // Insert a message into the table public function insert_message() { return mysql_query("INSERT INTO `".self::noscript_table."` (`name`,`message`) VALUES ('".mysql_escape_string($this->name)."','".mysql_escape_string($this->message)."');"); } // Used for installing the database and table if they don't exist private function create_database() { return mysql_query("CREATE DATABASE IF NOT EXISTS `".self::noscript_database."` ;"); } private function create_table() { return mysql_query("CREATE TABLE IF NOT EXISTS `".self::noscript_table."` (`number` int(4) NOT NULL AUTO_INCREMENT, `name` varchar(12) NOT NULL, `message` varchar(200) NOT NULL, PRIMARY KEY (`number`) ) ENGINE=MyISAM DEFAULT CHARSET=ascii AUTO_INCREMENT=1 ;"); } public function install_noscript() { $this->create_database(); $this->noscript_select(); $this->create_table(); } } // Start of page header("Content-type: text/html; charset=ASCII"); $request = @$_GET["request"]; $name = @$_POST["name"]; $noscript = new noscript_chat; if($request) { $noscript->noscript_select(); if($request=="stream") { set_time_limit(0); // Blank div for browsers that require 2k bytes for flush to work (Internet explorer and older versions of Chrome). echo "".str_repeat(" ", 2010).""; $noscript->stream_chat(); } elseif($request=="messenger") { @session_start(); if(@$_POST["message"]!=null) { $noscript->name = $_SESSION["noscript_name"]; $noscript->message = $_POST["message"]; $noscript->insert_message(); } echo "".$_SESSION["noscript_name"].": "; } } elseif($name) { @session_start(); $_SESSION["noscript_name"] = $name; echo " "; } else { $noscript->noscript_connect(); $noscript->install_noscript(); echo "Username: "; } ?>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.