Advertisement
Guest User

Untitled

a guest
Dec 1st, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. index.php
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <title>Chat - Customer Module</title>
  7. <style>
  8. /* CSS Document */
  9. body {
  10.     font:12px arial;
  11.     color: #222;
  12.     text-align:center;
  13.     padding:35px; }
  14.  
  15. form, p, span {
  16.     margin:0;
  17.     padding:0; }
  18.  
  19. input { font:12px arial; }
  20.  
  21. a {
  22.     color:#0000FF;
  23.     text-decoration:none; }
  24.  
  25.     a:hover { text-decoration:underline; }
  26.  
  27. #wrapper, #loginform {
  28.     margin:0 auto;
  29.     padding-bottom:25px;
  30.     background:#EBF4FB;
  31.     width:504px;
  32.     border:1px solid #ACD8F0; }
  33.  
  34. #loginform { padding-top:18px; }
  35.  
  36.     #loginform p { margin: 5px; }
  37.  
  38. #chatbox {
  39.     text-align:left;
  40.     margin:0 auto;
  41.     margin-bottom:25px;
  42.     padding:10px;
  43.     background:#fff;
  44.     height:270px;
  45.     width:430px;
  46.     border:1px solid #ACD8F0;
  47.     overflow:auto; }
  48.  
  49. #usermsg {
  50.     width:395px;
  51.     border:1px solid #ACD8F0; }
  52.  
  53. #submit { width: 60px; }
  54.  
  55. .error { color: #ff0000; }
  56.  
  57. #menu { padding:12.5px 25px 12.5px 25px; }
  58.  
  59. .welcome { float:left; }
  60.  
  61. .logout { float:right; }
  62.  
  63. .msgln { margin:0 0 2px 0; }
  64. </style>
  65. </head>
  66.  <?php
  67. session_start();
  68.  
  69. function loginForm(){
  70.     echo'
  71.     <div id="loginform">
  72.     <form action="index.php" method="post">
  73.         <p>Please enter your name to continue:</p>
  74.         <label for="name">Name:</label>
  75.         <input type="text" name="name" id="name" />
  76.         <input type="submit" name="enter" id="enter" value="Enter"/>
  77.     </form>
  78.     </div>
  79.     ';
  80. }
  81.  
  82. if(isset($_POST['enter'])){
  83.     if($_POST['name'] != ""){
  84.         $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
  85.     }
  86.     else{
  87.         echo '<span class="error">Please type in a name</span>';
  88.     }
  89. }
  90. ?>
  91. <?php
  92. if(!isset($_SESSION['name'])){
  93.     loginForm();
  94. }
  95. else{
  96. ?>
  97. <div id="wrapper">
  98.     <div id="menu">
  99.         <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
  100.         <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
  101.         <div style="clear:both"></div>
  102.     </div>    
  103.     <div id="chatbox"><?php
  104.     if(file_exists("log.html") && filesize("log.html") > 0){
  105.         $handle = fopen("log.html", "r");
  106.         $contents = fread($handle, filesize("log.html"));
  107.         fclose($handle);
  108.          
  109.         echo $contents;
  110.     }
  111.     ?></div>
  112.      
  113.     <form name="message" action="post.php" method="post" id="message">
  114.         <input name="usermsg" type="text" id="usermsg" size="63" />
  115.         <input name="submitmsg" type="submit"  id="submitmsg" value="Send" onclick="$('#message')[0].reset();" />
  116.     </form>
  117. </div>
  118.  
  119. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  120.  
  121. <script type="text/javascript">
  122. // jQuery Document
  123. $(document).ready(function(){
  124. });
  125. </script>
  126.  
  127. <script type="text/javascript">
  128. // jQuery Document
  129. $(document).ready(function(){
  130.     //If user wants to end session
  131.     $("#exit").click(function(){
  132.         var exit = confirm("Are you sure you want to end the session?");
  133.         if(exit==true){window.location = 'index.php?logout=true';}      
  134.     });
  135. });
  136.  
  137. // ------------------------------------------------------------------------------------------
  138. // Вот эта часть вроде нужна
  139.     $("#submitmsg").click(function(){  
  140.         var usermsg = $("#usermsg").val();
  141.         $.post("post.php", {text: usermsg});              
  142.         $("#usermsg").attr("value", "");
  143.         return false;
  144.     });
  145. // ------------------------------------------------------------------------------------------
  146.  
  147. //Load the file containing the chat log
  148. setInterval (loadLog, 1500);    //Reload file every 2500 ms or x ms if you wish to change the second parameter
  149.     function loadLog(){    
  150.  
  151.         $.ajax({
  152.             url: "log.html",
  153.             cache: false,
  154.             success: function(html){        
  155.                 $("#chatbox").html(html); //Insert chat log into the #chatbox div              
  156.             },
  157.         });
  158.     }
  159.  
  160. //Load the file containing the chat log
  161.     function loadLog(){    
  162.         var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
  163.         $.ajax({
  164.             url: "log.html",
  165.             cache: false,
  166.             success: function(html){        
  167.                 $("#chatbox").html(html); //Insert chat log into the #chatbox div  
  168.                  
  169.                 //Auto-scroll          
  170.                 var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
  171.                 if(newscrollHeight > oldscrollHeight){
  172.                     $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
  173.                 }              
  174.             },
  175.         });
  176.     }
  177. </script>
  178.  
  179. <?php
  180. if(isset($_GET['logout'])){
  181.      
  182.     //Simple exit message
  183.     $fp = fopen("log.html", 'a');
  184.     fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
  185.     fclose($fp);
  186.      
  187.     session_destroy();
  188.     header("Location: index.php"); //Redirect the user
  189. }
  190. ?>
  191. <?php
  192. }
  193. ?>
  194.  
  195. </body>
  196. </html>
  197.  
  198. ------------------------------------------------------------------------------------------------------------------
  199.  
  200. post.php
  201.  
  202. <?php
  203. session_start();
  204. if(isset($_SESSION['name'])){
  205.     $text = $_POST['usermsg'];
  206.      
  207.     $fp = fopen("log.html", 'a');
  208.     fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
  209.     fclose($fp);
  210. }
  211. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement