0% found this document useful (0 votes)
52 views3 pages

Hacked by Brother

This PHP code implements a chat application with signup, login, and messaging functionality. It connects to a MySQL database to retrieve and insert user and message data. On page load, it checks if a user is logged in and displays either the login/signup form or the chat interface with recent messages. Users can sign up, log in, and send messages that get inserted into the database and displayed.

Uploaded by

testusr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

Hacked by Brother

This PHP code implements a chat application with signup, login, and messaging functionality. It connects to a MySQL database to retrieve and insert user and message data. On page load, it checks if a user is logged in and displays either the login/signup form or the chat interface with recent messages. Users can sign up, log in, and send messages that get inserted into the database and displayed.

Uploaded by

testusr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php
session_start();

// Database configuration
$servername = "localhost";
$username = "your_mysql_user";
$password = "your_mysql_password";
$dbname = "your_database_name";

// Connect to the database


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Signup functionality
if (isset($_POST['signup'])) {
$username = $_POST['username'];
$password = $_POST['password'];

// Insert user into the database


$sql = "INSERT INTO users (username, password) VALUES ('$username',
'$password')";
if ($conn->query($sql) === TRUE) {
echo "Signup successful. Please log in.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

// Login functionality
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];

// Retrieve user from the database


$sql = "SELECT * FROM users WHERE username = '$username' AND password =
'$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION['username'] = $username;
} else {
echo "Invalid username or password";
}
}

// Chat functionality
if (isset($_POST['send']) && isset($_SESSION['username'])) {
$message = $_POST['message'];
$username = $_SESSION['username'];
$timestamp = date("Y-m-d H:i:s");

// Insert message into the database


$sql = "INSERT INTO messages (username, message, timestamp) VALUES
('$username', '$message', '$timestamp')";
if ($conn->query($sql) === TRUE) {
// Message sent successfully
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

// Retrieve messages from the database


$sql = "SELECT * FROM messages ORDER BY timestamp DESC";
$result = $conn->query($sql);

?>

<!DOCTYPE html>
<html>
<head>
<title>Hacker Chat</title>
<style>
body {
background-color: #000;
color: #0F0;
font-family: monospace;
}
#chatbox {
width: 600px;
height: 400px;
background-color: #111;
padding: 10px;
overflow-y: scroll;
}
#inputArea {
display: flex;
margin-top: 10px;
}
#messageInput {
flex: 1;
background-color: #222;
border: none;
color: #0F0;
padding: 5px;
}
#sendButton {
background-color: #333;
color: #0F0;
border: none;
padding: 5px 10px;
cursor: pointer;
}
#loginArea {
margin-top: 20px;
}
#usernameInput,
#passwordInput {
background-color: #222;
border: none;
color: #0F0;
padding: 5px;
margin-right: 10px;
}
#loginButton,
#signupButton {
background-color: #333;
color: #0F0;
border: none;
padding: 5px 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Welcome to Hacker Chat</h1>

<?php if (!isset($_SESSION['username'])) : ?>

<div id="loginArea">
<form method="post" action="">
<input type="text" name="username" placeholder="Username"
required />
<input type="password" name="password" placeholder="Password"
required />
<button type="submit" name="login" id="loginButton">Login</button>
<button type="submit" name="signup" id="signupButton">Sign
Up</button>
</form>
</div>

<?php else : ?>

<div id="chatbox">
<?php while ($row = $result->fetch_assoc()) : ?>
<p>[<?php echo $row['timestamp']; ?>] <?php echo
$row['username']; ?>: <?php echo $row['message']; ?></p>
<?php endwhile; ?>
</div>

<div id="inputArea">
<form method="post" action="">
<input type="text" name="message" id="messageInput"
placeholder="Type your message" required />
<button type="submit" name="send" id="sendButton">Send</button>
</form>
</div>

<?php endif; ?>

</body>
</html>

<?php
$conn->close();
?>

You might also like