0

我真的希望得到一个有用的答案。我一直在努力制作一个允许两个用户之间进行私人会话的聊天框,其中只有两个用户可以选择加入普通聊天室进行聊天,我这样做没有任何 xmpp、套接字等,我是仅使用可用作日志的文件,我将解释这个概念:用户已登录,他们被定向到聊天页面,其中存储在数据库中的朋友列表加载到他们的右侧,这部分是工作正常:

//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.

 <?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,但如果你能发现其他任何东西,我将非常感激,我恳请相关答案将帮助我实现这一点,如果您希望我以一种不像我在这里说明的方式那样分开的方式发布 testing.php 和 makeend.php 中存在的整个代码,请询问如果它有帮助。我感谢所有不遗余力地帮助他人的高级程序员,在此先感谢。

4

1 回答 1

0

我想这是处理页面makesend.php没有检索到任何请求。您可以使用萤火虫来确定它是否正在写一些东西。我这么说是因为您在更新后没有读取文件(没有看到它)您可以创建一个新的 ajax 请求,该请求在一段时间后定期调用,并将内容与对话一起加载到日志文件中。

于 2012-10-16T16:22:38.830 回答