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