PHP - Problemas con el chat en mi web

 
Vista:
sin imagen de perfil

Problemas con el chat en mi web

Publicado por Erick (5 intervenciones) el 12/04/2014 18:27:15
Hola chicos y chicas he instalado en mi web el chat de anantgarg.com
El problema que tengo es que me funciona perfectamente pero justo cuando cierro la sesion, el chat se queda activado y se pueden mandar mensajes si los dos usuarios estan desconectados, sin embargo cuando los usuarios se conectan no se puede usar el chat, en el server me dicen que el error está en la linea 51 y 122
exactamente esto:
[Fri Apr 11 12:07:40 2014] [warn] [client X.X.X.X] mod_fcgid: stderr: PHP Notice: Undefined index: username in /var/www/vhosts/oshasocial.com/httpdocs/chat.php on line 51, referer: http://www.miweb.com
[Fri Apr 11 12:07:40 2014] [warn] [client X.X.X.X] mod_fcgid: stderr: PHP Notice: Undefined index: username in /var/www/vhosts/oshasocial.com/httpdocs/chat.php on line 122, referer: http://www.miweb.com

os adjunto aqui el codigo con las lineas de error subrayadas para si alguien me puede ayudar con este problemita, gracias y saludos

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
if (!isset($_SESSION)) {
session_start();
}
 
 
 
 
define ('DBPATH','localhost');
define ('DBUSER','XXXXXXXX');
define ('DBPASS','XXXXXXXX');
define ('DBNAME','XXXXXXXX');
 
 
global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);
 
if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
if ($_GET['action'] == "sendchat") { sendChat(); }
if ($_GET['action'] == "closechat") { closeChat(); }
if ($_GET['action'] == "startchatsession") { startChatSession(); }
 
if (!isset($_SESSION['chatHistory'])) {
$_SESSION['chatHistory'] = array();
}
 
if (!isset($_SESSION['openChatBoxes'])) {
$_SESSION['openChatBoxes'] = array();
}
 
function chatHeartbeat() {
 
(linea 51) $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
$query = mysql_query($sql);
$items = '';
 
$chatBoxes = array();
 
while ($chat = mysql_fetch_array($query)) {
 
if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
$items = $_SESSION['chatHistory'][$chat['from']];
}
 
$chat['message'] = sanitize($chat['message']);
 
$items .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;
 
if (!isset($_SESSION['chatHistory'][$chat['from']])) {
$_SESSION['chatHistory'][$chat['from']] = '';
}
 
$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;
 
unset($_SESSION['tsChatBoxes'][$chat['from']]);
$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
}
 
if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
$now = time()-strtotime($time);
$time = date('g:iA M dS', strtotime($time));
 
$message = "Enviado $time";
if ($now > 180) {
$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
 
if (!isset($_SESSION['chatHistory'][$chatbox])) {
$_SESSION['chatHistory'][$chatbox] = '';
}
 
$_SESSION['chatHistory'][$chatbox] .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
$_SESSION['tsChatBoxes'][$chatbox] = 1;
}
}
}
}
 
(linea122) $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
$query = mysql_query($sql);
 
if ($items != '') {
$items = substr($items, 0, -1);
}
header('Content-type: application/json');
?>
{
"items": [
<?php echo $items;?>
]
}
 
<?php
exit(0);
}
 
function chatBoxSession($chatbox) {
 
$items = '';
 
if (isset($_SESSION['chatHistory'][$chatbox])) {
$items = $_SESSION['chatHistory'][$chatbox];
}
 
return $items;
}
 
function startChatSession() {
$items = '';
if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
$items .= chatBoxSession($chatbox);
}
}
 
 
if ($items != '') {
$items = substr($items, 0, -1);
}
 
header('Content-type: application/json');
?>
{
"username": "<?php echo $_SESSION['username'];?>",
"items": [
<?php echo $items;?>
]
}
 
<?php
 
 
exit(0);
}
 
function sendChat() {
$from = $_SESSION['username'];
$to = $_POST['to'];
$message = $_POST['message'];
 
$_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
 
$messagesan = sanitize($message);
 
if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
$_SESSION['chatHistory'][$_POST['to']] = '';
}
 
$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
{
"s": "1",
"f": "{$to}",
"m": "{$messagesan}"
},
EOD;
 
 
unset($_SESSION['tsChatBoxes'][$_POST['to']]);
 
$sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
$query = mysql_query($sql);
echo "1";
exit(0);
}
 
function closeChat() {
 
unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
 
echo "1";
exit(0);
}
 
function sanitize($text) {
$text = htmlspecialchars($text, ENT_QUOTES);
$text = str_replace("\n\r","\n",$text);
$text = str_replace("\r\n","\n",$text);
$text = str_replace("\n","<br>",$text);
return $text;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Problemas con el chat en mi web

Publicado por xve (6935 intervenciones) el 12/04/2014 21:45:27
Hola Erick, no son errores son Notice... notificaciones de que la variable $_SESSION["username"] no esta definida.

Entiendo que tiene que existir dicha variable para mantener la conexión...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con el chat en mi web

Publicado por Erick (5 intervenciones) el 12/04/2014 22:30:46
Hola xve gracias por responder, puedes decirme como defino la variable para mantener la conexión,
domino muy poco php, solo se integrarlo en la web, pero me quedo corto a la hora de desarrollar variables, disculpa mi desconocimiento y gracias por tu ayuda
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con el chat en mi web

Publicado por Erick (5 intervenciones) el 13/04/2014 01:01:23
Hola xve no se si antes me he respondido a mi mismo o si te ha llegado el mensaje

gracias por responder, puedes decirme como defino la variable para mantener la conexión,
domino muy poco php, solo se integrarlo en la web, pero me quedo corto a la hora de desarrollar variables, disculpa mi desconocimiento y gracias por tu ayuda
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problemas con el chat en mi web

Publicado por Leonel (1 intervención) el 13/04/2014 07:24:08
$_SESSION["username"] = "colocar_nombre_de_usuario_acá";
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Problemas con el chat en mi web

Publicado por xve (6935 intervenciones) el 13/04/2014 10:15:10
Hola Erick, he estado buscando el chat al que haces referencia para ver como lo hacen y el único que he encontrado en: anantgarg.com es un chat de pago... es este http://www.cometchat.com/?r=ag4 el que has implementado?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con el chat en mi web

Publicado por Erick (5 intervenciones) el 13/04/2014 11:20:15
Hola xve
Este es el enlace http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/
Lo puedes descargar gratis si no es para uso comercial,
el caso es que en otro servidor (000webhost) donde tengo la web subida para hacer pruebas, me funciona bien con el código así
¿Tu sabes porque?
Gracias por tu ayuda
saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con el chat en mi web

Publicado por Erick (5 intervenciones) el 13/04/2014 12:16:15
Otra cosa que sucede, cuando cierro sesion me muestra todo el historial de lasconversaciones que intente mantener con la sesion iniciada,
Inicio sesion busco un usuario y escribo por el chat, pero no lleganlos mensajes
cierro sesion y aparece toda la conversacion

Gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar