Professional Documents
Culture Documents
This is a Simple Chat System wherein you can discuss about some topic and
implement in real world. This simple system requires users to register and login in
order to join the group discussion. The system is programmed using PHP, MySQL
Database, HTML, CSS, JavaScript (jQuery), and MySQLi procedural method. The
user can also update his/her account details.
Features:
Login
Registration
The following scripts are the codes that I have used to build the login and registration
form
Index.php
Source Code :
<?php
session_start();
if (isset($_SESSION['message'])){
echo $_SESSION['message'];
unset ($_SESSION['message']);
?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>chatME</title>
</head>
<body>
<div class="pen-title">
<h1><!-- ChatME--></h1>
</div>
</div>
<div class="form">
</form>
</div>
<div class="form">
<h2>Create an account</h2>
<button>Register</button>
</form>
</div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://codepen.io/andytran/pen/vLmRVp.js'></script>
<script src="js/index.js"></script>
</body>
</html>
register.php
Source code:
<?php
include("conn.php");
$username=mysqli_real_escape_string($conn,$_POST['username']);
$password=mysqli_real_escape_string($conn,$_POST['password']);
$email=mysqli_real_escape_string($conn,$_POST['email']);
$phone=mysqli_real_escape_string($conn,$_POST['phone']);
$your_name=mysqli_real_escape_string($conn,$_POST['your_name']);
else{
echo "<script>window.location.href='index.php?registered'</script>";
?>
Result
login.php
Source Code :
<?php
session_start();
include('conn.php');
$username=$_POST['username'];
$password=$_POST['password'];
if (mysqli_num_rows($query)<1){
echo "<script>window.location.href='index.php?attempt=failed'</script>";
//header('location:index.php');
else{
$row=mysqli_fetch_array($query);
$_SESSION['userid']=$row['userid'];
header('location:home.php');
?>
Result
For the "conn.php", the included file for both register and login process is the file where I
open the connection with the database.
Source Code :
<?php
date_default_timezone_set("Asia/Manila");
$date=date('F j, Y g:i:a');
//mysqli procedural
$conn=mysqli_connect("localhost","root","","chatme");
if(!$conn){
?>
I used below script for the structure of my user table in the database.
Source Code :
Result
After the user login to the system, they will be redirected to the home page which is the chat
room by default. For sending the messages of the user I used ajax send the message and user
info to the back-end code to save it into the database without leaving the page.
home.php
Source Code :
<?php
include('conn.php');
session_start();
header('location:index.php');
exit();
$urow=mysqli_fetch_assoc($uquery);
?>
<!DOCTYPE html>
<html>
<head>
<title>chatME</title>
<script src="jquery-3.1.1.js"></script>
<script src="js/home.js"></script>
<script type="text/javascript">
displayResult();
/* Send Message */
if($('#msg').val() == ""){
}else{
$msg = $('#msg').val();
$id = $('#id').val();
$.ajax({
type: "POST",
url: "send_message.php",
data: {
msg: $msg,
id: $id,
},
success: function(){
displayResult();
});
/* $("form").submit();
);
displayResult();
/* Send Message */
$('#send_msg').on('click', function(){
if($('#msg').val() == ""){
}else{
$msg = $('#msg').val();
$id = $('#id').val();
$.ajax({
type: "POST",
url: "send_message.php",
data: {
msg: $msg,
id: $id,
},
success: function(){
displayResult();
});
});
/* END */
});
function displayResult(){
$id = $('#id').val();
$.ajax({
url: 'send_message.php',
type: 'POST',
async: false,
data:{
id: $id,
res: 1,
},
success: function(response){
$('#result').html(response);
});
</script>
</head>
<body>
<tr>
</tr>
<?php
$row=mysqli_fetch_array($query);
?>
<div>
<tr>
</tr>
</div>
<tr>
<td>
</button>
</form>
</td>
</tr>
</table>
</body>
</html>
Result
I used the code below for the structure of my "chat" table in the database.
Source Code :
`chat_msg` text,
Result
For building the User Account Details View or the User profile using the below script.
profile.php
<?php
include('conn.php');
session_start();
header('location:index.php');
exit();
$row=mysqli_fetch_array($query);
$username=$row['username'];
$password=$row['password'];
$email=$row['email'];
$phone=$row['phone'];
$your_name=$row['your_name'];
?>
<!DOCTYPE html>
<html>
<head>
<title>chatME - Profile</title>
</head>
<body>
<tr>
<td><a href="home.php">Home</a></td>
</tr>
<tr>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td><b>Details</b></td>
</tr>
<tr>
<td>Username: <input type="text"
value="<?php echo $username;?>" disabled /></td>
</tr>
<tr>
<td>Password: <input
type="password" value="<?php echo $password; ?>" disabled /></td>
</tr>
<tr>
<td>Email: &nb
sp;<input type="email" value="<?php echo $email; ?>" disabled /></td>
</tr>
<tr>
<td>Phone: <in
put type="text" value="<?php echo $phone; ?>" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
</tr>
</table>
</body>
</html>
Result
And below is for updating the user's account details.
edit_details.php
Source Code :
<?php
include('conn.php');
session_start();
header('location:index.php');
exit();
$row=mysqli_fetch_array($query);
$username=$row['username'];
$password=$row['password'];
$email=$row['email'];
$phone=$row['phone'];
$your_name=$row['your_name'];
if(isset($_POST['submit'])){
$userid=$_GET['userid'];
$your_name_edit=mysqli_real_escape_string($conn,$_POST['your_name']);
$username_edit=mysqli_real_escape_string($conn,$_POST['username']);
$password_edit=mysqli_real_escape_string($conn,$_POST['password']);
$email_edit=mysqli_real_escape_string($conn,$_POST['email']);
$phone_edit=mysqli_real_escape_string($conn,$_POST['phone']);
echo
"<script>window.location.href='edit_details.php?userid=$_SESSION[userid]'</script>
";
?>
<!DOCTYPE html>
<html>
<head>
<script src="js/home.js"></script>
</head>
<body>
<tr>
<td><a href="home.php">Home</a></td>
</tr>
<tr>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td><b>Details</b></td>
</tr>
<tr>
</tr>
<tr>
<td>Username: <input type="text"
name="username" value="<?php echo $username;?>" /></td>
</tr>
<tr>
<td>Password: <input
type="password" name="password" value="<?php echo $password; ?>" /></td>
</tr>
<tr>
<td>Email: &nb
sp;<input type="email" name="email" value="<?php echo $email; ?>" /></td>
</tr>
<tr>
<td>Phone: <in
put type="text" name="phone" value="<?php echo $phone; ?>" /></td>
</tr>
<tr>
</tr>
</form>
<tr>
<td></td>
</tr>
<tr>
</tr>
</table>
</body>
</html>
Result
The system is free to download and for modification for educational purposes. To download
and run this simple chat system, please continue reading and follow the instructions below.
How to Run
Requirements
Setting Up
Download and Extract the source code zip file. (download button is located below)
Start the "Apache" and "MySQL" of your local web server. For XAMPP/WAMP,
open the XAMPP/WAMP's Control Panel to do this.
Locate the SQL file in the extracted folder of the source code located inside the db
folder. The SQL file is known as "chatme.sql".
If you are using XAMPP, copy the extracted source code folder and paste it into the
xampp's "htdocs" folder. And for the WAMP, paste the folder inside the "www"
folder.