Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP MySQL real time chat application without JavaScript

Featured Replies

This was originally a small side project I made a while ago, purely just to show that chat applications can be made without Javascript or any other components such as Java or Flash. It’s not being used, so it will be good to share it on here.

 

Screenshot:

post-21310-0-26701300-1353269175_thumb.jpg

 

Things to note:

  • This works by http streaming the chat through an iframe.
  • This will stop working if the user choses to stop loading the page.
  • This has only been tested locally on XAMPP.
  • This script may be a bit buggy, as it hasn’t been rigorously tested.
  • Uses the minimum amount of validation required for it to work.
  • The naming of the php file does not matter.
  • The chat frame will not automatically scroll.

I recommend that you don’t use this on your website, as there are better ways to do real time chat. Only use this as guidance/reference/help if you chose to make a real time PHP application.

 

Have fun trying this out, this will work out the box; all you need to do is change the database details at the top of the page, and make sure your http server and MySQL are running.

 

<?php
// Real time chat application without the use of Javascript
// CC http://creativecommons.org/licenses/by-sa/3.0/ by Barth
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 "<div style='float:left;width:270px;'>".$stream["name"].": ".htmlspecialchars($stream["message"],ENT_QUOTES)."<br /><br /></div>";
 $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 < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

$number = $this->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 "<div style='visibility:hidden;'>".str_repeat(" ", 2010)."</div>";
$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 "<form action='' method='post'>".$_SESSION["noscript_name"].": <input type='text' name='message' maxlength='200' /><input type='submit' value='Send'></form>";
}
}
elseif($name) {
@session_start();
$_SESSION["noscript_name"] = $name;
echo "<div style='margin:auto;width:300px;height:500px;'><iframe src='?request=stream' frameborder='0' scrolling='yes' style='float:left;width:300px;height:450px;'>
</iframe><iframe src='?request=messenger' frameborder='0' scrolling='no' style='float:left;width:300px;height:50px;'></iframe></div>";
}
else {
$noscript->noscript_connect();
$noscript->install_noscript();
echo "<form action='' method='post'>Username: <input type='text' name='name' maxlength='15' /><input type='submit' value='Join'></form>";
}
?>

Mmm i would not even suggest this to even guide anyone. Look u even got jock in a state of shock either way the above is really poor code enough said

Edited by webdesigner93

  • Author

@webdesigner93, i really just mean for people to take the relevant parts from it. Looking at the code now i can see it's a mess and its lost most of it's formatting for some reason when i posted it.

 

Here is an attachment of the original file with the formatting intact so it's a bit easier to read

 

noscriptchat.php

Sorry, but for me its not the formatting, or suppressing error messages or use of deprecated functions, its running an infinite loop on a web script. The exit point of your script is going to be PHP hitting the max_execution_time, memory_limit, apache will hit the max clients, the server cant spawn any new child processes, etc. essentially an easy DDoS target.

 

I understand you will probably go defensive and say this is a proof of concept, but its a proof of concept that’s fundamentally wrong. If you can't use it in a production environment then surely its a failure? Javascript is used by 92% of users, why not just use it? why not write something a bit more cutting edge like using websockets or node.js ? Something the community would really benefit from.

  • Author

I can’t disagree with you; your comment explains why it shouldn’t be used a lot better than my original explanation. I’d just like to say that this was only made as just a bit of fun after being told that real time applications on the web are impossible without Javascript or plugins. And yes, node.js + socket.io = amazing, would definitely recommend if anyone is looking at taking real time applications seriously.

 

Also, before anyone asks me why I didn’t use MySQLi or PDO, it was purely because MySQL was slightly faster at fetching the data from the database in my case, I should have mentioned this in the OP

For the love of everything holy. This forum needs to detect naff code and block it.

 

But I like posting my code here :(

  • 3 years later...

I see that this code is not good or appropriate; However, is it really possible to create a chat/messenger without having to use JavaScript? I have been researching this and would like to have a JavaScript free chats and websites.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.