Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/08/2012, 04:49
edie8
 
Fecha de Ingreso: noviembre-2011
Mensajes: 516
Antigüedad: 13 años, 2 meses
Puntos: 10
problemas con un chat

Buenas tengo un problema tengo un chat que me guarda el nombre del usuario que abla en vez del id, si cambio para que me guarde el id ya no me sale el nombre en el chat sino este id, este es mi codigo aber si me podeis ayudar y dar ideas de como modificarlo:
chat.php
Código PHP:
Ver original
  1. <?php
  2. define ('DBPATH','localhost');
  3. define ('DBUSER','root');
  4. define ('DBPASS','pass');
  5. define ('DBNAME','muroredsocial');
  6.  
  7.  
  8. global $dbh;
  9. $dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
  10. mysql_selectdb(DBNAME,$dbh);
  11.  
  12. if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
  13. if ($_GET['action'] == "sendchat") { sendChat(); }
  14. if ($_GET['action'] == "closechat") { closeChat(); }
  15. if ($_GET['action'] == "startchatsession") { startChatSession(); }
  16.  
  17. if (!isset($_SESSION['chatHistory'])) {
  18.     $_SESSION['chatHistory'] = array();
  19. }
  20.  
  21. if (!isset($_SESSION['openChatBoxes'])) {
  22.     $_SESSION['openChatBoxes'] = array();  
  23. }
  24.  
  25. function chatHeartbeat() {
  26.    
  27.     $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' AND recd = 0) order by id ASC";
  28.     $query = mysql_query($sql);
  29.     $items = '';
  30.  
  31.     $chatBoxes = array();
  32.  
  33.     while ($chat = mysql_fetch_array($query)) {
  34.  
  35.         if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
  36.             $items = $_SESSION['chatHistory'][$chat['from']];
  37.         }
  38.  
  39.         $chat['message'] = sanitize($chat['message']);
  40.  
  41.         $items .= <<<EOD
  42.                        {
  43.             "s": "0",
  44.             "f": "{$chat['from']}",
  45.             "m": "{$chat['message']}"
  46.        },
  47. EOD;
  48.  
  49.     if (!isset($_SESSION['chatHistory'][$chat['from']])) {
  50.         $_SESSION['chatHistory'][$chat['from']] = '';
  51.     }
  52.  
  53.     $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
  54.                            {
  55.             "s": "0",
  56.             "f": "{$chat['from']}",
  57.             "m": "{$chat['message']}"
  58.        },
  59. EOD;
  60.        
  61.         unset($_SESSION['tsChatBoxes'][$chat['from']]);
  62.         $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
  63.     }
  64.  
  65.     if (!empty($_SESSION['openChatBoxes'])) {
  66.     foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
  67.         if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
  68.             $now = time()-strtotime($time);
  69.             $time = date('g:iA M dS', strtotime($time));
  70.  
  71.             $message = "Sent at $time";
  72.             if ($now > 180) {
  73.                 $items .= <<<EOD
  74. {
  75. "s": "2",
  76. "f": "$chatbox",
  77. "m": "{$message}"
  78. },
  79. EOD;
  80.  
  81.     if (!isset($_SESSION['chatHistory'][$chatbox])) {
  82.         $_SESSION['chatHistory'][$chatbox] = '';
  83.     }
  84.  
  85.     $_SESSION['chatHistory'][$chatbox] .= <<<EOD
  86.         {
  87. "s": "2",
  88. "f": "$chatbox",
  89. "m": "{$message}"
  90. },
  91. EOD;
  92.             $_SESSION['tsChatBoxes'][$chatbox] = 1;
  93.         }
  94.         }
  95.     }
  96. }
  97.  
  98.     $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' and recd = 0";
  99.     $query = mysql_query($sql);
  100.  
  101.     if ($items != '') {
  102.         $items = substr($items, 0, -1);
  103.     }
  104. header('Content-type: application/json');
  105. ?>
  106. {
  107.         "items": [
  108.             <?php echo $items;?>
  109.         ]
  110. }
  111.  
  112. <?php
  113.             exit(0);
  114. }
  115.  
  116. function chatBoxSession($chatbox) {
  117.    
  118.     $items = '';
  119.    
  120.     if (isset($_SESSION['chatHistory'][$chatbox])) {
  121.         $items = $_SESSION['chatHistory'][$chatbox];
  122.     }
  123.  
  124.     return $items;
  125. }
  126.  
  127. function startChatSession() {
  128.     $items = '';
  129.     if (!empty($_SESSION['openChatBoxes'])) {
  130.         foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
  131.             $items .= chatBoxSession($chatbox);
  132.         }
  133.     }
  134.  
  135.  
  136.     if ($items != '') {
  137.         $items = substr($items, 0, -1);
  138.     }
  139.  
  140. header('Content-type: application/json');
  141. ?>
  142. {
  143.         "username": "<?php echo $_SESSION['logueado_nombre'];?>",
  144.         "items": [
  145.             <?php echo $items;?>
  146.         ]
  147. }
  148.  
  149. <?php
  150.  
  151.  
  152.     exit(0);
  153. }
  154.  
  155. function sendChat() {
  156.     $from = $_SESSION['logueado_nombre'];
  157.     $to = $_POST['to'];
  158.     $message = $_POST['message'];
  159.  
  160.     $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
  161.    
  162.     $messagesan = sanitize($message);
  163.  
  164.     if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
  165.         $_SESSION['chatHistory'][$_POST['to']] = '';
  166.     }
  167.  
  168.     $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
  169.                        {
  170.             "s": "1",
  171.             "f": "{$to}",
  172.             "m": "{$messagesan}"
  173.        },
  174. EOD;
  175.  
  176.  
  177.     unset($_SESSION['tsChatBoxes'][$_POST['to']]);
  178.  
  179.     $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())";
  180.     $query = mysql_query($sql);
  181.     echo "1";
  182.     exit(0);
  183. }
  184.  
  185. function closeChat() {
  186.  
  187.     unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  188.    
  189.     echo "1";
  190.     exit(0);
  191. }
  192.  
  193. function sanitize($text) {
  194.     $text = htmlspecialchars($text, ENT_QUOTES);
  195.     $text = str_replace("\n\r","\n",$text);
  196.     $text = str_replace("\r\n","\n",$text);
  197.     $text = str_replace("\n","<br>",$text);
  198.     return $text;
  199. }
este es el que guardaria el id de cada usuario, el problema lo tengo en el ajax que muestra el chat.