0

I have been created php online chat.

Here is my 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>';
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat</title>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>



<?php
if(!isset($_SESSION['name'])){
    loginForm();
}
else{
?>
<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"></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(){
    //If user submits the form
    $("#submitmsg").click(function(){   
        var clientmsg = $("#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        return false;
    });

    //Load the file containing the chat log
    function loadLog(){     

        $.ajax({
            url: "log.html",
            cache: false,
            success: function(html){        
                $("#chatbox").html(html); //Insert chat log into the #chatbox div               
            },
        });
    }

    //Load the file containing the chat log
    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, 2500);    //Reload file every 2500 ms or x ms if you w
});
</script>
<?php
}
?>
<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';}      
    });
});
</script>
<?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"); //Redirect the user
}
?>

</body>
</html>

When i run this code above i got warning like this,

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\selva\php\onlinechat\chat\index.php:1) in C:\xampp\htdocs\selva\php\onlinechat\chat\index.php on line 2

i followed all other stackoverlfow resources and modified some changes..

But still the above warning is showing.

Can anyone help me to fix this?

Thanks.

sandhu
  • 117
  • 1
  • 3
  • 15
  • `` change to ` – Saty Jul 31 '15 at 08:45
  • Can you post full error message? It will point to the line in your file which produced the output. – martynasma Jul 31 '15 at 08:46
  • @martynasma: i have been edited my post..please check it.. – sandhu Jul 31 '15 at 08:48
  • Let's get the obvious out the way.. is your file saved as UTF-8 **with** BOM? (easy way to check, on Windows, is open it in Notepad++) – CD001 Jul 31 '15 at 08:51
  • yes, i m working with notepad++.. – sandhu Jul 31 '15 at 08:54
  • 3
    It could be the BOM (Byte Order Mark) it's essentially an invisible character at the start of the file that ensures it's UTF-8 : https://en.wikipedia.org/wiki/Byte_order_mark ... try re-saving with file as UTF-8 **without** BOM. – CD001 Jul 31 '15 at 08:55
  • Looking at that image, it's not that anyway ... look in the bottom right of the app window, you'll see `Dos\Windows | UTF-8 w/o BOM | ...` *w/o BOM* indicates that the file isn't using the Byte Order Mark anyway... – CD001 Jul 31 '15 at 09:02
  • However, that image does show that there's a space before the opening ` – CD001 Jul 31 '15 at 09:11
  • Look at your code ... it begins ' – CD001 Jul 31 '15 at 09:28
  • @CD001: when i clear the white space, warning removed.. fine thanks..but at last exit chat,, it shows the same warning.. so i clear the white space for the corresponding line..but this time it didnt remove..help? – sandhu Jul 31 '15 at 09:44
  • Same issue - you can't do **anything** with HTTP headers (i.e. `header()`) **after** you've output **anything** to the browser - seriously, read the answer at http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php - it covers all of this... – CD001 Jul 31 '15 at 09:59
  • @CD001: thanks,.. i got it... – sandhu Jul 31 '15 at 10:19

2 Answers2

1

You need to call session_start() directly after your opening <?php tag in the first script that is called without any whitespace before that and before any other information is outputted/sent to the browser.

<?php
session_start();
//... everything else
Raphioly-San
  • 403
  • 3
  • 10
Ben
  • 8,894
  • 7
  • 44
  • 80
1

Use session_start() directly after your opening tag, before any other information is outputted/sent to the browser.

sujivasagam
  • 1,659
  • 1
  • 14
  • 26