Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. --- index.php ---
  2. <?php
  3.  
  4. session_start();
  5.  
  6. include 'functions.php';
  7.  
  8. if (!isset($_SESSION['is_logged'])) {
  9.  
  10. if (!isset($_REQUEST['log_in'])) {
  11.  
  12. echo '<html>
  13. <link rel="stylesheet" type="text/css" href="style.css">
  14. <body>
  15. <div>
  16. <form action="'.$_SERVER['SCRIPT_NAME'].'" method="POST">
  17. <input type="text" name="nick" value=""><br>
  18. <input type="submit" name="log_in" value="Войти">
  19. </form>
  20. </div>
  21. </body>
  22. </html>';
  23.  
  24. } else {
  25.  
  26. $_SESSION['is_logged'] = 1;
  27. exit("<meta http-equiv='refresh' content='0; url= $_SERVER[PHP_SELF]'>");
  28.  
  29. }
  30.  
  31. } else {
  32.  
  33. db_connect();
  34.  
  35. $post_count = db_get_post_count();
  36.  
  37. echo '<html>
  38. <link rel="stylesheet" type="text/css" href="style.css">
  39. <body>
  40. <div>
  41. <form action="'.$_SERVER['SCRIPT_NAME'].'" method="POST">
  42. <textarea class="chat_field" readonly>';
  43.  
  44.  
  45.  
  46. while($post_text = db_read_message()) {
  47. echo $post_text['time'];
  48.  
  49. }
  50.  
  51. echo ' </textarea>
  52. <textarea class="users_field" readonly></textarea><br>
  53. <input class="message_field" type="text" name="message_field" value="">
  54. <input type="submit" name="post_message" value="ОК">
  55. <input type="submit" name="log_out" value="Выйти">
  56. </form>
  57. </div>
  58. </body>
  59. </html>';
  60.  
  61. if (isset($_REQUEST['log_out'])) {
  62.  
  63. session_destroy();
  64. exit("<meta http-equiv='refresh' content='0; url= $_SERVER[PHP_SELF]'>");
  65.  
  66. }
  67.  
  68. }
  69.  
  70. ?>
  71.  
  72. --- functions.php ---
  73. <?php
  74.  
  75. function db_connect() {
  76. include 'config.php';
  77. mysql_connect($host, $user, $password);
  78. mysql_select_db($database);
  79. }
  80.  
  81. function db_post_message($nick, $message) {
  82. $time = date("H:i:s");
  83. $query = mysql_query("INSERT INTO chat (id, time, nick, message) VALUES ('', '$time', '$nick', '$message')");
  84. }
  85.  
  86. function db_read_message() {
  87. $query = "SELECT * FROM chat ORDER BY `id`";
  88. $result = mysql_fetch_array(mysql_query($query));
  89. return $result;
  90.  
  91. }
  92.  
  93. function db_get_post_count() {
  94. $query = "SELECT COUNT(1) FROM chat";
  95. $result = mysql_fetch_array(mysql_query($query));
  96. return $result[0];
  97. }
  98.  
  99. ?>
  100.  
  101. --- config.php ---
  102. <?php
  103.  
  104. $host = 'localhost'; // Имя хоста
  105. $database = 'chat'; // Имя базы данных
  106. $user = 'root'; // Имя пользователя
  107. $password = ''; // Пароль
  108.  
  109. ?>
  110.  
  111. --- style.css ---
  112. textarea.chat_field {
  113. resize: none;
  114. height: 97%;
  115. width: 89%;
  116. }
  117.  
  118. textarea.users_field {
  119. resize: none;
  120. height: 97%;
  121. width: 10%;
  122. }
  123.  
  124. input.message_field {
  125. width: 89%;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement