Skip to main content

How To create Chat Server using - PHP with MYsql db connectivity

//-------------- Chat server BY Kilvish ------------


<?php
// Establish MySQL database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chatdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve chat messages from the database
function getChatMessages() {
    global $conn;
    $sql = "SELECT * FROM chat_messages ORDER BY created_at ASC";
    $result = $conn->query($sql);
    $messages = array();

    if ($result->num_rows > 0) {    
        while ($row = $result->fetch_assoc()) {
            $messages[] = $row;
        }
    }

    return $messages;
}

// Insert a new chat message into the database
function insertChatMessage($username, $message) {
    global $conn;
    $sql = "INSERT INTO chat_messages (username, message,created_at) VALUES ('$username', '$message','')";
   
    if ($conn->query($sql) === true) {
        return true;
    } else {
        return false;
    }
}

// Handle new chat message submission
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['username']) && isset($_POST['message'])) {
    $username = $_POST['username'];
    // $_SESSION[$username]=$uid;
    $message = $_POST['message'];

    if (insertChatMessage($username, $message)) {
        echo "Message sent successfully.";
    } else {
        echo "Error sending message.";
    }
}

// Retrieve chat messages
$chatMessages = getChatMessages();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Chat Server</title>
</head>
<body>
    <h2>Chat Messages</h2>
    <ul>
        <?php foreach ($chatMessages as $message): ?>
            <li><?php echo $message['username'] . ": " . $message['message']; ?></li>
        <?php endforeach; ?>
    </ul>

    <h2>Send Message</h2>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="username">Username:</label>
        <input type="text" name="username" required><br><br>

        <label for="message">Message:</label>
        <input type="text" name="message" required><br><br>

        <input type="submit" value="Send">
    </form>
</body>
</html>

<?php
// Close the database connection
$conn->close();
?>

Comments

Popular posts from this blog

How to deploy NodeJS app on server with Apache2 and acess it with Server IP addresss - Node JS deploy project Live.

     hello friens , This is Vasu Birla , in previous post we have seen the deployement of NodeJS app on AWS instance server instance but AWS server is expensive than other servers . SO today we will use simple Ubuntu server for making Live out Project using Apache2 . At the end you will be abe to access your NodeJS app using server IP address from anywhere  .. 1.  Login to your server using SSH terminal . (in AWS part i already explained how to do this )     -> Open your putty in hostname put your server IP -      login with ssh username ->root and password     (if you dont have root username and pass ask your      provider or reset it from cpanel or hosting panel ) 2. after login - on terminal you can Put your project anywhere.     There are two ways to put your project folder on server location     (i) - > using Github - (very popular and easy to track your everyday code changes to your project folder)             you can push your entire project folder to git-hub and and

How to to deploy python app sepratly on same server (previous one ) /root/demo - Deploy Python app on Ubuntu Server using ssh Terminal - by Vasu Birla

hello friends , am Vasu Birla and Today we will deploy Python App.. in previous post we have deployed NODE js app on 0.0.0.0 (your server IP) and acess it using http://0.0.0.0/   Now  If you want to deploy a Python app separately on the same server, in the /root/demo directory, and access it using http://0.0.0.0/demo,  you can use Apache2 and mod_wsgi to host the Python app.   Additionally, you can configure Apache2 to proxy the requests for the   /demo path to your Python app without affecting the Node.js app hosted at the root.   First of all  put your python project folder on root location of server , using putty with cloning git repo or using FTP server and direct paste folder (in previous post we aleady discussed it)   lets suppose your folder name is demo .. location would be  /root/demo   install necessery things to support your python app .  like pip install or django ..etc     as well install in virtual enviorment also ..       -> python3 -m venv venv        -> source v

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  Functions of Router