0

I'm trying to make a simple web chat using mostly JavaScript and AJAX.The problem is that I use prompt box for login, and the function is called in <body onload="login()">which is simple JS function, but I want to save the data in PHP session.And what's going wrong is that everytime I reload the chat page I get this

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by

I guess It's something standart - I have few more functions that I load in the body and even thou I put my session_start function at the very top of the page, maybe there's something that is loaded before it.So, If I'm right and there's some standart issue when you use JS and PHP sessions is there any way to solve it, am I doin it wrong by trying to mix PHP and JS that way?I was thinking to use ob_start, but even if this solve the problem I would like to find another way to do this, because I don't think it's the best programming practice.Anyways, will appretiate any advice on the topic.

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<LINK href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function showUser()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// Използваните браузъри
  xmlhttp=new XMLHttpRequest();
  }
else
  {// Кой ли ползва тези версии..
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("ActiveUsers").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showuser.php",true);
xmlhttp.send();
setTimeout("showUser()",5000);
}
function showChat()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// Използваните браузъри
  xmlhttp=new XMLHttpRequest();
  }
else
  {// Кой ли ползва тези версии..
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("chat").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showchat.php",true);
xmlhttp.send();
setTimeout("showChat()",1000);
}
function Login()
{
var x=prompt("Please enter your name","");
var xmlhttp;
if (window.XMLHttpRequest)
  {// Използваните браузъри
  xmlhttp=new XMLHttpRequest();
  }
else
  {// Кой ли ползва тези версии..
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.open("GET","login.php?u="+x,true);
    xmlhttp.send();
    }
</script>
  <title></title>
</head>

<body onload = "showUser(), showChat(), Login()">
<?php
  If ($_SESSION['UserName']!='alabala'){echo "hi";}
?>
<script type="text/javascript">
function doWork(str)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// Използваните браузъри
  xmlhttp=new XMLHttpRequest();
  }
else
  {// Кой ли ползва тези версии..
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.open("GET","newmsg.php?q="+str,true);
    xmlhttp.send();
}
</script>
<div id="main">
<div id="ActiveUsers"></div>
    <div id="chat"></div>


     <div id="sender">
   Your message: <input type="text" name="msg" size="30" id="msg" />
   <button onclick="doWork(document.getElementById('msg').value);">Send</button>
    </div>
</div>
</body>

</html>

Thanks

Leron

Leron
  • 9,546
  • 35
  • 156
  • 257
  • 1
    It has nothing to do with javascript. You likely have some kind of warning or error that's getting hidden but printed first which causes the session_start() problems. Please post your code so someone can show you the actual problem. – Cfreak Jun 06 '11 at 15:22
  • It may be a BOM problem. http://stackoverflow.com/questions/2243087/warning-cannot-modify-header-information-headers-already-sent/2243113#2243113 – meouw Jun 06 '11 at 15:23
  • You just omitted the interesting part of the message. What does the message say where the output started? – Gumbo Jun 06 '11 at 15:30
  • output started at C:\Program Files\webPHP\xampp\htdocs\MyChat\chat1.php:1) in C:\Program Files\webPHP\xampp\htdocs\MyChat\chat1.php on line 2 – Leron Jun 06 '11 at 15:32
  • And the code in my question is the actual chat1.php code. – Leron Jun 06 '11 at 15:33
  • Ok, so what happen is that throws an warning, but do not.I'm newb but is this it? – Leron Jun 06 '11 at 15:45

2 Answers2

0

The session_start() function has to be called before you've sent any output to the browser. That includes any HTML or even white space like blank lines. Double check.

The JavaScript issue is likely to be a red herring. Ignore the JavaScript and work with the PHP page directly in your browser. Can you get it to load without the error?

If you still get the error - pay attention to the headers already sent by part of the error - that tells you where the output has started. It will usually be a script and line number. Go there and see what's outputting at that point.

drewm
  • 2,003
  • 1
  • 16
  • 22
  • I have a few .php files for the MySQL records - I put the usernames and the messages in a DB and I call them from there.My actual PHP code in tha main page is just the session_start() function. – Leron Jun 06 '11 at 15:36
0

What happens if you comment out session_start()? Are there any errors, or does the page load smoothly?

Maaatt
  • 118
  • 6