Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/03/2011, 12:52
Derzz
 
Fecha de Ingreso: enero-2011
Mensajes: 112
Antigüedad: 13 años, 3 meses
Puntos: 4
Respuesta: ayuda con tutorial

codigos
index.php
Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4. function loginForm(){
  5. echo
  6. '<div id="loginform">
  7. <form action="index.php" method="post">
  8. <p>Please enter your name to continue:</p>
  9. <label for="name">Name:</label>
  10. <input type="text" name="name" id="name" />
  11. <input type="submit" name="enter" id="enter" value="Enter" />
  12. </form>
  13. </div>'
  14. ;
  15. }
  16.  
  17. if(isset($_POST['enter'])){
  18. if($_POST['name'] != ""){
  19. $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
  20. }
  21. else{
  22. echo '<span class="error">Please type in a name</span>';
  23. }
  24. }
  25. //desloguea
  26. if(isset($_GET['logout'])){
  27.  
  28. //Mensaje simple de salida
  29. $fp = fopen("log.html", 'a');
  30. fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
  31. fclose($fp);
  32.  
  33. header("Location: index.php"); //Redirige al usuario
  34. }
  35.  
  36. //fin desloguea
  37. ?>
  38.  
  39.  
  40.  
  41. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  42. <html xmlns="http://www.w3.org/1999/xhtml">
  43. <head>
  44. <title>Chat - Customer Module</title>
  45. <link type="text/css" rel="stylesheet" href="style.css" />
  46. </head>
  47.  
  48. <?php
  49. if(!isset($_SESSION['name'])){
  50. loginForm();
  51. }
  52. else{
  53. ?>
  54. <div id="wrapper">
  55. <div id="menu">
  56. <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
  57. <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
  58. <div style="clear:both"></div>
  59. </div>
  60. <div id="chatbox"><?php
  61. if(file_exists("log.html") && filesize("log.html") > 0){
  62. $handle = fopen("log.html", "r");
  63. $contents = fread($handle, filesize("log.html"));
  64. fclose($handle);
  65.  
  66. echo $contents;
  67. }
  68. ?></div>
  69. <form name="message" action="">
  70. <input name="usermsg" type="text" id="usermsg" size="63" />
  71. <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
  72. </form>
  73. </div>
  74.  
  75.  
  76. <script type="text/javascript" src="jquery.min.js"></script>
  77. <script type="text/javascript">
  78.  
  79. $(document).ready(function(){
  80. //Si el usuario quiere dejar la sesión
  81. $("#exit").click(function(){
  82. var exit = confirm("¿Está seguro que desea finalizar la sesión?");
  83. if(exit==true){window.location = 'index.php?logout=true';}
  84. });
  85. //enviar en log de mensaje
  86. $("#submitmsg").click(function(){
  87. var clientmsg = $("#usermsg").val();
  88. $.post("post.php", {text: clientmsg});
  89. $("#usermsg").attr("value", "");
  90. return false;
  91. });
  92. //Carga el archivo que contiene el log de chat
  93. function loadLog(){//Inicio de loadLog
  94. var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //La altura del scroll antes de la petición
  95. $.ajax({//Inicio ajax
  96. url: "log.html",
  97. cache: true,
  98. success: function(html){//Inicio del success
  99. $("#chatbox").html(html); //Inserta el log de chat en el div #chatbox
  100. //Auto-scroll
  101. var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //La altura del scroll después del pedido
  102. if(newscrollHeight > oldscrollHeight){//inicio del if verificar scroll
  103. $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll hacia el fondo del div
  104. }//Fin del if verificar scroll
  105. }//Fin del success
  106. });//Fin ajax
  107. }//Fin de loadLog
  108. //Recarga el archivo cada 2500 ms o x ms si deseas cambiar el segundo parámetro
  109. setInterval (loadLog, 2500);
  110. });//fin del Jquery
  111.  
  112.  
  113. </script>
  114. <?php
  115. }
  116. ?>
  117.  
  118.  
  119.  
  120.  
  121.  
  122. </body>
  123. </html>

post.php
Código PHP:
Ver original
  1. <?
  2. if(isset($_SESSION['name'])){
  3. $text = $_POST["text"];
  4.  
  5. $fp = fopen("log.html", 'a');
  6. fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
  7. fclose($fp);
  8. }
  9. ?>

style.css

Código CSS:
Ver original
  1. @charset "utf-8";
  2. /* CSS Document */
  3. /* CSS Document */
  4. body {
  5. font:12px arial;
  6. color: #222;
  7. text-align:center;
  8. padding:35px; }
  9.  
  10. form, p, span {
  11. margin:0;
  12. padding:0; }
  13.  
  14. input { font:12px arial; }
  15.  
  16. a {
  17. color:#0000FF;
  18. text-decoration:none; }
  19.  
  20. a:hover { text-decoration:underline; }
  21.  
  22. #wrapper, #loginform {
  23. margin:0 auto;
  24. padding-bottom:25px;
  25. background:#EBF4FB;
  26. width:504px;
  27. border:1px solid #ACD8F0; }
  28.  
  29. #loginform { padding-top:18px; }
  30.  
  31. #loginform p { margin: 5px; }
  32.  
  33. #chatbox {
  34. text-align:left;
  35. margin:0 auto;
  36. margin-bottom:25px;
  37. padding:10px;
  38. background:#fff;
  39. height:270px;
  40. width:430px;
  41. border:1px solid #ACD8F0;
  42. overflow:auto; }
  43.  
  44. #usermsg {
  45. width:395px;
  46. border:1px solid #ACD8F0; }
  47.  
  48. #submit { width: 60px; }
  49.  
  50. .error { color: #ff0000; }
  51.  
  52. #menu { padding:12.5px 25px 12.5px 25px; }
  53.  
  54. .welcome { float:left; }
  55.  
  56. .logout { float:right; }
  57.  
  58. .msgln { margin:0 0 2px 0; }