Skip to content
Browse files

login created with php sessions

  • Loading branch information...
1 parent 2ca28c5 commit b9e60b74558f4dd80ec83788d9a44233e93e91f8 @D-Ne0 committed Mar 4, 2012
Showing with 124 additions and 202 deletions.
  1. +5 −7 chat/chat.js
  2. +8 −23 chat/chat.php
  3. +16 −7 chat/chatFunctions.php
  4. +0 −30 chat/popUpChat.php
  5. +43 −51 chat/processRequest.php
  6. +11 −22 chat/show_online.php
  7. +4 −0 includes/const.php
  8. +21 −0 includes/init.php
  9. +3 −9 index.php
  10. +10 −43 login.php
  11. +3 −10 logout.php
View
12 chat/chat.js
@@ -17,15 +17,13 @@ $(document).ready(function(){
$("div#online_search_box input").val("");
});
$(window).focus(function(){
- $("title").text(browserTitle);
+ document.title = browserTitle;
});
});
-function showOnline() {
-
+function showOnline() {
var str = "action=checkMyOnlineStatus";
$.ajax({url:"processRequest.php", type:"POST", dataType:"xml", data:""+str+"", success:function(result){
-
var onlineStatus = $(result).find("root").attr("online");
if(onlineStatus=="yes") {
$("div#online_users_box").html("Loading...");
@@ -39,8 +37,7 @@ function showOnline() {
});
}
-function refreshOnline() {
-
+function refreshOnline() {
var search = $("div#online_search_box input").val();
if(search.length!=0 && search!="Search") {
var str = "search="+search;
@@ -246,7 +243,8 @@ function popUpChat() {
}
function refreshPopUpChat() {
- $.ajax({url:"popUpChat.php", dataType:"xml", success:function(result){
+ var str = "action=popUpChat";
+ $.ajax({url:"processRequest.php", type:"POST", data:""+str+"", dataType:"xml", success:function(result){
var c = $(result).find("root").attr("count");
if(c>0) {
$(result).find("users").each(function(){
View
31 chat/chat.php
@@ -1,19 +1,11 @@
<?php
-require('../includes/config.php');
-if(isset($_COOKIE['session_id'])) {
-
- $session_id = $_COOKIE['session_id'];
-
- $sql = "SELECT username,enroll FROM online WHERE session_id='".$session_id."' LIMIT 1";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
-
- if($count==1) {
- while($row = mysql_fetch_assoc($result)) {
- $username = $row['username'];
- $enroll = $row['enroll'];
- echo "Welcome ".$username.", <a href='../logout.php'>Logout</a>";
- }
+require('../includes/init.php');
+if(!check_login())
+ header('location: ../index.php');
+else {
+ $username = get_username();
+ $enroll = get_enroll();
+ echo "Welcome ".$username.", <a href='../logout.php'>Logout</a>";
?>
<!DOCTYPE html>
<html>
@@ -25,7 +17,7 @@
</head>
<body>
<div id="online_box" class="online">
- <audio controls="controls" style="display:none;" id="soundHandle"></audio> //this tag is for chat sound
+ <audio controls="controls" style="display:none;" id="soundHandle"></audio> <!--this tag is for chat sound -->
<div id="online_title_box" class="online" >
<div id="online_title" onClick="goOnline()">Who's Online</div>
<div id="min" class="opt" onClick="goOffline()" title="Go offline">-</div>
@@ -44,12 +36,5 @@
</body>
</html>
<?php
-
- }
-
- else
- header('Location: ../index.php');
}
-else
-header('Location: ../index.php');
?>
View
23 chat/chatFunctions.php
@@ -6,18 +6,15 @@ function startChatSession($to_roll,$enroll) {
}
function checkMyOnlineStatus($enroll) {
- $sql = "SELECT online FROM online WHERE enroll=".$enroll." LIMIT 1";
+ $sql = "SELECT online FROM stud_data WHERE usr_roll=".$enroll." LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
- if($row['online']=="yes")
- echo "<root online='yes'><roll>".$enroll."</roll></root>";
- else
- echo "<root online='no'><roll>".$enroll."</roll></root>";
+ echo "<root online='".$row['online']."'><roll>".$enroll."</roll></root>";
}
}
function setOnlineStatus($enroll,$status) {
- $sql = "UPDATE online SET online='".$status."' WHERE enroll=".$enroll;
+ $sql = "UPDATE stud_data SET online='".$status."' WHERE usr_roll=".$enroll;
mysql_query($sql);
}
@@ -28,7 +25,7 @@ function sendChat($to_enroll,$to_user,$msg,$enroll,$username) {
$msg = htmlentities($msg);
$msg = mysql_real_escape_string($msg);
- $sql = "SELECT null FROM online WHERE enroll=".$enroll." AND online='yes' LIMIT 1";
+ $sql = "SELECT null FROM stud_data WHERE usr_roll=".$enroll." AND online='yes' LIMIT 1";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==0)
@@ -122,4 +119,16 @@ function setWritingStatus($enroll,$s) {
mysql_query($sql);
}
}
+
+function popUpChat($enroll) {
+ $t = time()-3;
+ $sql = "SELECT from_enroll,from_user,msg FROM chat_messages WHERE to_enroll=".$enroll." AND time>=".$t." ORDER BY msg_id DESC";
+ $res = mysql_query($sql);
+ $c = mysql_num_rows($res);
+ echo "<root count='".$c."'>";
+ while($r = mysql_fetch_assoc($res)) {
+ echo "<users><name>".$r['from_user']."</name><roll>".$r['from_enroll']."</roll></users>";
+ }
+ echo "</root>";
+}
?>
View
30 chat/popUpChat.php
@@ -1,30 +0,0 @@
-<?php
-require('../includes/config.php');
-header('Content-type: text/xml');
-if(isset($_COOKIE['session_id'])) {
-
- $session_id = $_COOKIE['session_id'];
-
- $sql = "SELECT username,enroll FROM online WHERE session_id='".$session_id."' LIMIT 1";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
-
- if($count>0) {
- while($row = mysql_fetch_assoc($result)) {
- $username = $row['username'];
- $enroll = $row['enroll'];
- // echo $username." ".$enroll;
- }
- $t = time()-3;
- $sql = "SELECT from_enroll,from_user,msg FROM chat_messages WHERE to_enroll=".$enroll." AND time>=".$t." ORDER BY msg_id DESC";
- $res = mysql_query($sql);
- $c = mysql_num_rows($res);
- echo "<root count='".$c."'>";
- while($r = mysql_fetch_assoc($res)) {
- echo "<users><name>".$r['from_user']."</name><roll>".$r['from_enroll']."</roll></users>";
- }
- echo "</root>";
- }
-
-}
-?>
View
94 chat/processRequest.php
@@ -1,57 +1,49 @@
<?php
-require('../includes/config.php');
+require('../includes/init.php');
include('chatFunctions.php');
-if(isset($_COOKIE['session_id'])) {
+if(check_login()==true) {
+
+ $username = get_username();
+ $enroll = get_enroll();
+ $action = $_POST['action'];
+
+ if(isset($_POST['roll']))
+ $to_roll = mysql_real_escape_string($_POST['roll']);
- $session_id = $_COOKIE['session_id'];
-
- $sql = "SELECT username,enroll FROM online WHERE session_id='".$session_id."' LIMIT 1";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
-
- if($count==1) {
- while($row = mysql_fetch_assoc($result)) {
- $username = $row['username'];
- $enroll = $row['enroll'];
- //echo $username." ".$enroll;
- }
-
- $action = $_POST['action'];
- if(isset($_POST['roll']))
- $to_roll = mysql_real_escape_string($_POST['roll']);
-
- if($action=="startChatSession") {
-
- startChatSession($to_roll,$enroll);
- }
- else
- if($action=="sendChat") {
- $msg = $_POST['msg'];
- $to_user = mysql_real_escape_string($_POST['name']);
- $t = time()-3;
- $sql = "SELECT NULL FROM online WHERE enroll=".$to_roll." AND time>=".$t;
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
- if($count==1)
- sendChat($to_roll,$to_user,$msg,$enroll,$username);
- else
- echo "<root success='no'><user>".$to_user."</user></root>";
- }
- else
- if($action=="getChat") {
- getChat($to_roll,$enroll);
- }
- else
- if($action=="setWritingStatus")
- setWritingStatus($enroll,"yes");
- else
- if($action=="checkMyOnlineStatus")
- checkMyOnlineStatus($enroll);
- else
- if($action=="setOnlineStatus") {
- $status=mysql_real_escape_string($_POST['status']);
- setOnlineStatus($enroll,$status);
- }
+ if($action=="startChatSession") {
+ startChatSession($to_roll,$enroll);
+ }
+ else
+ if($action=="sendChat") {
+ $msg = $_POST['msg'];
+ $to_user = mysql_real_escape_string($_POST['name']);
+ $t = time()-3;
+ $sql = "SELECT NULL FROM stud_data WHERE usr_roll=".$to_roll." AND time>=".$t;
+ $result = mysql_query($sql);
+ $count = mysql_num_rows($result);
+ if($count==1)
+ sendChat($to_roll,$to_user,$msg,$enroll,$username);
+ else
+ echo "<root success='no'><user>".$to_user."</user></root>";
+ }
+ else
+ if($action=="getChat") {
+ getChat($to_roll,$enroll);
+ }
+ else
+ if($action=="setWritingStatus")
+ setWritingStatus($enroll,"yes");
+ else
+ if($action=="checkMyOnlineStatus")
+ checkMyOnlineStatus($enroll);
+ else
+ if($action=="setOnlineStatus") {
+ $status=mysql_real_escape_string($_POST['status']);
+ setOnlineStatus($enroll,$status);
}
+ else
+ if($action=="popUpChat") {
+ popUpChat($enroll);
+ }
}
?>
View
33 chat/show_online.php
@@ -1,39 +1,28 @@
<?php
-require('../includes/config.php');
-if(isset($_COOKIE['session_id'])) {
+require('../includes/init.php');
+if(check_login()==true) {
- $session_id = $_COOKIE['session_id'];
-
- $sql = "SELECT username,enroll FROM online WHERE session_id='".$session_id."' LIMIT 1";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
-
- if($count>0) {
- while($row = mysql_fetch_assoc($result)) {
- $username = $row['username'];
- $enroll = $row['enroll'];
- //echo $username." ".$enroll;
- }
+ $enroll = get_enroll();
$search = $_POST['search'];
- $sql = "UPDATE online SET time=".time()." WHERE session_id='".$session_id."' AND enroll=".$enroll;
+
+ //updating the time
+ $sql = "UPDATE stud_data SET time=".time()." WHERE usr_roll=".$enroll;
mysql_query($sql);
+ // getting online users
$time = time()-3;
if($search=="")
- $sql = "SELECT username, enroll FROM online WHERE time>=".$time." AND enroll<>".$enroll." AND online='yes'";
+ $sql = "SELECT usr_name, usr_roll FROM stud_data WHERE time>=".$time." AND usr_roll<>".$enroll;
else
- $sql = "SELECT username, enroll FROM online WHERE time>=".$time." AND enroll<>".$enroll." AND online='yes' AND UCASE(username) LIKE'%".strtoupper($search)."%'";
+ $sql = "SELECT usr_name, usr_roll FROM stud_data WHERE time>=".$time." AND usr_roll<>".$enroll." AND online='yes' AND UCASE(usr_name) LIKE'%".strtoupper($search)."%'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count>0) {
while($row = mysql_fetch_assoc($result)) {
- echo "<div id='user' onclick='javascript:chatWith(&#39;".$row['username']."&#39;,".$row['enroll'].")'>".$row['username']."</div>";
+ echo "<div id='user' onclick='javascript:chatWith(&#39;".$row['usr_name']."&#39;,".$row['usr_roll'].")'>".$row['usr_name']."</div>";
}
}
- }
- else
- echo "<div class='err_msg'>Invalid Username/Password, please <a href='../'>login </a>again</div>";
}
else
-echo "<div class='err_msg'>Session expired, please <a href='../'>login </a>again</div>";
+ echo "<div class='err_msg'>Invalid Username/Password, please <a href='../'>login </a>again</div>";
?>
View
4 includes/const.php
@@ -0,0 +1,4 @@
+<?php
+define(DOC_DIR,dirname(__FILE__));
+$db_file = DOC_DIR."/config.php";
+?>
View
21 includes/init.php
@@ -0,0 +1,21 @@
+<?php
+session_start();
+require('const.php');
+require($db_file);
+
+function check_login($username,$enroll) {
+ if(isset($_SESSION['username']) && isset($_SESSION['enroll'])) {
+ return true;
+ }
+ else
+ return false;
+}
+
+function get_username() {
+ return $_SESSION['username'];
+}
+
+function get_enroll() {
+ return $_SESSION['enroll'];
+}
+?>
View
12 index.php
@@ -1,13 +1,7 @@
<?php
-require('includes/config.php');
-
-if(isset($_COOKIE['session_id'])) {
- $session_id = $_COOKIE['session_id'];
- $sql = "SELECT null from online WHERE session_id='".$session_id."' LIMIT 1";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
- if($count==1)
- header('location:chat/chat.php');
+require('includes/init.php');
+if(check_login()==true){
+ header('location: chat/chat.php');
}
?>
View
53 login.php
@@ -1,35 +1,16 @@
<?php
-require('includes/config.php');
+require('includes/init.php');
+
function back_to_login() {
header('Location: index.php');
}
-function generate_session_id() {
-
- $session_id="";
- $char = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
- "0","1","2","3","4","5","6","7","8","9");
- for($i=0;$i<20;$i++)
- $session_id.=$char[mt_rand(0,61)];
-
- $sql = "SELECT null FROM online WHERE session_id='".$session_id."'";
- $result = mysql_query($sql);
- $count = mysql_num_rows($result);
-
- if($count>0)
- generate_session_id();
- else
- return $session_id;
-
-}
-
function send_to_chat() {
header('Location: chat/chat.php');
}
-if(isset($_POST['user']) && $_POST['user']!="" && isset($_POST['pass'])&& $_POST['pass']!="") {
+if(isset($_POST['user']) && isset($_POST['pass'])) {
$enroll = $_POST['user'];
$password = $_POST['pass'];
@@ -40,29 +21,15 @@ function send_to_chat() {
$count = mysql_num_rows($result);
// echo $count;
if($count==0)
- back_to_login();
- while($row = mysql_fetch_assoc($result)) {
-
-// echo "helli";
- $newsql = "SELECT session_id FROM online WHERE enroll=".$enroll;
- $newresult = mysql_query($newsql);
- $newcount = mysql_num_rows($newresult);
- if($newcount>0) {
- while($newrow = mysql_fetch_assoc($newresult)) {
- $sql = "DELETE FROM online WHERE session_id='".$newrow['session_id']."'";
- mysql_query($sql);
-// echo "hello";
- }
- }
-
- $session_id = generate_session_id();
- $username = $row['usr_name'];
- $curr_time = time();
- $sql = "INSERT INTO online (session_id, username, enroll, time) VALUES ('".$session_id."', '".$username."', ".$enroll.", ".$curr_time.")";
- mysql_query($sql);
- setcookie("session_id",$session_id,$curr_time+60*60*24*3);
+ back_to_login();
+ else {
+ while($row=mysql_fetch_array($result))
+ $username = $row['usr_name'];
+ $_SESSION['username'] = $username;
+ $_SESSION['enroll'] = $enroll;
send_to_chat();
}
+
}
else
View
13 logout.php
@@ -1,12 +1,5 @@
<?php
-include('includes/config.php');
-if(isset($_COOKIE['session_id'])) {
- $session_id = $_COOKIE['session_id'];
- $sql = "DELETE FROM online WHERE session_id='".$session_id."'";
- mysql_query($sql);
- setcookie("session_id",$session_id,time());
- header('location:index.php');
-
-} else
- header('location:index.php');
+session_start();
+session_destroy();
+header('location:index.php');
?>

0 comments on commit b9e60b7

Please sign in to comment.
Something went wrong with that request. Please try again.