Advertisement
matthewenderle

Index.php

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