Chat code help needed

okay, i have a little site with chat in bottom of page

i use mysql to store the messages, but i dont have login system and i dont wanna make 1/use 1.

what i want to do ->

1.when the user post a message, it stores the username in cookie file, then it should hide the <input name> field and show only the text area for the message

then once the user post another message, if the cookie exist it use the username from the cookie.

any tips?

http://php.net/manual/en/function.setcookie.php

yes

this is what i have currently written.

im getting some errors and no idea how to fix :S


<script type="text/javascript">
<!--
cookie_name="fbskchat"
expdays=365

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function saving_cookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000));

Data="cooked"

set_cookie(cookie_name,Data,expdate)
}

function get_cookie_data(){
inf=get_cookie(cookie_name)
if(!inf){
document.getElementById("form1").style.display="block"
}
else{
document.getElementById("form2").style.display="block"
}
}

// -->
</script>  
<div id="talk">

<?php
if (isset($_COOKIE["username"]))
  echo <<<OUT
  <table border="0">
<tr>
    <td>
    <form name="form1" action="" method="POST">
    <input name="username" value="<? echo $_POST["username"];?>" id="chattext" type="hidden" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);">
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" onsubmit="return setCookies();" type="submit">
    </form>
    </td>
</tr>
 </table>
 
OUT;
else
  echo <<<OUT
// Welcome " . $_COOKIE["Guest"] . "!<br />";
<table border="0">
<tr>
    <td>
    <form name="form2" action="" method="POST">
   
    <input  value="guest" name="username" id="chattext" size="15"  type="text" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);">
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" onsubmit="return setCookies();" onclick="setcookie();" type="submit">
    </form>
    </td>
</tr>
</table>
OUT;
?>
</div>

ok i almost got it to work, i dont understand why this doesnt work inside the form but does work outside form.


//insert username in a cookie
$value = $_POST["username"];
 
setcookie("fbskchat", $value, time()+3600);
?>

this works

<td>Users Online (Last 10 min): xxx - [<? echo $_COOKIE["fbskchat"];?>]

this doesnt.

<?php
if (isset($_COOKIE["fbskchat"]))
  echo <<<OUT
  <table border="0">
<tr>
    <td>
    <form name="form1" action="" method="POST">
    <input name="username" value="!!!!!!<? echo $_COOKIE["fbskchat"];?!!!!!!>" id="chattext" type="hidden" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);">
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;"  type="submit">
    </form>
    </td>
</tr>
 </table>
 
OUT;
else
  echo <<<OUT

<table border="0">
<tr>
    <td>
    <form name="form2" action="" method="POST">
   
    <input name="username" value="guest" id="chattext" size="15"  type="text" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);">
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" [U]onclick="setCookie(30, 1)"[/U]  type="submit">
    </form>
    </td>
</tr>
</table>
OUT;
?>

is the setCookie nesessary, or some mistakes with it?

sorry for my bad englsih btw :slight_smile:

i got it almost working, still same problem tho as before…

the same line work in other places of the script but not inside the form -_-

 <em><? echo {"$_COOKIE['username']"};?></em>
<? echo $_COOKIE['username'];?>

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /xxxxxxxxx/public_html/xxxx.co.cc/chat.php on line 273

I would use this instead:


<em><?php echo $_COOKIE['username']; ?></em> 
<?php echo $_COOKIE['username']; ?>

oh, i was tired last night :smiley:

neither of those work, i still get the errror.

<?php echo $_COOKIE[‘username’]; ?>

works fine in other places than form, i really dont have an idea what to do :frowning:

hey guys, any1 can help me with the problem?

it cant be that hard, but i just cant find solution and im getting frutrated :frowning:

Please post the code that is having trouble, along with a few lines of code from either side of it.

So, if for example it’s line 273 having trouble, please post lines 270 through to 276.

the code is pretty messy now that i been messing with it without getting it to work :stuck_out_tongue:

</td><tr>
  <a href="FBSKchathistory.php" target="_blank">View History</a> | <a href="javascript:ChatWindow();" id="chatwindow">Close</a>   
	</tr>
<div id="talk">

<?php
if (isset($_COOKIE['username']))
  echo <<<OUT
  <table border="0">
<tr>
    <td>
    <form name="form1" action="" method="POST">
    <em style="padding-left:10px;" id="username" class="username"><? print ". $_COOKIE['username'] .";?></em>
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" onlick="return fbskuser()" type="submit">
    </form>
    </td>
</tr>
 </table>
 
OUT;
else
  echo <<<OUT

<table border="0">
<tr>
    <td>
    <form name="form2" action="" method="POST">
    <em class="fbuser"><script type="text/javascript">fbskuser();</script></em>
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" onclick="return fbskuser();" type="submit">
    </form>
    </td>
</tr>
</table>
OUT;
?>
</tr>
</table>
</div>

i have another problem also, the script i have saves teh username in the cookie, but it lose the username after few page updates, and if i refresh the page, it post’s the same message i posted before… something is weird he ´, but i think i can getthat fixed.

now i just want teh username to stay in the cookie and the php script to work to show the username in the chat page from cookiel

First, resolve any possible structural issues by using braces for the conditional code.


if (condition) {
    ...
} else {
    ...
}

On the cookies, what is the expiry date on the cookies?

Does the cookie get lost after a certain time period? If so, the cookie may be getting expired.

Or, does it get lost after certain types of pages have been viewed? The cookie may be getting reset or lost at a certain page.

Try to narrow down the problem to when the cookie is getting lost. Then you can dig deeper in to how.

i use this script to save the username inthe cookie file


<script type="text/JavaScript">
function fbskuser(){
//First we set the cookie date to a year hence.
var now= new Date();
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
//We call the function GetCookie() with the name, "username".
var username = GetCookie("username");
if ((!username)||(username=='null'))
{ username = prompt("Please enter your name:", "");}
setCookie("username", username, now);
if (username) {
document.write(username);
setCookie("username", username, now);
}
else
document.write("<br><center>You didn\\'t enter your name.<a href=# onClick=\\"fbskuser();history.go(-1)\\">Enter it now?</a></center>");
}
</script>

and this 1, can it cause the error?

if (condition) {
    ...
} else {
    ...
}

because this code works and it is outside the form

<td>Users Online (Last 10 min): xxx - [<? echo $_COOKIE["username"];?>]

but in here it doesnt.

    <form name="form1" action="" method="POST">
    <em style="padding-left:10px;" id="username" class="username"><? echo $_COOKIE['username']";?></em>
    <input name="chattext" id="chattext" size="64" maxlength="128" style="font-size: 10px; font-family: Verdana; background-color: rgb(31, 36, 59); color: rgb(255, 255, 255);" type="text">
    <input class="button" value="Send" style="font-size: 10px; font-family: Verdana;" onlick="return fbskuser()" type="submit">
    </form>

From what I see there should be no trouble.

You may want to engage someone to take a more personal look at the code and apply special troubleshooting techniques.