Finalize
This commit is contained in:
97
chat.php
97
chat.php
@@ -1,49 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>ChatLine - Real-time Messaging App</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||
<link rel="stylesheet" href="css/style.css" />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="chat-area">
|
||||
<header>
|
||||
<a href="#" class="back-icon"><i class="fas fa-arrow-left"></i></a>
|
||||
<img src="assets/default.png" alt="Profile photo" />
|
||||
<div class="details">
|
||||
<span>User</span>
|
||||
<p>Active now</p>
|
||||
</div>
|
||||
</header>
|
||||
<div class="chat-box">
|
||||
<div class="chat post">
|
||||
<div class="details">
|
||||
<p>
|
||||
This is an outgoing message. Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam facilis fugiat reiciendis adipisci iste assumenda error nemo quos nisi asperiores officiis voluptatibus, cumque laudantium. Praesentium aperiam rerum quisquam voluptate tempore!
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="chat get">
|
||||
<img src="assets/default.png" alt="Profile photo" />
|
||||
<div class="details">
|
||||
<p>
|
||||
This is an incoming message. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quidem consectetur illo nam! Veritatis odio consectetur delectus doloribus dolores rem, hic, id molestias odit cupiditate laboriosam, facere commodi eius officia repudiandae?
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
session_start();
|
||||
include_once "php/config.php";
|
||||
if (!isset($_SESSION["unique_id"])) {
|
||||
header("location: start.php");
|
||||
}
|
||||
?>
|
||||
|
||||
<?php include_once "header.php"; ?>
|
||||
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="chat-area">
|
||||
<header>
|
||||
<?php
|
||||
$user_id = mysqli_real_escape_string($conn, $_GET['user_id']);
|
||||
$sql = mysqli_query(
|
||||
$conn,
|
||||
"SELECT * FROM users WHERE unique_id = {$user_id}"
|
||||
);
|
||||
if (mysqli_num_rows($sql) > 0) {
|
||||
$selected_user = mysqli_fetch_assoc($sql);
|
||||
}
|
||||
?>
|
||||
<a href="home.php" class="back-icon"><i class="fas fa-arrow-left"></i></a>
|
||||
<img src="php/assets/<?php if ($selected_user["img"]) {
|
||||
echo $selected_user["img"];
|
||||
} else {
|
||||
echo "default.png";
|
||||
} ?>" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>
|
||||
<?php echo $selected_user['first_name'] . " " . $selected_user['last_name'] ?>
|
||||
</span>
|
||||
<p>
|
||||
<?php echo $selected_user["status"]; ?>
|
||||
</p>
|
||||
</div>
|
||||
<form action="#" class="message-input">
|
||||
<input type="text" placeholder="Send a message..." />
|
||||
<button type="submit"><i class="fab fa-telegram-plane"></i></button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</body>
|
||||
</header>
|
||||
<div class="chat-box">
|
||||
</div>
|
||||
<form action="#" class="message-input">
|
||||
<input type="text" class="selected-id" name="selected_id" value="<?php echo $user_id; ?>" hidden>
|
||||
<input type="text" class="message-text" name="message_text" placeholder="Send a message..." />
|
||||
<button type="submit"><i class="fab fa-telegram-plane"></i></button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/chat.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -30,8 +30,7 @@ CREATE TABLE `messages` (
|
||||
`msg_id` int(11) NOT NULL,
|
||||
`get_msg_id` int(255) NOT NULL,
|
||||
`post_msg_id` int(255) NOT NULL,
|
||||
`msg` varchar(1000) NOT NULL,
|
||||
`unique_id` int(255) NOT NULL
|
||||
`msg` varchar(1000) NOT NULL
|
||||
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;
|
||||
-- --------------------------------------------------------
|
||||
--
|
||||
@@ -57,7 +56,7 @@ CREATE TABLE `users` (
|
||||
--
|
||||
ALTER TABLE `messages`
|
||||
ADD PRIMARY KEY (`msg_id`),
|
||||
ADD KEY `FOREIGN` (`unique_id`) USING BTREE;
|
||||
ADD KEY `FOREIGN` (`post_msg_id`) USING BTREE;
|
||||
--
|
||||
-- Indexes for table `users`
|
||||
--
|
||||
@@ -86,7 +85,7 @@ MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
-- Constraints for table `messages`
|
||||
--
|
||||
ALTER TABLE `messages`
|
||||
ADD CONSTRAINT `FOREIGN_MESSAGES_USERS` FOREIGN KEY (`unique_id`) REFERENCES `users` (`unique_id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
ADD CONSTRAINT `FOREIGN_MESSAGES_USERS` FOREIGN KEY (`post_msg_id`) REFERENCES `users` (`unique_id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
COMMIT;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */
|
||||
;
|
||||
|
||||
@@ -265,7 +265,7 @@ body {
|
||||
color: forestgreen;
|
||||
}
|
||||
|
||||
.users a .status-dot .offline {
|
||||
.users a .status-dot.offline {
|
||||
font-size: 12px;
|
||||
color: gray;
|
||||
}
|
||||
@@ -311,55 +311,54 @@ body {
|
||||
}
|
||||
|
||||
.chat-box .chat {
|
||||
margin: 15px 0;
|
||||
margin: 15px 0;
|
||||
}
|
||||
|
||||
.chat-box .chat p {
|
||||
word-wrap: break-word;
|
||||
padding: 10px 15px;
|
||||
box-shadow: 0 32px 32px -32px rgba(0, 0, 0, 5%),
|
||||
0 -32px 32px -32px rgba(0, 0, 0, 5%);
|
||||
word-wrap: break-word;
|
||||
padding: 10px 15px;
|
||||
box-shadow: 0 32px 32px -32px rgba(0, 0, 0, 5%),
|
||||
0 -32px 32px -32px rgba(0, 0, 0, 5%);
|
||||
}
|
||||
|
||||
.chat-box .post {
|
||||
display: flex;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.post .details {
|
||||
margin-left: auto ;
|
||||
max-width: calc(100% - 100px);
|
||||
margin-left: auto;
|
||||
max-width: calc(100% - 100px);
|
||||
}
|
||||
|
||||
.post .details p {
|
||||
background: #333;
|
||||
color: #fff;
|
||||
border-radius: 15px 15px 0 15px;
|
||||
|
||||
background: #333;
|
||||
color: #fff;
|
||||
border-radius: 15px 15px 0 15px;
|
||||
}
|
||||
|
||||
.chat-box .get {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
margin-top: 15px;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.get img {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
object-fit: cover;
|
||||
border-radius: 50%;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
object-fit: cover;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.get .details {
|
||||
margin-left: 10px;
|
||||
margin-right: auto;
|
||||
max-width: calc(100% - 100px);
|
||||
margin-left: 10px;
|
||||
margin-right: auto;
|
||||
max-width: calc(100% - 100px);
|
||||
}
|
||||
|
||||
.get .details p {
|
||||
background: lightgray;
|
||||
color: black;
|
||||
border-radius: 15px 15px 15px 0;
|
||||
background: lightgray;
|
||||
color: black;
|
||||
border-radius: 15px 15px 15px 0;
|
||||
}
|
||||
|
||||
.chat-area .message-input {
|
||||
@@ -368,7 +367,7 @@ body {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.message-input input{
|
||||
.message-input input {
|
||||
height: 40px;
|
||||
width: calc(100% - 50px);
|
||||
font-size: 15px;
|
||||
|
||||
11
header.php
Normal file
11
header.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>ChatLine - Real-time Messaging App</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
|
||||
</head>
|
||||
166
home.php
166
home.php
@@ -1,117 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>ChatLine - Real-time Messaging App</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="user">
|
||||
<header>
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>User</span>
|
||||
<p>Active now</p>
|
||||
</div>
|
||||
<?php
|
||||
session_start();
|
||||
include_once "php/config.php";
|
||||
if (!isset($_SESSION["unique_id"])) {
|
||||
header("location: start.php");
|
||||
}
|
||||
?>
|
||||
|
||||
<?php include_once "header.php"; ?>
|
||||
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="user">
|
||||
<header>
|
||||
<div class="content">
|
||||
<?php
|
||||
$sql = mysqli_query(
|
||||
$conn,
|
||||
"SELECT * FROM users WHERE unique_id = {$_SESSION["unique_id"]}"
|
||||
);
|
||||
if (mysqli_num_rows($sql) > 0) {
|
||||
$current_user = mysqli_fetch_assoc($sql);
|
||||
}
|
||||
?>
|
||||
<img src="php/assets/<?php if ($current_user["img"]) {
|
||||
echo $current_user["img"];
|
||||
} else {
|
||||
echo "default.png";
|
||||
} ?>" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>
|
||||
<?php echo $current_user['first_name'] . " " . $current_user['last_name'] ?>
|
||||
</span>
|
||||
<p>
|
||||
<?php echo $current_user["status"]; ?>
|
||||
</p>
|
||||
</div>
|
||||
<a href="#" class="logout">Logout</a>
|
||||
</header>
|
||||
<div class="search">
|
||||
<span class="text">
|
||||
Who do you want to chat with?
|
||||
</span>
|
||||
<input type="text" placeholder="Enter name to search...">
|
||||
<button><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
<div class="users">
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
<a href="#">
|
||||
<div class="content">
|
||||
<img src="assets/default.png" alt="Profile photo">
|
||||
<div class="details">
|
||||
<span>Friend #1</span>
|
||||
<p>This is a test message</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot"><i class="fas fa-circle"></i></div>
|
||||
</a>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/search-visibility.js"></script>
|
||||
</body>
|
||||
<a href="php/logout.php?logout_id=<?php echo $current_user['unique_id']; ?>" class="logout">Logout</a>
|
||||
</header>
|
||||
<div class="search">
|
||||
<span class="text">
|
||||
Who do you want to chat with?
|
||||
</span>
|
||||
<input type="text" placeholder="Enter name to search...">
|
||||
<button><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
<div class="users">
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/home.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
98
index.php
98
index.php
@@ -1,52 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>ChatLine - Real-time Messaging App</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="register form">
|
||||
<header>Join us at ChatLine!</header>
|
||||
<form action="#" method="POST" enctype="multipart/form-data" autocomplete="off">
|
||||
<div class="error"></div>
|
||||
<div class="names">
|
||||
<div class="field">
|
||||
<label>First name</label>
|
||||
<input type="text" name="first_name" placeholder="First name" required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Last name</label>
|
||||
<input type="text" name="last_name" placeholder="Last name" required>
|
||||
</div>
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_SESSION['unique_id'])) {
|
||||
header("location: home.php");
|
||||
}
|
||||
?>
|
||||
|
||||
<?php include_once "header.php"; ?>
|
||||
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="register form">
|
||||
<header>Join us at ChatLine!</header>
|
||||
<form action="#" method="POST" enctype="multipart/form-data" autocomplete="off">
|
||||
<div class="error"></div>
|
||||
<div class="names">
|
||||
<div class="field">
|
||||
<label>First name</label>
|
||||
<input type="text" name="first_name" placeholder="First name" required>
|
||||
</div>
|
||||
<div>
|
||||
<div class="field">
|
||||
<label>Email</label>
|
||||
<input type="text" name="email" placeholder="Enter your email" required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Password</label>
|
||||
<input type="password" name="password" placeholder="Enter new password" required>
|
||||
<i class="fas fa-eye"></i>
|
||||
</div>
|
||||
<div class="image">
|
||||
<label>Profile photo</label>
|
||||
<input type="file" name="image" accept="image/x-png,image/jpeg,image/jpg">
|
||||
</div>
|
||||
<div class="button">
|
||||
<input type="submit" value="Register">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Last name</label>
|
||||
<input type="text" name="last_name" placeholder="Last name" required>
|
||||
</div>
|
||||
</form>
|
||||
<div class="link">Already have an account? <a href="login.html">Login now!</a></div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/password-visibility.js"></script>
|
||||
<script src="js/register.js"></script>
|
||||
</body>
|
||||
</div>
|
||||
<div>
|
||||
<div class="field">
|
||||
<label>Email</label>
|
||||
<input type="text" name="email" placeholder="Enter your email" required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Password</label>
|
||||
<input type="password" name="password" placeholder="Enter new password" required>
|
||||
<i class="fas fa-eye"></i>
|
||||
</div>
|
||||
<div class="image">
|
||||
<label>Profile photo</label>
|
||||
<input type="file" name="image" accept="image/x-png,image/jpeg,image/jpg">
|
||||
</div>
|
||||
<div class="button">
|
||||
<input type="submit" value="Register">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="link">Already have an account? <a href="start.php">Login now!</a></div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/password-visibility.js"></script>
|
||||
<script src="js/register.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
63
js/chat.js
Normal file
63
js/chat.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const form = document.querySelector(".message-input"),
|
||||
selectedId = form.querySelector(".message-input .selected-id").value,
|
||||
inputField = form.querySelector(".message-input .message-text"),
|
||||
sendButton = form.querySelector(".message-input button"),
|
||||
chatBox = document.querySelector(".chat-box");
|
||||
|
||||
form.onsubmit = (e) => {
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
inputField.focus();
|
||||
inputField.onkeyup = () => {
|
||||
if (inputField.value != "") {
|
||||
sendButton.classList.add("active");
|
||||
} else {
|
||||
sendButton.classList.remove("active");
|
||||
}
|
||||
};
|
||||
|
||||
sendButton.onclick = () => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "php/send_msg.php", true);
|
||||
xhr.onload = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
inputField.value = "";
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
};
|
||||
let formData = new FormData(form);
|
||||
xhr.send(formData);
|
||||
};
|
||||
|
||||
chatBox.onmouseenter = () => {
|
||||
chatBox.classList.add("active");
|
||||
};
|
||||
|
||||
chatBox.onmouseleave = () => {
|
||||
chatBox.classList.remove("active");
|
||||
};
|
||||
|
||||
setInterval(() => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "php/get_msg.php", true);
|
||||
xhr.onload = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
let data = xhr.response;
|
||||
chatBox.innerHTML = data;
|
||||
if (!chatBox.classList.contains("active")) {
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
xhr.send("selected_id=" + selectedId);
|
||||
}, 1000);
|
||||
|
||||
function scrollToBottom() {
|
||||
chatBox.scrollTop = chatBox.scrollHeight;
|
||||
}
|
||||
48
js/home.js
Normal file
48
js/home.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const searchElement = document.querySelector(".user .search"),
|
||||
searchBar = document.querySelector(".user .search input"),
|
||||
searchButton = document.querySelector(".user .search button"),
|
||||
usersList = document.querySelector(".user .users");
|
||||
|
||||
searchElement.onmouseover = () => {
|
||||
searchButton.classList.toggle("active");
|
||||
searchBar.classList.toggle("active");
|
||||
};
|
||||
|
||||
searchElement.onmouseout = () => {
|
||||
searchButton.classList.remove("active");
|
||||
searchBar.classList.remove("active");
|
||||
};
|
||||
|
||||
searchBar.onkeyup = () => {
|
||||
let query = searchBar.value;
|
||||
if (query != "") {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "php/search.php", true);
|
||||
xhr.onload = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
let data = xhr.response;
|
||||
usersList.innerHTML = data;
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
xhr.send("query=" + query);
|
||||
}
|
||||
};
|
||||
|
||||
setInterval(() => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "php/users.php", true);
|
||||
xhr.onload = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
let data = xhr.response;
|
||||
if (!searchBar.classList.contains("active")) {
|
||||
usersList.innerHTML = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
}, 1000);
|
||||
27
js/login.js
Normal file
27
js/login.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const form = document.querySelector(".login form"),
|
||||
continueBtn = form.querySelector(".button input"),
|
||||
errorText = form.querySelector(".error");
|
||||
|
||||
form.onsubmit = (e) => {
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
continueBtn.onclick = () => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "php/login.php", true);
|
||||
xhr.onload = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
let data = xhr.response;
|
||||
if (data === "success") {
|
||||
location.href = "home.php";
|
||||
} else {
|
||||
errorText.style.display = "block";
|
||||
errorText.textContent = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let formData = new FormData(form);
|
||||
xhr.send(formData);
|
||||
};
|
||||
@@ -14,7 +14,7 @@ continueBtn.onclick = () => {
|
||||
if (xhr.status === 200) {
|
||||
let data = xhr.response;
|
||||
if (data === "success") {
|
||||
location.href = "home.php";
|
||||
location.href = "start.php";
|
||||
} else {
|
||||
errorText.style.display = "block";
|
||||
errorText.textContent = data;
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
const searchElement = document.querySelector(".user .search");
|
||||
|
||||
const searchBar = document.querySelector(".user .search input");
|
||||
const searchButton = document.querySelector(".user .search button");
|
||||
|
||||
searchElement.onmouseover = () => {
|
||||
searchButton.classList.toggle("active");
|
||||
searchBar.classList.toggle("active");
|
||||
}
|
||||
|
||||
searchElement.onmouseout = () => {
|
||||
searchButton.classList.remove("active");
|
||||
searchBar.classList.remove("active");
|
||||
}
|
||||
37
login.php
37
login.php
@@ -1,37 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>ChatLine - Real-time Messaging App</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="register form">
|
||||
<header>Login to ChatLine</header>
|
||||
<form action="#" >
|
||||
<div class="error"></div>
|
||||
<div>
|
||||
<div class="field">
|
||||
<label>Email</label>
|
||||
<input type="text" placeholder="Enter your email" required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Password</label>
|
||||
<input type="password" placeholder="Enter your password" required>
|
||||
<i class="fas fa-eye"></i>
|
||||
</div>
|
||||
<div class="button">
|
||||
<input type="submit" value="Login">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="link">Don't have an account? <a href="register.html">Register now!</a></div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/password-visibility.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 5.8 KiB |
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
$hostname = "localhost:3200";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$dbname = "chatline";
|
||||
$hostname = "localhost:3200";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$dbname = "chatline";
|
||||
|
||||
$conn = mysqli_connect($hostname, $username, $password, $dbname);
|
||||
if(!$conn){
|
||||
echo "Database connection error".mysqli_connect_error();
|
||||
}
|
||||
$conn = mysqli_connect($hostname, $username, $password, $dbname);
|
||||
if (!$conn) {
|
||||
echo "Database connection error" . mysqli_connect_error();
|
||||
}
|
||||
?>
|
||||
37
php/get_msg.php
Normal file
37
php/get_msg.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_SESSION['unique_id'])) {
|
||||
include_once "config.php";
|
||||
$current_user = $_SESSION['unique_id'];
|
||||
$selected_user = mysqli_real_escape_string($conn, $_POST['selected_id']);
|
||||
$output = "";
|
||||
$sql = "SELECT * FROM messages LEFT JOIN users ON users.unique_id = messages.post_msg_id
|
||||
WHERE (post_msg_id = {$current_user} AND get_msg_id = {$selected_user})
|
||||
OR (post_msg_id = {$selected_user} AND get_msg_id = {$current_user}) ORDER BY msg_id";
|
||||
$query = mysqli_query($conn, $sql);
|
||||
if (mysqli_num_rows($query) > 0) {
|
||||
while ($row = mysqli_fetch_assoc($query)) {
|
||||
($row['img']) ? $img = $row['img'] : $img = "default.png";
|
||||
if ($row['post_msg_id'] === $current_user) {
|
||||
$output .= '<div class="chat post">
|
||||
<div class="details">
|
||||
<p>' . $row['msg'] . '</p>
|
||||
</div>
|
||||
</div>';
|
||||
} else {
|
||||
$output .= '<div class="chat get">
|
||||
<img src="php/assets/' . $img . '" alt="">
|
||||
<div class="details">
|
||||
<p>' . $row['msg'] . '</p>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$output .= '<div class="text">Send a message to start conversation :)</div>';
|
||||
}
|
||||
echo $output;
|
||||
} else {
|
||||
header("location: ../start.php");
|
||||
}
|
||||
?>
|
||||
30
php/list.php
Normal file
30
php/list.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
while ($row = mysqli_fetch_assoc($query)) {
|
||||
$sql2 = "SELECT * FROM messages WHERE (get_msg_id = {$row['unique_id']}
|
||||
OR post_msg_id = {$row['unique_id']}) AND (post_msg_id = {$current_user}
|
||||
OR get_msg_id = {$current_user}) ORDER BY msg_id DESC LIMIT 1";
|
||||
$query2 = mysqli_query($conn, $sql2);
|
||||
$row2 = mysqli_fetch_assoc($query2);
|
||||
(mysqli_num_rows($query2) > 0) ? $result = $row2['msg'] : $result = "No message available";
|
||||
(strlen($result) > 20) ? $msg = substr($result, 0, 20) . '...' : $msg = $result;
|
||||
if (isset($row2['post_msg_id'])) {
|
||||
($current_user == $row2['post_msg_id']) ? $you = "You: " : $you = "";
|
||||
} else {
|
||||
$you = "";
|
||||
}
|
||||
($row['status'] == "Offline") ? $offline = "offline" : $offline = "";
|
||||
($current_user == $row['unique_id']) ? $hide_me = "hide" : $hide_me = "";
|
||||
($row['img']) ? $img = $row['img'] : $img = "default.png";
|
||||
|
||||
$output .= '<a href="chat.php?user_id=' . $row['unique_id'] . '">
|
||||
<div class="content">
|
||||
<img src="php/assets/' . $img . '" alt="">
|
||||
<div class="details">
|
||||
<span>' . $row['first_name'] . " " . $row['last_name'] . '</span>
|
||||
<p>' . $you . $msg . '</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-dot ' . $offline . '"><i class="fas fa-circle"></i></div>
|
||||
</a>';
|
||||
}
|
||||
?>
|
||||
55
php/login.php
Normal file
55
php/login.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once "config.php";
|
||||
function escape($conn, $value)
|
||||
{
|
||||
return mysqli_real_escape_string($conn, $value);
|
||||
}
|
||||
function isValidEmail($email)
|
||||
{
|
||||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
function fetchUserByEmail($conn, $email)
|
||||
{
|
||||
$sql = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
|
||||
return mysqli_fetch_assoc($sql);
|
||||
}
|
||||
function encryptPassword($password)
|
||||
{
|
||||
return md5($password);
|
||||
}
|
||||
function updateStatus($conn, $user)
|
||||
{
|
||||
return mysqli_query($conn, "UPDATE users SET status = 'Active now' WHERE unique_id = {$user["unique_id"]}");
|
||||
}
|
||||
function loginUser($user)
|
||||
{
|
||||
$_SESSION["unique_id"] = $user["unique_id"];
|
||||
echo "success";
|
||||
}
|
||||
if (!empty($_POST["email"]) && !empty($_POST["password"])) {
|
||||
$email = escape($conn, $_POST["email"]);
|
||||
$password = escape($conn, $_POST["password"]);
|
||||
if (isValidEmail($email)) {
|
||||
$existingUser = fetchUserByEmail($conn, $email);
|
||||
if ($existingUser) {
|
||||
$encrypt_pass = encryptPassword($password);
|
||||
if ($encrypt_pass === $existingUser["password"]) {
|
||||
if (updateStatus($conn, $existingUser)) {
|
||||
loginUser($existingUser);
|
||||
} else {
|
||||
echo "An error occurred while logging in!";
|
||||
}
|
||||
} else {
|
||||
echo "The email or password is incorrect!";
|
||||
}
|
||||
} else {
|
||||
echo "The email address $email does not exist!";
|
||||
}
|
||||
} else {
|
||||
echo "The email address $email is not valid!";
|
||||
}
|
||||
} else {
|
||||
echo "All input fields are required!";
|
||||
}
|
||||
?>
|
||||
18
php/logout.php
Normal file
18
php/logout.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_SESSION['unique_id'])) {
|
||||
include_once "config.php";
|
||||
$logout_id = mysqli_real_escape_string($conn, $_GET['logout_id']);
|
||||
if (isset($logout_id)) {
|
||||
$sql = mysqli_query($conn, "UPDATE users SET status = 'Offline' WHERE unique_id={$_GET['logout_id']}");
|
||||
if ($sql) {
|
||||
session_unset();
|
||||
session_destroy();
|
||||
header("location: ../start.php");
|
||||
}
|
||||
} else {
|
||||
header("location: ../home.php");
|
||||
}
|
||||
} else {
|
||||
header("location: ../start.php");
|
||||
}
|
||||
@@ -1,34 +1,36 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once "config.php";
|
||||
function escape($conn, $value) {
|
||||
function escape($conn, $value)
|
||||
{
|
||||
return mysqli_real_escape_string($conn, $value);
|
||||
}
|
||||
function isValidEmail($email) {
|
||||
function isValidEmail($email)
|
||||
{
|
||||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
function moveUploadedFile($tempFileName, $newFileName) {
|
||||
return move_uploaded_file($tempFileName, "images/" . $newFileName);
|
||||
function moveUploadedFile($tempFileName, $newFileName)
|
||||
{
|
||||
return move_uploaded_file($tempFileName, "assets/" . $newFileName);
|
||||
}
|
||||
function generateUniqueUserId() {
|
||||
function generateUniqueUserId()
|
||||
{
|
||||
return rand(time(), 100000000);
|
||||
}
|
||||
function encryptPassword($password) {
|
||||
function encryptPassword($password)
|
||||
{
|
||||
return md5($password);
|
||||
}
|
||||
function fetchUserByEmail($conn, $email) {
|
||||
$email = escape($conn, $email);
|
||||
function fetchUserByEmail($conn, $email)
|
||||
{
|
||||
$sql = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
|
||||
return mysqli_fetch_assoc($sql);
|
||||
}
|
||||
function insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name, $status) {
|
||||
return mysqli_query($conn, "INSERT INTO users (unique_id, first_name, last_name, email, password, img, status) VALUES ({$unique_id}, '{$first_name}','{$last_name}', '{$email}', '{$encrypt_pass}', '{$new_img_name}', '{$status}')");
|
||||
function insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name)
|
||||
{
|
||||
return mysqli_query($conn, "INSERT INTO users (unique_id, first_name, last_name, email, password, img, status) VALUES ({$unique_id}, '{$first_name}','{$last_name}', '{$email}', '{$encrypt_pass}', '{$new_img_name}', 'Offline')");
|
||||
}
|
||||
function loginUser($user) {
|
||||
$_SESSION["unique_id"] = $user["unique_id"];
|
||||
echo "success";
|
||||
}
|
||||
if (!empty($_POST["first_name"]) || !empty($_POST["last_name"]) || !empty($_POST["email"]) || !empty($_POST["password"])) {
|
||||
if (!empty($_POST["first_name"]) && !empty($_POST["last_name"]) && !empty($_POST["email"]) && !empty($_POST["password"])) {
|
||||
$first_name = escape($conn, $_POST["first_name"]);
|
||||
$last_name = escape($conn, $_POST["last_name"]);
|
||||
$email = escape($conn, $_POST["email"]);
|
||||
@@ -44,12 +46,11 @@ if (!empty($_POST["first_name"]) || !empty($_POST["last_name"]) || !empty($_POST
|
||||
$temp_file_name = $_FILES["image"]["tmp_name"];
|
||||
$img_ext = pathinfo($img_name, PATHINFO_EXTENSION);
|
||||
$allowedExtensions = ["jpeg", "png", "jpg"];
|
||||
if (in_array($img_ext, $allowedExtensions) && in_array($img_type, ["image/jpeg", "image/jpg", "image/png", ])) {
|
||||
if (in_array($img_ext, $allowedExtensions) && in_array($img_type, ["image/jpeg", "image/jpg", "image/png",])) {
|
||||
$time = time();
|
||||
$new_img_name = $time . $img_name;
|
||||
if (moveUploadedFile($temp_file_name, $new_img_name)) {
|
||||
$unique_id = generateUniqueUserId();
|
||||
$status = "Active now";
|
||||
$encrypt_pass = encryptPassword($password);
|
||||
} else {
|
||||
echo "An error occurred while uploading the image.";
|
||||
@@ -61,15 +62,14 @@ if (!empty($_POST["first_name"]) || !empty($_POST["last_name"]) || !empty($_POST
|
||||
}
|
||||
} else {
|
||||
$unique_id = generateUniqueUserId();
|
||||
$status = "Active now";
|
||||
$encrypt_pass = encryptPassword($password);
|
||||
}
|
||||
if (insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name, $status)) {
|
||||
if (insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name)) {
|
||||
$loggedInUser = fetchUserByEmail($conn, $email);
|
||||
if ($loggedInUser) {
|
||||
loginUser($loggedInUser);
|
||||
} else {
|
||||
if (!$loggedInUser) {
|
||||
echo "An error occurred. Please try again.";
|
||||
} else {
|
||||
echo "success";
|
||||
}
|
||||
} else {
|
||||
echo "An error occurred. Please try again.";
|
||||
|
||||
17
php/search.php
Normal file
17
php/search.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once "config.php";
|
||||
$current_user = $_SESSION["unique_id"];
|
||||
if (isset($_POST["query"])) {
|
||||
$query = mysqli_real_escape_string($conn, $_POST["query"]);
|
||||
$sql = "SELECT * FROM users WHERE NOT unique_id = {$current_user} AND (first_name LIKE '%{$query}%' OR last_name LIKE '%{$query}%') ";
|
||||
$output = "";
|
||||
$query = mysqli_query($conn, $sql);
|
||||
if (mysqli_num_rows($query) > 0) {
|
||||
include_once "list.php";
|
||||
} else {
|
||||
$output .= "User not found :(";
|
||||
}
|
||||
echo $output;
|
||||
}
|
||||
?>
|
||||
15
php/send_msg.php
Normal file
15
php/send_msg.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_SESSION['unique_id'])) {
|
||||
include_once "config.php";
|
||||
$current_user = $_SESSION['unique_id'];
|
||||
$selected_user = mysqli_real_escape_string($conn, $_POST['selected_id']);
|
||||
$message = mysqli_real_escape_string($conn, $_POST['message_text']);
|
||||
if (!empty($message)) {
|
||||
$sql = mysqli_query($conn, "INSERT INTO messages (get_msg_id, post_msg_id, msg)
|
||||
VALUES ({$selected_user}, {$current_user}, '{$message}')") or die();
|
||||
}
|
||||
} else {
|
||||
header("location: ../start.php");
|
||||
}
|
||||
?>
|
||||
14
php/users.php
Normal file
14
php/users.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once "config.php";
|
||||
$current_user = $_SESSION['unique_id'];
|
||||
$sql = "SELECT * FROM users WHERE NOT unique_id = {$current_user} ORDER BY user_id DESC";
|
||||
$query = mysqli_query($conn, $sql);
|
||||
$output = "";
|
||||
if (mysqli_num_rows($query) == 0) {
|
||||
$output .= "No users are available to chat";
|
||||
} elseif (mysqli_num_rows($query) > 0) {
|
||||
include_once "list.php";
|
||||
}
|
||||
echo $output;
|
||||
?>
|
||||
38
start.php
Normal file
38
start.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_SESSION['unique_id'])) {
|
||||
header("location: home.php");
|
||||
}
|
||||
?>
|
||||
|
||||
<?php include_once "header.php"; ?>
|
||||
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<section class="login form">
|
||||
<header>Login to ChatLine</header>
|
||||
<form action="#">
|
||||
<div class="error"></div>
|
||||
<div>
|
||||
<div class="field">
|
||||
<label>Email</label>
|
||||
<input type="text" name="email" placeholder="Enter your email" required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Password</label>
|
||||
<input type="password" name="password" placeholder="Enter your password" required>
|
||||
<i class="fas fa-eye"></i>
|
||||
</div>
|
||||
<div class="button">
|
||||
<input type="submit" value="Login">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="link">Don't have an account? <a href="index.php">Register now!</a></div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="js/password-visibility.js"></script>
|
||||
<script src="js/login.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user