-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatSession.php
81 lines (65 loc) · 2.27 KB
/
chatSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Phprachet;
session_start();
class ChatSession{
private $chatId;
private $sessionId;
private $sender;
private $receiver;
private $msg;
private $date_sent;
protected $dbConn;
function setSender($sender) {$this->sender=$sender;}
function setReceiver($receiver){$this->receiver=$receiver;}
function setMsg($msg){$this->msg=$msg;}
function getSessionId() {return $this->sessionId;}
function __construct() {
echo "Chat Session Initialized";
}
function setSessionId($connect){
$sql="SELECT * FROM chat";
$result=mysqli_query($connect,$sql);
$num = rand(100,1000);
if (mysqli_num_rows($result)>0){
$data=mysqli_fetch_assoc($result);
if ($data['sessionId']==$num){
$num = rand(100,1000);
$this->sessionId=$num;
}
}else{
$this->sessionId=$num;
}
}
function savetoDb($connect){
echo "Saving to DB ... ";
// $sql="INSERT INTO chat VALUES ($this->sessionId,'$this->sender','$this->receiver','$this->msg',CURRENT_DATE());";
// if (mysqli_query($connect,$sql)){
// echo "Saved to DB!";
// }
}
function getAllUsers($connect){
$sql="SELECT * FROM users";
$result=mysqli_query($connect,$sql);
if (mysqli_query($connect,$sql)){
echo '<input type="hidden" id="sendTo" value="">';
while($data=mysqli_fetch_assoc($result)){
if ($data["Username"]==$_SESSION['Username']){
continue;
}
echo "<br>";
echo '<form action="" method="post">';
echo '<button name="submit" type="submit" value="'.$data["Name"].'">'.$data["Name"].'</button>';
if (isset($_POST["submit"])){
$_SESSION["sendTo"]=$_POST["submit"];
echo $_SESSION["sendTo"];
header("Location: chatbox.php");
}
echo '</form>';
echo "<br>";
}
}else{
echo "Error: " ,mysqli_error($connect);
}
}
}
?>