Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. Step 5 : Displaying the Chat Log (log.html) Contents
  2.  
  3. Everything the user has posted is handled and posted using jQuery; it is written to the chat log with PHP. The only thing left to do is to display the updated chat log to the user.
  4.  
  5. In order to save ourselves some time, we will preload the chat log into the #chatbox div if it has any content.
  6.  
  7. -------------------------------------------------------------------------------------------------------------------
  8.  
  9. <div id="chatbox"><?php
  10. if(file_exists("log.html") && filesize("log.html") > 0){
  11. $handle = fopen("log.html", "r");
  12. $contents = fread($handle, filesize("log.html"));
  13. fclose($handle);
  14.  
  15. echo $contents;
  16. }
  17. ?></div>
  18.  
  19. -----------------------------------------------------------------------------------------------------------------------
  20.  
  21.  
  22. We use a similar routine as we used the post.php file, except this time we are only reading and outputting the contents of the file.
  23.  
  24. The ajax request is the core of everything we are doing. This request not only allows us to send and receive data throught the form without refreshing the page, but it also allows us to handle the data requested.
  25.  
  26.  
  27. ---------------------------------------------------------------------------------------------------------------------------
  28.  
  29. //Load the file containing the chat log
  30. function loadLog(){
  31.  
  32. $.ajax({
  33. url: "log.html",
  34. cache: false,
  35. success: function(html){
  36. $("#chatbox").html(html); //Insert chat log into the #chatbox div
  37. },
  38. });
  39. }
  40.  
  41. ------------------------------------------------------------------------------------------------------------------------------
  42.  
  43. We wrap our ajax request inside a function. As you see, we then move the data we requested (html) into the #chatbox div.
  44.  
  45. Auto-scrolling
  46. As you have seen in other chat applications, the content automatically scrolls down if the chat log container (#chatbox) overflows. We are going to implement a simple and similar feature, that will compare the container's scroll height before and after we do the ajax request. If the scroll height is greater after the request, we will use jQuery's animate effect to scroll the #chatbox div.
  47.  
  48. ----------------------------------------------------------------------------------------------------------------------------
  49.  
  50.  
  51. //Load the file containing the chat log
  52. function loadLog(){
  53. var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
  54. $.ajax({
  55. url: "log.html",
  56. cache: false,
  57. success: function(html){
  58. $("#chatbox").html(html); //Insert chat log into the #chatbox div
  59.  
  60. //Auto-scroll
  61. var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
  62. if(newscrollHeight > oldscrollHeight){
  63. $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
  64. }
  65. },
  66. });
  67. }
  68.  
  69. -------------------------------------------------------------------------------------------------------------------------
  70.  
  71. We first store the #chatbox div's scroll height into the oldscrollHeight variable before we make the request.
  72. After our request has returned sucessful, we store the #chatbox div's scrolle height into the newscrollHeight variable.
  73. We then compare both of the scroll height variables using an if statement. If the newscrollHeight is greater than the oldscrollHeight, we use the animate effect to scroll the #chatbox div.
  74.  
  75.  
  76. Continuously Updating the Chat Log
  77.  
  78. ----------------------------------------------------------------------------------------------------------------
  79. setInterval (loadLog, 2500);
  80. ----------------------------------------------------------------------------------------------------------------
  81.  
  82. This function will run our loadLog() function every 2.5 seconds, and the loadLog function will request the updated file and autoscroll the div.
  83.  
  84.  
  85. I hope i explained it as clear as i can. It became like a tutorial but i needed to explain every single code to gain trust, you probably know what those codes do, but like i said, i felt like i had to explain. Color codes, background and other non-lethal stuff can be managed easily. If you just place a page under your own domain, we can start our hacking roleplay without losing any time. Basicly this is just a chatbox app on a web page, i couldn't think more, because if dig deeper, illegal advertising and other stuff will pop-up and this till take lots of time. With this chat box appliication, we can connect to other illegal people like other hackers, hitmen etc. Also thanks for spending your time to read that! Have fun!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement