From a67188dd014551816470fa11cde2391cc3992e42 Mon Sep 17 00:00:00 2001 From: ankitjain28may Date: Sun, 25 Sep 2016 00:27:50 +0530 Subject: [PATCH] Show user online --- ajax/change.php | 16 +++++----- ajax/chat.php | 5 ++-- css/style.css | 10 ++++++- js/index.js | 30 +++++++++++++++++++ registration-module/source/class.login.php | 28 ++++++++++------- registration-module/source/class.logout.php | 12 ++++++-- registration-module/source/class.register.php | 17 ++++++----- 7 files changed, 87 insertions(+), 31 deletions(-) diff --git a/ajax/change.php b/ajax/change.php index 22f4ede..666e7dd 100644 --- a/ajax/change.php +++ b/ajax/change.php @@ -10,13 +10,13 @@ $id=$_SESSION['start']; $query="SELECT * FROM total_message WHERE user1='$id' or user2='$id' ORDER BY id DESC"; // $query="SELECT * FROM total_message WHERE identifier like '%:$id' or '$id:%'"; - if($result=$connect->query($query)) + if($result=$connect->query($query)) { - if ($result->num_rows > 0) + if ($result->num_rows > 0) { $array = array(); $ln=strlen($id); - while($row = $result->fetch_assoc()) + while($row = $result->fetch_assoc()) { if($row['id']==$last_time && $flag!=0) { @@ -32,8 +32,8 @@ $st=substr($value, 0,$ln); if($st!=$id) { - $query="SELECT username,name from login where login_id='$st'"; - if($result1=$connect->query($query)) + $query="SELECT username,name,login_status from login where login_id='$st'"; + if($result1=$connect->query($query)) { if($result1->num_rows>0) { @@ -51,12 +51,12 @@ } } } - + else { $st=substr($value,$ln+1); - $query="SELECT username,name from login where login_id='$st'"; - if($result1=$connect->query($query)) + $query="SELECT username,name,login_status from login where login_id='$st'"; + if($result1=$connect->query($query)) { if($result1->num_rows>0) { diff --git a/ajax/chat.php b/ajax/chat.php index 17524d3..704a758 100644 --- a/ajax/chat.php +++ b/ajax/chat.php @@ -13,7 +13,7 @@ // $query="SELECT * FROM total_message WHERE user1='$id' or user2='$id'"; - $query="SELECT login_id,name FROM login WHERE username='$username'"; + $query="SELECT login_id,name,login_status FROM login WHERE username='$username'"; $connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if($result=$connect->query($query)) { @@ -71,6 +71,7 @@ $row['time']=substr($row['time'],4,11); $row['identifier_message_number']=$login_id; $row=array_merge($row,['name'=>$fetch['name']]); + $row=array_merge($row,['login_status'=>$fetch['login_status']]); $row=array_merge($row,['start'=>$id]); $row=array_merge($row,['username'=>$username]); $array=array_merge($array,[$row]); @@ -82,7 +83,7 @@ } else { - echo json_encode(['identifier_message_number'=>$login_id,'name'=>$fetch['name'],'new'=>0]); + echo json_encode(['identifier_message_number'=>$login_id,'name'=>$fetch['name'],'login_status'=>$fetch['login_status'],'new'=>0]); } } else diff --git a/css/style.css b/css/style.css index 82b637d..e3ffb44 100644 --- a/css/style.css +++ b/css/style.css @@ -124,6 +124,15 @@ a { border: 1px solid #e9ebee; } +.online { + display: inline-block; + margin-left: 5px; + width: 6px; + height: 6px; + border-radius: 50%; + background-color:#42b72a; + margin-top:10px; +} #message { overflow-y: scroll; } @@ -211,7 +220,6 @@ a { } - .sender { float: right; color: #fff; diff --git a/js/index.js b/js/index.js index 84c8831..b80ca9c 100644 --- a/js/index.js +++ b/js/index.js @@ -46,6 +46,15 @@ function init(index) bre.appendChild(inp); bre.setAttribute('class','message_time'); para.appendChild(bre); + + if(arr[i]['login_status']=='1') + { + var online = document.createElement("div"); + online.setAttribute('class','online'); + para.appendChild(online); + } + + } // Load messgage for the first conversation @@ -147,6 +156,15 @@ function chat(element,num) var txt=$("").text(arr[0].name); $("#chat_heading").append(txt); $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr[0].username}); + // Online + if(arr[0]['login_status']=='1') + { + var online = document.createElement("p"); + online.setAttribute('class','online'); + $("#chat_heading a").append(online); + $("#chat_heading a p").css({"float":'right'}); + } + if(width()) $(".text_icon #text_reply").attr({'name':arr[0]['identifier_message_number']}); else @@ -159,14 +177,26 @@ function chat(element,num) { ele.innerHTML=""; $("#chat_heading a").remove('a'); + var txt=$("").text(arr.name); $("#chat_heading").append(txt); $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr.username}); + + if(arr['login_status']=='1') + { + var online = document.createElement("p"); + online.setAttribute('class','online'); + $("#chat_heading a").append(online); + $("#chat_heading a p").css({"float":'right'}); + } + if(width()) $(".text_icon #text_reply").attr({'name':arr['identifier_message_number']}); else $("#text_reply").attr({'name':arr['identifier_message_number']}); } + + } } }; diff --git a/registration-module/source/class.login.php b/registration-module/source/class.login.php index f4f250b..6577527 100644 --- a/registration-module/source/class.login.php +++ b/registration-module/source/class.login.php @@ -24,19 +24,19 @@ function _login($login,$password) $this->login=trim($login); $this->password=trim($password); - if(empty($this->login)) + if(empty($this->login)) { $this->key=1; $this->array_error=array_merge($this->array_error,["login"=>"Enter the login field"]); } elseif (preg_match("/^[@]{1}$/",$this->login)) - { - if(filter_var($this->email,FILTER_VALIDATE_EMAIL)== false) + { + if(filter_var($this->email,FILTER_VALIDATE_EMAIL)== false) { $this->key=1; $this->array_error=array_merge($this->array_error,["login"=>"Enter correct Email address"]); } - } + } if(empty($this->password)) { $this->key=1; $this->array_error=array_merge($this->array_error,["password"=>"Enter the password"]); @@ -49,9 +49,9 @@ function _login($login,$password) if($this->key==0) { $query="SELECT * FROM login WHERE email='$this->login' or username='$this->login'"; - if ($result=$connect->query($query)) + if ($result=$connect->query($query)) { - if ($result->num_rows>0) + if ($result->num_rows>0) { $row=$result->fetch_assoc(); $login_id=$row['login_id']; @@ -60,8 +60,16 @@ function _login($login,$password) { if ($result->num_rows>0) { - $_SESSION['start']=$login_id; - return json_encode(["location"=>"http://localhost/openchat/account.php"]); + $query = "Update login set login_status = '1' where login_id = '$login_id'"; + if($result = $connect->query($query)) + { + $time=date("D d M Y H:i:s", time()+12600); // current time + $time_id=date("YmdHis",time()+12600); + // $query = "Update " + $_SESSION['start']=$login_id; + return json_encode(["location"=>"http://localhost/openchat/account.php"]); + } + } else { @@ -75,8 +83,8 @@ function _login($login,$password) $this->array_error=array_merge($this->array_error,["login"=>"Invalid username or email"]); return json_encode($this->array_error); } - } - + } + } else { diff --git a/registration-module/source/class.logout.php b/registration-module/source/class.logout.php index 5cf9db0..78d0922 100644 --- a/registration-module/source/class.logout.php +++ b/registration-module/source/class.logout.php @@ -1,9 +1,17 @@ query($query)) + { + unset($_SESSION['start']); + header('Location: ../../index.php'); + } + } else { diff --git a/registration-module/source/class.register.php b/registration-module/source/class.register.php index 96e7baf..4d63c55 100644 --- a/registration-module/source/class.register.php +++ b/registration-module/source/class.register.php @@ -21,7 +21,7 @@ function __construct() $this->key=0; $this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $this->ob=new validate(); - + $query="CREATE TABLE IF NOT EXISTS register ( id int primary key auto_increment unique not null, @@ -40,9 +40,10 @@ function __construct() email varchar(255) unique not null, username varchar(255) unique not null, mobile varchar(255) not null, + login_status varchar(255), FOREIGN KEY (login_id) REFERENCES register(id) ) ENGINE=INNODB;"; - + if (!$this->connect->query($query)) { echo "Table is not created || Query failed"; } @@ -53,11 +54,11 @@ function __construct() education text, gender varchar(10) ) ENGINE=INNODB;"; - + if (!$this->connect->query($query)) { echo "Table is not created || Query failed"; } - + } function _register($name,$email,$username,$password,$mob) @@ -135,8 +136,8 @@ function _register($name,$email,$username,$password,$mob) if($result=$this->connect->query($query)) { $row=$result->fetch_assoc(); $id=$row['id']; - - $query="INSERT INTO login VALUES('$id','$this->name','$this->email','$this->username','$this->mob')"; + + $query="INSERT INTO login VALUES('$id','$this->name','$this->email','$this->username','$this->mob','1')"; if(!$this->connect->query($query)) { $this->key=1; echo "You are not registered || Error in registration1"; @@ -148,7 +149,7 @@ function _register($name,$email,$username,$password,$mob) echo "You are not updated || Error in profile"; } } - } + } } if ($this->key==0) { $_SESSION['start']=$id; @@ -158,6 +159,6 @@ function _register($name,$email,$username,$password,$mob) { return json_encode($this->array_error); } - } + } } ?> \ No newline at end of file