Advertisement
Guest User

ChatBox

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