1

I have a multi user chat application with four files as under:

log.html,
index.php,
post.php and 
style.css

I have installed these on server (host :megaleech.in) & there are two following concerns that I am facing:

a) chat inputs do not reflect inside chat box when user press the send button. b) We need to press exit button two times instead of one to take the user to login form.

You can see it on domain: http://demo.megaleech.in/chting/

It should be noted that this chat application is working perfectly fine on wamp server on my personal laptop and above mentioned concerns are only when I upload files to said internet server.

Please help in resolving concern.

Thanks in advance.

My codes are as under:

log.html : This is empty file

index.php

<?php
    session_start();
     function loginForm(){
        echo'
        <div id="loginForm">
        <form action="index.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{
    ?>
    <doctype html>
    <head>
    <title>Chat - Customer Module</title>
    <link type="text/css" rel="stylesheet" href="style.css" />
    </head>
    <div id="wrapper">
        <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"><?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 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(){
        //If user wants to end session
        $("#exit").click(function(){
            var exit = confirm("Are you sure you want to end the session?");
            if(exit==true){window.location = 'index.php?logout=true';}      
        });
    });
    $("#submitmsg").click(function(){   
            var clientmsg = $("#usermsg").val();
            $.post("post.php", {text: clientmsg});              
            $("#usermsg").attr("value", "");
            return false;
        });

        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
                    }               
                },
            });
        }
    setInterval("loadLog() ",1000); 
    </script>
    <html>
    <?php

}
?>
<?php
if(isset($_GET['logout'])){ 

    //Simple exit message
    $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: index.php"); 
}
?>

style.css

body {
    font:12px arial;
    color: #222;
    text-align:center;
    padding:35px; }

form, p, span {
    margin:0;
    padding:0; }

input { font:12px arial; }

a {
    color:#0000FF;
    text-decoration:none; }

    a:hover { text-decoration:underline; }

#wrapper, #loginform {
    margin:0 auto;
    padding-bottom:25px;
    background:#EBF4FB;
    width:504px;
    border:1px solid #ACD8F0; }

#loginform { padding-top:18px; }

    #loginform p { margin: 5px; }

#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    height:270px;
    width:430px;
    border:1px solid #ACD8F0;
    overflow:auto; }

#usermsg {
    width:395px;
    border:1px solid #ACD8F0; }

#submit { width: 60px; }

.error { color: #ff0000; }

#menu { padding:12.5px 25px 12.5px 25px; }

.welcome { float:left; }

.logout { float:right; }

.msgln { margin:0 0 2px 0; }

post.php

<?php
    session_start();
    if(isset($_SESSION['name'])){
        $text = $_POST['text'];

        $fp = fopen("log.html", 'a');
        fwrite($fp, "<div class='msgln' id='chat".date("U")."'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
        fclose($fp);
    }
    ?>
user3811050
  • 467
  • 1
  • 3
  • 12
  • 1
    holy cow. you do realize that the second argument to `setInterval` is in milliseconds (i.e. 1/1000th of a second). So you are starting a new ajax request roughly every 16 milliseconds (minimum time for `setInterval`) or roughly 60 times every second. Press F12 and view the net tab to see your ajax requests. You are likely just having issues with the browser not being able to keep up. – Jonathan Kuhn Feb 09 '15 at 18:08
  • I have modified time from one milli second to one second and even for three seconds to test but still it is not working – user3811050 Feb 09 '15 at 18:15
  • Check your error log. I can see another small issue this is sending a header call at the bottom of your page after there has been output. That is likely why you need to click exit chat twice. – Jonathan Kuhn Feb 09 '15 at 18:26
  • Getting following error on error log: [09-Feb-2015 18:27:14 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/downlo14/public_html/demo/chting/index.php:26) in /home/downlo14/public_html/demo/chting/index.php on line 111 – user3811050 Feb 09 '15 at 18:30
  • Thats the error I was talking about. Move your logout code to the top of the page right below the loginForm definition and see here (http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) for explanation of problem. – Jonathan Kuhn Feb 09 '15 at 18:36
  • This has solved exit page concern but not first one, at present i have put time as one second – user3811050 Feb 09 '15 at 18:48
  • Chatbox is updating after around 1 minute – user3811050 Feb 09 '15 at 18:51

0 Answers0