Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Skrypt Czatu, Prosze o pomoc !
rozkosz2514
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 6.08.2012

Ostrzeżenie: (0%)
-----


Mam pewien skrypt czatu, niestety jestem początkujący więc nie wiem jak to zrobić aby po zalogowaniu na stronę w miejscu gdzie wpisałem ID i LOGIN pojawiały się dane zalogowanej osoby. Dzięki temu czat działał by automatycznie.
Skrypt Czatu:
  1. <?php
  2. $_SESSION['chatuser'] = 'ID';
  3. $_SESSION['chatuser_name'] = 'LOGIN'; //; Must be already set
  4. ?>
  5.  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd" >
  7.  
  8. <html>
  9. <head>
  10. <title>Live Demo | Simulating gmail, facebook type simple chat application using css, jQuery and PHP free @ blog.afriend.in with space and special character support</title>
  11. <style>
  12. body {
  13. background-color: #eeeeee;
  14. padding:0;
  15. margin:0 auto;
  16. font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
  17. font-size:11px;
  18. }
  19. </style>
  20.  
  21. <link type="text/css" rel="stylesheet" media="all" href="css/chat.css" />
  22. <link type="text/css" rel="stylesheet" media="all" href="css/screen.css" />
  23.  
  24. <!--[if lte IE 7]>
  25. <link type="text/css" rel="stylesheet" media="all" href="css/screen_ie.css" />
  26. <![endif]-->
  27.  
  28. </head>
  29. <body>
  30. <div id="main_container" align="center">
  31. <div style="background-color:#f2f2f2">
  32. <a href="http://blog.afriend.in"><img src="http://blog.afriend.in/blog.afriend.in.png"/></a>
  33. </div>
  34.  
  35. <H1>Live Demo | Free gmail, facebook type chat application using CSS, jQuery and PHP @ <a href="http://blog.afriend.in">blog.afriend.in</a></H1>
  36. -> Supports both space and special characters <-
  37. <h2><a style='color:green' href="java script:void(0)" onclick="java script:chatWith('1','Swadesh')">Chat with Swadesh</a></h2>
  38. <h2><a style='color:green' href="java script:void(0)" onclick="java script:chatWith('3','Vimla')">Chat with Vimla</a></h2>
  39. <!-- YOUR BODY HERE -->
  40. <p>&nbsp;</p>
  41. <p>&nbsp;</p>
  42. <p>&nbsp;</p>
  43. <p>&nbsp;</p>
  44. <p>&nbsp;</p>
  45. <p>&nbsp;</p>
  46. <p>&nbsp;</p>
  47. <p>&nbsp;</p>
  48.  
  49. <h2>Current User Session</h2> <?=$_SESSION['chatuser_name']?> (ID = <?=$_SESSION['chatuser']?> )
  50. <p>&nbsp;</p>
  51. <p>&nbsp;</p>
  52. <p>&nbsp;</p>
  53. <p>&nbsp;</p>
  54. <p>&nbsp;</p>
  55. <h2> <a href="http://itswadesh.wordpress.com/2011/05/07/gmail-facebook-style-jquery-chat/">Back to tutorial</a></h2>
  56. </div>
  57.  
  58. <script type="text/javascript" src="js/jquery.min.js"></script>
  59. <script type="text/javascript" src="js/chat.js"></script>
  60.  
  61. </body>
  62. </html>

  1. <?php
  2. define('DB_SERVER', 'mysql.cba.pl');
  3. define('DB_USERNAME', '');
  4. define('DB_PASSWORD', '');
  5. define('DB_DATABASE', '');
  6. $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
  7. $database = mysql_select_db(DB_DATABASE) or die(mysql_error());
  8.  
  9. if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
  10. if ($_GET['action'] == "sendchat") { sendChat(); }
  11. if ($_GET['action'] == "closechat") { closeChat(); }
  12. if ($_GET['action'] == "startchatsession") { startChatSession(); }
  13. if ($_GET['action'] == "chatname") { chatName(); }
  14. if ($_GET['login'] == "login") { login(); }
  15.  
  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. $sql = "select rejestracja.login,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd from chat,rejestracja where (chat.to = '".mysql_real_escape_string($_SESSION['chatuser'])."' AND recd = 0) and chat.from=rejestracja.uid order by id ASC";
  27.  
  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. "u": "{$chat['login']}",
  45. "f": "{$chat['from']}",
  46. "m": "{$chat['message']}"
  47. },
  48. EOD;
  49.  
  50. if (!isset($_SESSION['chatHistory'][$chat['from']])) {
  51. $_SESSION['chatHistory'][$chat['from']] = '';
  52. }
  53.  
  54. $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
  55. {
  56. "s": "0",
  57. "u": "{$chat['login']}",
  58. "f": "{$chat['from']}",
  59. "m": "{$chat['message']}"
  60. },
  61. EOD;
  62.  
  63. unset($_SESSION['tsChatBoxes'][$chat['from']]);
  64. $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
  65. }
  66.  
  67. if (!empty($_SESSION['openChatBoxes'])) {
  68. foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
  69. if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
  70. $now = time()-strtotime($time);
  71. $time = date('g:iA M dS', strtotime($time));
  72.  
  73. $message = "Sent at $time";
  74. if ($now > 180) {
  75. $items .= <<<EOD
  76. {
  77. "s": "2",
  78. "f": "$chatbox",
  79. "m": "{$message}"
  80. },
  81. EOD;
  82.  
  83. if (!isset($_SESSION['chatHistory'][$chatbox])) {
  84. $_SESSION['chatHistory'][$chatbox] = '';
  85. }
  86.  
  87. $_SESSION['chatHistory'][$chatbox] .= <<<EOD
  88. {
  89. "s": "2",
  90. "f": "$chatbox",
  91. "m": "{$message}"
  92. },
  93. EOD;
  94. $_SESSION['tsChatBoxes'][$chatbox] = 1;
  95. }
  96. }
  97. }
  98. }
  99.  
  100. $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['chatuser'])."' and recd = 0";
  101. $query = mysql_query($sql);
  102.  
  103. if ($items != '') {
  104. $items = substr($items, 0, -1);
  105. }
  106. header('Content-type: application/json');
  107. ?>
  108. {
  109. "items": [
  110. <?php echo $items;?>
  111. ]
  112. }
  113.  
  114. <?php
  115. exit(0);
  116. }
  117.  
  118. function chatBoxSession($chatbox) {
  119.  
  120. $items = '';
  121.  
  122. if (isset($_SESSION['chatHistory'][$chatbox])) {
  123. $items = $_SESSION['chatHistory'][$chatbox];
  124. }
  125.  
  126. return $items;
  127. }
  128.  
  129. function startChatSession() {
  130. $items = '';
  131. if (!empty($_SESSION['openChatBoxes'])) {
  132. foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
  133. $items .= chatBoxSession($chatbox);
  134. }
  135. }
  136.  
  137.  
  138. if ($items != '') {
  139. $items = substr($items, 0, -1);
  140. }
  141.  
  142. header('Content-type: application/json');
  143. /*
  144. $suser=$_SESSION['chatuser'];
  145. $sc=mysql_query("select login from rejestracja where login='$su'");
  146. while($row_sc=mysql_fetch_array($sc))
  147. {
  148. $_SESSION['current_chat_username']=$row_sc['login'];
  149. }*/
  150. ?>
  151. {
  152. "username": "<?php echo $_SESSION['chatuser'];?>",
  153. "items": [
  154. <?php echo $items;?>
  155. ]
  156. }
  157.  
  158. <?php
  159. exit(0);
  160.  
  161. }
  162.  
  163. function chatName() {
  164. $un = '';
  165.  
  166. $su=$_GET['usw'];
  167. $sc2=mysql_query("select login from rejestracja where uid='$su' limit 1");
  168. while($row_sc2=mysql_fetch_array($sc2))
  169. {
  170. $un=$row_sc2["login"];
  171. }
  172. ?>
  173. {
  174. "unm": ["<?php echo $un;?>"]
  175.  
  176. }
  177.  
  178. <?php
  179.  
  180.  
  181. exit(0);
  182. }
  183.  
  184.  
  185.  
  186. function sendChat() {
  187. $from = $_SESSION['chatuser'];
  188. $to = $_POST['to'];
  189. $message = $_POST['message'];
  190. $sql = "select rejestracja.login from rejestracja where rejestracja.uid='$from' limit 1";
  191. $uname = mysql_query($sql);
  192. $from_user='';
  193. while ($un = mysql_fetch_array($uname)) {
  194. $from_user=$un['login'];
  195. }
  196.  
  197.  
  198. $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
  199.  
  200. $messagesan = sanitize($message);
  201.  
  202. if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
  203. $_SESSION['chatHistory'][$_POST['to']] = '';
  204. }
  205.  
  206. $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
  207. {
  208. "s": "1",
  209. "u": "{$from_user}",
  210. "f": "{$to}",
  211. "m": "{$messagesan}"
  212. },
  213. EOD;
  214.  
  215.  
  216. unset($_SESSION['tsChatBoxes'][$_POST['to']]);
  217.  
  218. $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())";
  219. $query = mysql_query($sql);
  220. echo "1";
  221. exit(0);
  222. }
  223.  
  224. function closeChat() {
  225.  
  226. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  227.  
  228. echo "1";
  229. exit(0);
  230. }
  231.  
  232. function sanitize($text) {
  233. $text = htmlspecialchars($text, ENT_QUOTES);
  234. $text = str_replace("\n\r","\n",$text);
  235. $text = str_replace("\r\n","\n",$text);
  236. $text = str_replace("\n","<br>",$text);
  237. return $text;
  238. }

Dodatkowo zauważyłem że jak napisze cos na tym czacie do innej osoby to się jej to nie wyświetla (trzeba odświeżyć stronę)

Ten post edytował rozkosz2514 6.08.2012, 13:30:34
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 03:12