Проблемы с чатом на сайте

215
19 декабря 2016, 19:34

Нашел в интернете гайд по созданию чата на сайте. Вроде делал всё, как там, но в поле не выводятся введённые сообщения.

  <?
// поле для ввода имени
session_start();
function loginForm(){
    echo'
    <div id="loginform">
    <form action="l_chat.php" method="post">
        <p>Please enter your name to continue:</p>
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" />
        <input type="submit" name="enter" id="enter" value="Enter" />
    </form>
    </div>
    ';
}
if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
    else{
        echo '<span class="error">Please type in a name</span>';
    }
}
?>
<?php
if(!isset($_SESSION['name'])){
    loginForm();
}
else{
?>
// поле для ввода сообщений и само поле с сообщениями
<div id="wrapper2">
    <div id="menu">
        <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
        <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
        <div style="clear:both"></div>
    </div>    
    <div id="chatbox"></div>
    <form name="message" action="l_chat.php">
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
    </form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
});
</script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
    //выход из чата
    $("#exit").click(function(){
        var exit = confirm("Are you sure you want to end the session?");
        if(exit==true){window.location = 'l_chat.php?logout=true';}     
    });
});
</script>
<?
if(isset($_GET['logout'])){ 

    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
    fclose($fp);
    session_destroy();
}
?>
<?
if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
    fclose($fp);
}
?>
<script>
    $("#submitmsg").click(function(){   
        var clientmsg = $("#usermsg").val();
        $.post("l_chat.php", {text: clientmsg});                
        $("#usermsg").attr("value", "");
        return false;
    });
</script>
<link type="text/css" rel="stylesheet" href="view.css" />
<?php
}
?>
<div id="chatbox"><?php
if(file_exists("log.html") && filesize("log.html") > 0){
    $handle = fopen("log.html", "r");
    $contents = fread($handle, filesize("log.html"));
    fclose($handle);
    echo $contents;
}
?></div>
<script>
// логи
function loadLog(){     
        var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
        $.ajax({
            url: "log.html",
            cache: false,
            success: function(html){        
                $("#chatbox").html(html); //Insert chat log into the #chatbox div   
                //Auto-scroll           
                var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
                if(newscrollHeight > oldscrollHeight){
                    $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
                }               
            },
        });
    }
    </script>
READ ALSO
WebSocket with apache_PHP - connection error during handshake

WebSocket with apache_PHP - connection error during handshake

На платформе OpenServer [Apache_24

257
OpenID и разные домены

OpenID и разные домены

Использую подобную библиотеку (https://githubcom/SmItH197/SteamAuthentication) пытаюсь сделать авторизацию, но не простую

191
Использовать очень долгую сессию в php

Использовать очень долгую сессию в php

Как можно увеличить срок жизни сессии если пользователь нажал "запомнить меня"Или никак и нужно использовать куки?

201