Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.16 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style>
  4. .message {
  5.    overflow:hidden;
  6.    width:498px;
  7.    margin-bottom:5px;
  8.    border:1px solid #999;
  9. }
  10. .messagehead {
  11.    overflow:hidden;
  12.    background:#FFC;
  13.    width:500px;
  14. }
  15. .messagecontent {
  16.    overflow:hidden;
  17.    width:496px;
  18. }
  19. </style>
  20. </head>
  21.  
  22. <body>
  23. <div id="chat" style="width:500px;margin:0 auto;overflow:hidden;">
  24. //This div will contain the messages
  25. <div id="messages"></div>
  26. //This div will contain an eventual error message
  27. <div id="error" style="width:500px;text-align:center;color:red;"></div>
  28. <div id="write" style="text-align:center;"><textarea id="message" cols="50" rows="5"></textarea><br/>Name:<input type="text" id="name"/><input type="button" value="Send" onClick="send();"/></div>
  29. </div>
  30.  
  31. <script type="text/javascript">
  32. //This function will display the messages
  33. function showmessages(){
  34.    //Send an XMLHttpRequest to the 'show-message.php' file
  35.    if(window.XMLHttpRequest){
  36.       xmlhttp = new XMLHttpRequest();
  37.       //And here is the line what makes it cross-browser:
  38.       xmlhttp.open("GET","show-messages.php?" + Math.random(),false);
  39.       xmlhttp.send(null);
  40.    }
  41.    else{
  42.       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  43.       xmlhttp.open("GET","showmessages.php?" + Math.random(),false);
  44.       xmlhttp.send();
  45.    }
  46.    //Replace the content of the messages with the response from the 'show-messages.php' file
  47.    document.getElementById('messages').innerHTML = xmlhttp.responseText;
  48.    //Repeat the function each 1 seconds
  49.    setTimeout('showmessages()',1000);
  50. }
  51. //Start the showmessages() function
  52. showmessages();
  53. //This function will submit the message
  54. function send(){
  55.    //Send an XMLHttpRequest to the 'send.php' file with all the required informations
  56.    var sendto = 'send.php?message=' + document.getElementById('message').value + '&name=' + document.getElementById('name').value;
  57.    if(window.XMLHttpRequest){
  58.       xmlhttp = new XMLHttpRequest();
  59.       xmlhttp.open("GET",sendto,false);
  60.       xmlhttp.send(null);
  61.    }
  62.    else{
  63.       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  64.       xmlhttp.open("GET",sendto,false);
  65.       xmlhttp.send();
  66.    }
  67.    var error = '';
  68.    //If an error occurs the 'send.php' file send`s the number of the error and based on that number a message is displayed
  69.    switch(parseInt(xmlhttp.responseText)){
  70.    case 1:
  71.       error = 'The database is down!';
  72.       break;
  73.    case 2:
  74.       error = 'The database is down!';
  75.       break;
  76.    case 3:
  77.       error = 'Don`t forget the message!';
  78.       break;
  79.    case 4:
  80.       error = 'The message is too long!';
  81.       break;
  82.    case 5:
  83.       error = 'Don`t forget the name!';
  84.       break;
  85.    case 6:
  86.       error = 'The name is too long!';
  87.       break;
  88.    case 7:
  89.       error = 'This name is already used by somebody else!';
  90.       break;
  91.    case 8:
  92.       error = 'The database is down!';
  93.    }
  94.    if(error == ''){
  95.       document.getElementById('error').innerHTML = '';
  96.       showmessages();
  97.    }
  98.    else{
  99.       document.getElementById('error').innerHTML = error;
  100.    }
  101. }
  102. </script>
  103.  
  104. </body>
  105. </html>
  106.  
  107. // other file
  108.  
  109. <?php
  110. //Connect to MySQL
  111. mysql_connect('localhost', 'rune', 'gz087x') or die (1);
  112. //Select database
  113. mysql_select_db('test') or die (2);
  114. //Check if message is empty and send the error code
  115. if(strlen($message) < 1){
  116.   echo 3;
  117. }
  118. //Check if message is too long
  119. else if(strlen($message) > 255){
  120.    echo 4;
  121. }
  122. //Check if name is empty
  123. else if(strlen($name) < 1){
  124.   echo 5;
  125. }
  126. //Check if name is too long
  127. else if(strlen($name) > 29){
  128.    echo 6;
  129. }
  130. //Check if the name is used by somebody else
  131. else if(mysql_num_rows(mysql_query("select * from chat where name = '" . $name . "' and ip != '" . @$REMOTE_ADDR . "'")) != 0){
  132.    echo 7;
  133. }
  134. //If everything is fine
  135. else{
  136.    //This array contains the characters what will be removed from the message and name, because else somebody could send redirection script or links
  137.    $search = array("<",">",">","<");
  138.   //Insert a new row in the chat table
  139.   mysql_query("insert into chat values ('" . time() . "', '" . str_replace($search,"",$name) . "', '" . @$REMOTE_ADDR . "', '" . str_replace($search,"",$message) . "')") or die(8);
  140. }
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement