|
Answer» ok. so i am trying to host a chatbox. i found this nifty tutorial. http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-simple-web-based-chat-application/
however, the PHP and Java scripts that it says to put, but where?
i have figured out some of this, but have a lot more that i am not knowing.
Stupid question has a stupid answer, i know. disregard. i found the source example, and figured it out that way.
ok, but i still get trouble. it seems that log.html is not getitng updated like it should be when a user submits the form.
files: index.php
Code: [Select]<?
session_start();
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
}
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 - Customer Module</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"><?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(){
//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(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
} //If user submits the form $("#submitmsg").click(function(){ var clientmsg = $("#usermsg").val(); $.post("post.php", {text: clientmsg}); $("#usermsg").attr("value", ""); return false; });
setInterval (loadLog, 2500); //Reload file every 2.5 seconds
//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
}
?>
</body>
</html> log.html
Code: [Select] <div class='msgln'>(12:32 PM) <b>Test</b>: asdf<br></div><div class='msgln'><i>User Test has left the chat session.</i><br></div><div class='msgln'>(12:47 PM) <b>user</b>: Hello<br></div><div class='msgln'>(12:47 PM) <b>user</b>: I'm a user.<br></div><div class='msgln'><i>User user has left the chat session.</i><br></div> post.php
Code: [Select] <?
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
fclose($fp);
}
?> style.css
Code: [Select]
/* CSS Document */
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; }
i currently have no idea where this is happening. This is running on my own server that does have PHP, MySQL, ext on the LAMP server typeLook in your web server logs. It might be that the scripts do not have the necessary permissions at the file level, to write to that file.AHA!! thanks.
I really need to look at stupid stuff like that. god. And then i break it after erasing the log file to prepare it for actual live use on my website.
Seems that my permissions keep getting re-set without me... How could i change this?Seccondly, when the log file gets to a LONG length, it lags for the clients. What script would i look into to get the PHP to return the HTML log file to a default header saying welcome?Frankly I wouldn't do it this way at all.
Anyway, the file you're writing too will doubtless acquire permissions from whatever process is running the script that originally creates it. On typically Linux systems that will be the apache, httpd or www-data user. If you're user VirtualMin however, web scripts usually run under the id of the VirtualMin user. Check which is the case, then check the ownership of the parent directory. See if it all matches up.
PHP has functions related to file ownership and mode. Review those. http://uk.php.net/manual/en/ref.filesystem.php
If the file you're loading is too big, you should consider chopping something off the top of the file or only showing some of it by default. You could use Ajax to pull in more of the file as needed - like the way that Face book pages load extra content only once you scroll to the bottom of the page.
In any event, I still wouldn't do it this way. If you're needing to manage increasing quantities of data, a database-driven solution will almost always be more responsive. You could start here, perhaps: http://duckduckgo.com/?q=PHP+MySQL+chat+room
|