Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- index.php ---
- <?php
- session_start();
- include 'functions.php';
- if (!isset($_SESSION['is_logged'])) {
- if (!isset($_REQUEST['log_in'])) {
- echo '<html>
- <link rel="stylesheet" type="text/css" href="style.css">
- <body>
- <div>
- <form action="'.$_SERVER['SCRIPT_NAME'].'" method="POST">
- <input type="text" name="nick" value=""><br>
- <input type="submit" name="log_in" value="Войти">
- </form>
- </div>
- </body>
- </html>';
- } else {
- $_SESSION['is_logged'] = 1;
- exit("<meta http-equiv='refresh' content='0; url= $_SERVER[PHP_SELF]'>");
- }
- } else {
- db_connect();
- $post_count = db_get_post_count();
- echo '<html>
- <link rel="stylesheet" type="text/css" href="style.css">
- <body>
- <div>
- <form action="'.$_SERVER['SCRIPT_NAME'].'" method="POST">
- <textarea class="chat_field" readonly>';
- while($post_text = db_read_message()) {
- echo $post_text['time'];
- }
- echo ' </textarea>
- <textarea class="users_field" readonly></textarea><br>
- <input class="message_field" type="text" name="message_field" value="">
- <input type="submit" name="post_message" value="ОК">
- <input type="submit" name="log_out" value="Выйти">
- </form>
- </div>
- </body>
- </html>';
- if (isset($_REQUEST['log_out'])) {
- session_destroy();
- exit("<meta http-equiv='refresh' content='0; url= $_SERVER[PHP_SELF]'>");
- }
- }
- ?>
- --- functions.php ---
- <?php
- function db_connect() {
- include 'config.php';
- mysql_connect($host, $user, $password);
- mysql_select_db($database);
- }
- function db_post_message($nick, $message) {
- $time = date("H:i:s");
- $query = mysql_query("INSERT INTO chat (id, time, nick, message) VALUES ('', '$time', '$nick', '$message')");
- }
- function db_read_message() {
- $query = "SELECT * FROM chat ORDER BY `id`";
- $result = mysql_fetch_array(mysql_query($query));
- return $result;
- }
- function db_get_post_count() {
- $query = "SELECT COUNT(1) FROM chat";
- $result = mysql_fetch_array(mysql_query($query));
- return $result[0];
- }
- ?>
- --- config.php ---
- <?php
- $host = 'localhost'; // Имя хоста
- $database = 'chat'; // Имя базы данных
- $user = 'root'; // Имя пользователя
- $password = ''; // Пароль
- ?>
- --- style.css ---
- textarea.chat_field {
- resize: none;
- height: 97%;
- width: 89%;
- }
- textarea.users_field {
- resize: none;
- height: 97%;
- width: 10%;
- }
- input.message_field {
- width: 89%;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement