PHP消息没有写入日志文件 - tutsplus.com教程

时间:2015-01-15 21:51:45

标签: javascript php jquery

tutsplus,您将找到一个指导您完成如何创建简单网络聊天的步骤的教程。我试图遵循所说的一切,但我在测试时发现了一个问题。似乎usermsg未发布到log.html文件中。

这里是index.php,在本例中名为chat.php:

<?php
    function loginForm() {
        echo '
        <div id="loginform">
            <form action="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>';
        }
    }   

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Basic Chat Service</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css" title="style" type="text/css" media="screen" charset="utf-8">
    </head>
    <body>
        <?php
            if(!isset($_SESSION['name'])) {
                loginForm();
            }else{
        ?>
        <div id="wrapper">
            <div id="menu">
                <div class="welcome">Welcome, <?php echo $_SESSION["name"];  ?></div>
                <div class="logout"><a href="#" id="exit">Exit Chat</a></div>
                <div style="clear:both;"></div>
            </div>
            <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>
            <form name="message" action="">
                <input type="text" name="usermsg" id="usermsg" size="63">
                <input type="submit" name="submitmsg" id="submitmsg" value="Send">  
            </form>
        </div>  
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" charset="utf-8"></script>
        <script type="text/javascript">
            //jQuery Document
            $(document).ready(function() {
                $("#exit").click(function() {
                    var exit = confirm("Are you sure you want to logout?");
                    if(exit == true) {
                        window.location = 'chat.php?logout=true';
                    }
                });

            $("#submitmsg").click(function() {
                var clientmsg = $("#usermsg").val();
                console.log(clientmsg);
                $.post("post.php", {text: clientmsg});
                $("#usermsg").attr("value", "");
                return false;
            });

            function loadLog() {
                var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
                $.ajax({
                    url:"log.html",
                    cache: false,
                    success: function(html){
                            $("#chatbox").html(html);

                            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
                            if(newscrollHeight > oldscrollHeight) {
                                $("#chatbox").animate({scrollTop: newscrollHeight}, 'normal');
                            }
                        }
                });
            }

            setInterval(loadLog, 2500);
            });         
        </script>           
        <?php       
            }   
            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();
                header("Location: chat.php");
            }
        ?>
    </body>
</html>

这是post.php:

<?php
    session_start();

    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);
    }

?>

我正在使用MAMP,文件位于htdocs文件夹中,因此不存在问题。

提前感谢您提供给我的任何帮助,如果您需要更多信息,请告诉我们。

1 个答案:

答案 0 :(得分:0)

您应该在index.php中调用session_start()

from Marc B in the comments)功能