如何将新用户添加到Ajax聊天脚本中

问题描述 投票:3回答:2

[嗯,我下载了此脚本的独立版本http://frug.github.io/AJAX-Chat/,发现它没有注册页面可添加新用户。因此,我决定自己编写它。用户信息存储在chat \ lib \ data \ users.php中:

<?php
/*
 * @package AJAX_Chat
 * @author Sebastian Tschan
 * @copyright (c) Sebastian Tschan
 * @license Modified MIT License
 * @link https://blueimp.net/ajax/
 */

// List containing the registered chat users:
$users = array();

// Default guest user (don't delete this one):
$users[0] = array();
$users[0]['userRole'] = AJAX_CHAT_GUEST;
$users[0]['userName'] = null;
$users[0]['password'] = null;
$users[0]['channels'] = array(0);

// Sample admin user:
$users[1] = array();
$users[1]['userRole'] = AJAX_CHAT_ADMIN;
$users[1]['userName'] = 'jafar';
$users[1]['password'] = '123456';
$users[1]['channels'] = array(0,1);

// Sample moderator user:
//$users[2] = array();
//$users[2]['userRole'] = AJAX_CHAT_MODERATOR;
//$users[2]['userName'] = 'moderator';
//$users[2]['password'] = 'moderator';
//$users[2]['channels'] = array(0,1);

// Sample registered user:
$users[3] = array();
$users[3]['userRole'] = AJAX_CHAT_USER;
$users[3]['userName'] = '123';
$users[3]['password'] = '123';
$users[3]['channels'] = array(0,1)


?>

我在该文件夹中添加了id.txt文件以保存最后一个用户ID,然后我将此代码写入了注册页面:

<html>

<body>
<?php
if(isset($_GET['name']) && isset($_GET['password']))
{
$name = $_GET['name'];
$pass  = $_GET['password'];

$users =  file_get_contents('.\lib\data\users.php');
$id = file_get_contents('.\lib\data\id.txt');
$users = substr($users,0,strlen($users)-3);

$users = $users . '$users['. $id .'] = array();
$users[' . $id . '][\'userRole\'] = AJAX_CHAT_USER;
$users[' . $id . '][\'userName\'] = \'' . $name . '\';
$users[' . $id . '][\'password\'] = \'' . $pass . '\';
$users[' . $id . '][\'channels\'] = array(0,1);     ' ;



$id=$id+1;
$id=$id+'';
$a = file_put_contents('.\lib\data\users.php',$users.'?>');
$a = $a +file_put_contents('.\lib\data\id.txt',$id);

//if($a!=2)
//echo 'Error !';

}


?>
<center>
<form method=get>
username <input type=text name=name >
<br>
password <input type=password name=password >
<input type=submit>
</form>

</center>

但是当我尝试打开聊天室agian时,出现此错误:

XML Parsing Error: no element found Location: http://localhost/irc/chat/ Line Number 1, Column 1:^

我做错了什么?

ajax chat
2个回答
0
投票

您正在放标签内PHP代码必须位于顶部。希望对您有所帮助


-1
投票

我不知道这是否会破坏任何东西,但是您没有关闭<html>标签或<body>标签。您也没有关闭任何输入标签,因此在输入内部有输入。尝试关闭html和body标记,对于输入标记,可以将>替换为/>。如果仍然无法正常工作,我将进一步检查。

© www.soinside.com 2019 - 2024. All rights reserved.