[英]how to create user files within a chat and messaging system using ajax and php
我真的希望得到一个有用的答案。 我一直在研究一种允许两个用户之间进行私人会话的聊天框的方法,其中只有两个用户可以聊天,可以选择加入普通聊天室,而我却没有任何xmpp,sockets等,仅使用将用作日志的文件,我将解释这个概念:用户登录后,将他们定向到聊天页面,在该页面中,将存储在数据库中的朋友列表加载到他们的右边,这部分是工作正常:
//i've declared all the variables previously
//skip to the part that loads the names:
while($rowb=mysql_fetch_array($done))
{
$rafiki=$rowb['name'];
//show the names,when the user clicks on any of them, the name(variable $jina) of the user
//and the friend's name are sent to the function makefile()
echo "<a href='Javascript:makefile(".$jina.",".$rafiki.")'>"."$rafiki"."</a>"."<br/>";
}
当用户单击任何名称时,它们将被发送到javascript函数,该函数同时使用参数,用户名和朋友的名字,然后在末尾添加扩展名,以创建一个日志文件,用作他们之间的聊天记录:
function makefile(username,friendname)
{
//add file extension
var ext=".html";
//concatenate
var dash="-";
var full=username.concat(dash,friendname,dash,ext);
var str=friendname.concat(dash,username,dash,ext);
因此,每对都有自己的文件,变量full通过ajax发送到脚本以使过程无缝,该脚本做了很多事情:它首先检查是否有一个日志文件,其中包含从客户端存在(以用户名或朋友的名字开头),如果存在一个文件,则检查是否已发送任何消息并将其写入文件;如果两个文件都不存在,则创建一个新文件,并提示一个朋友想要开始聊天的用户,当该朋友单击用户名时,第一个条件将成功,因为已经创建了一个文件:
<?php
session_start();
$full=$_POST['full'];
//first, check to see if variations of the file already exist
//start by exploding the sent data
$result=explode("-",$full);
$usrnme=$result[0];
$frndnme=$result[1];
$ext=$result[2];
//make varied names for comparison
$vrdfull=array($result[1],$result[0],$result[2]);
$str=implode("-",$vrdfull);
$_SESSION['str']=$str;
$_SESSION['full']=$full;
//start actual comparison
if(file_exists($full)||file_exists($str))
{
//stuff to do if first file exists
if(file_exists($full))
{
//check to see if message has been sent
if (isset($_POST['message']))
{
$message=$_POST['message'];
//update existing file with message
$fp=fopen($full,'a');
fwrite($fp, "<div id=\"sent\">".$usrnme." :"." ".$message."<br/>"."</div>");
//close the file
fclose($fp);
}
}
else
//stuff to do if second file exists
{if(file_exists($str))
{
//check to see if message has been sent
if (isset($_POST['message']))
{
$message=$_POST['message'];
//update existing file with message
$fpp=fopen($str,'a');
fwrite($fpp, "<div id=\"sent\">".$usrname." :"." ".$message."<br/>"."</div>");
//close the file
fclose($fpp);
}
}
}
}
else
{
//create file, since neither files exist
$fhandel=fopen($full,'a');
//check if message has been sent
if(isset($_POST['message']))
{
$messssage=$_POST['message'];
fwrite($fhandel,"<div id=\"sent\">".$usrname." "." : ".$messssage."<br/>"."</div>");
}
fclose($fhandel);
//send msg to friend, incurring them to accept
$ext=".html";
$frrnme=$frndnme.$ext;
$fpp=fopen($frrnme,'a');
//prompt the user to initiate chat by opening file,by clicking on the name he/she would see
fwrite($fpp,"<div id=\"msg\">".$frndnme." wants to chat, click on their name to accept"."</div>");
fclose($fpp);
}
?>
我认为这部分没有任何问题,只是将其发布以帮助传达想法。这是将字符串完整发送到脚本的ajax(我认为可能是错误的):
$.ajax({
type:'POST',
url:'makesend.php',
data: {full:full},
//what to do if data was sent:
success: function(full){
$('#myResponse').html(full);
}
});
return false;
div“ myresponse”中没有显示任何数据,所以我认为这里有问题,我不知道是什么。 接下来的事情是处理用户发送的消息,以下是获取用户输入的表格:
<form name="message" action="">
<input name="message" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" onclick="send()" id="submitmsg" value="Send" />
</form>
这是将数据发送到makeend.php文件的send()函数:
function send(){
var clientmsg = $("#usermsg").val();
$.post("makesend.php", {message: clientmsg});
$("#usermsg").attr("value", "");
return false;
}
再次,当我对此进行测试并编写消息时,页面重新加载,并且没有数据发送到脚本! 这里也有问题,我不知道这是什么。 接下来,在创建文件并开始与用户互动之后,需要将其上传到用户可以查看的消息区域,这是消息区域div:
记住,我需要加载一个存在的文件,因此在将其加载到该区域之前,我需要使用php来查看存在哪个版本,可以使用用户名优先或使用朋友名称($ full或$ str):
$("#msgarea").html(
"<?php
//check to see if either file exists
if(file_exists($_SESSION['full'])||file_exists($_SESSION['str'])){
//check to see if the first file exists:
if(file_exists($_SESSION['full']))
{
$full=$_SESSION['full'];
$handlle = fopen($full, "r");
$contents = fread($handlle, filesize($full));
fclose($handlle);
//load it's contents to the division if it does
echo $contents;}
else
{
//if first file doesn't exist, load the second one:
$str=$_SESSION['str'];
//check to see if it exists before loading it
if(file_exists($str))
{
$handle = fopen($str, 'r');
$contents = fread($handle, filesize($str));
fclose($handle);
//load it to the division
echo $contents;
}
}
}
?>"
);
我认为这样做是合法的,将php代码添加到元素的innerHTML中,因此此代码将加载存在的文件的任何版本。 我从会话中获取文件名,因为这部分代码在将数据发送到makeend.php文件后执行,这将启动一个会话并给出它们($ _SESSION ['full']和$ _SESSION ['str'] )值。 加载文件的这一部分是函数makefile()中的最后一段代码,首先,该函数以用户单击的名称的形式从用户获取数据,然后将其发送到脚本(makesend.php),该脚本创建聊天日志文件,在此之后,来到最后一部分,它加载数据到师“msgarea”。 接下来,我需要在一定时间后刷新/重新加载创建的日志文件以显示用户发送的消息,我选择使用2.5秒,这部分不在函数makefile()之外,我必须使用jQuery中的php首先检查哪个文件存在,然后在php中使用jquery重新加载和动画化(自动滚动)现有的php,此php位于'javascript'标记内:
<?php
//set up the conditions
$full=$_SESSION['full'];
$str=$_SESSION['str'];
//check whether either file exists
if(file_exists($full)||file_exists($str))
{
//reload the first file if it exists
if(file_exists($full)){
//this is the jquery inside the php(which is also in javascript tags)
echo '<script type="text/javascript" src="jquery-1.8.0.min (1).js"></script>';
echo '<script type="text/javascript">';
echo
'function loadLog(){
var oldscrollHeight = $("#msgarea").attr("scrollHeight") - 20;
$.ajax({
url: <?php session_start(); echo $_SESSION[\'full\']?>,
cache: false,
success: function(html){
$("#msgarea").html(html); //Insert chat log into the #msgarea div
var newscrollHeight = $("#msgarea").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#msgarea").animate({ scrollTop: newscrollHeight }, \'normal\'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 2500); //Reload file every 2.5 seconds';
}
else{
//this will reload the second file since the first doesn't exist:
echo
'function lloadLog(){
var oldscrollHeight = $("#msgarea").attr("scrollHeight") - 20;
$.ajax({
url: <?php session_start(); echo $_SESSION[\'str\']?>,
cache: false,
success: function(html){
$("#msgarea").html(html); //Insert chat log into the #msgarea div
var newscrollHeight = $("#msgarea").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#msgarea").animate({ scrollTop: newscrollHeight }, \'normal\'); //Autoscroll to bottom of div
}
},
});
}
setInterval (lloadLog, 2500); //Reload file every 2.5 secsonds';
}
echo '</script>';
}
?>
我认为就是这样,整个系统也是如此,注销调用存在问题,这是代码,它位于文件的最后:$(“#exit”)。click(function(){var exit = Confirm(“确定要结束会话吗?”); if(exit == true){window.location ='testing.php?logout = true';}
}); 这是它的标记:
<p class="logout"><a id="exit" href="#">Exit Chat</a></p>
在文件的顶部,这是检查其是否设置为true的部分:
session_start();
//logout function
if(isset($_GET['logout']))
{
if(file_exists($_SESSION['full'])||file_exists($_SESSION['str']))
{
if(file_exists($_SESSION['full']))
{
$full=$_SESSION['full'];
$flogout=fopen($full,'a');
fwrite($flogout, $_SESSION['username']." has left the anichat!!! "."<br>");
fclose($flogout);
}
else
{
$str=$_SESSION['str'];
if(file_exists($str))
{
$flogoout=fopen($str,'a');
fwrite($flogoout, $_SESSION['username']." has left the chat "."<br>");
fclose($flogoout);
}
}
}
session_destroy();
header("Location:testing.php");//user redircect.
}
用户的页面甚至没有刷新,因此会话被破坏,它只是刷新而会话数据仍然存在(这是因为未显示登录表单,如果$ _SESSION ['name']应该显示,是空的)。 我知道这是一个很长的问题,但是我是一个初学者,我确实需要帮助,我认为主要的问题是ajax,但是如果您能发现其他问题,我将非常感谢,我恳请您相关的答案将帮助我实现这一目标,如果您希望我将在test.php和makeend.php中都存在的完整代码发布出来,请使用与我在此处进行说明的方式不同的方式进行发布,请询问如果有帮助,我要感谢所有为帮助他人而竭尽全力的高级程序员。
我想这是处理页面makesend.php
没有检索到请求的任何内容。 您可以使用萤火虫来确定它是否是某些东西。 我说这是因为更新后您没有读取文件(看不到文件),您可以创建一个新的ajax请求,该请求在一定间隔后会定期调用,并将对话内容加载到日志文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.