【发布时间】:2015-07-02 13:04:43
【问题描述】:
我正在运行一个简单的聊天应用程序,它由 process.php 文件提供支持,但聊天是在 chat.php 上进行的。
基本上人们可以搜索“主题”,然后将他们带到 domain.tld/chat.php?topic=topicname(主题名是他们搜索的任何内容)
我需要我的 process.php 文件来回显
<?php echo $_GET['topic']; ?>.txt
而不是chat.txt,这样每个主题都有一个唯一的文本文件(这样所有的聊天都不会链接)
这是我的 process.php 文件:
<?php
$function = $_POST['function'];
$log = array();
switch($function) {
case('getState'):
if(file_exists('logs/chat.txt')){
$lines = file('logs/chat.txt');
}
$log['state'] = count($lines);
break;
case('update'):
$state = $_POST['state'];
if(file_exists('logs/chat.txt')){
$lines = file('logs/chat.txt');
}
$count = count($lines);
if($state == $count){
$log['state'] = $state;
$log['text'] = false;
}
else{
$text= array();
$log['state'] = $state + count($lines) - $state;
foreach ($lines as $line_num => $line)
{
if($line_num >= $state){
$text[] = $line = str_replace("\n", "", $line);
}
}
$log['text'] = $text;
}
break;
case('send'):
$nickname = htmlentities(strip_tags($_POST['nickname']));
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$message = htmlentities(strip_tags($_POST['message']));
if(($message) != "\n"){
if(preg_match($reg_exUrl, $message, $url)) {
$message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
}
$message = preg_replace('/#(\w+)/', ' <a href="@$1" target="_blank">#$1</a>', $message);
fwrite(fopen('logs/chat.txt', 'a'), "<span>". $nickname . "</span>" . $message = str_replace("\n", " ", $message) . "\n");
}
break;
}
echo json_encode($log);
?>
这是我的 chat.js 文件
/*
Created by: Kenrick Beckett
Name: Chat Engine
*/
var instanse = false;
var state;
var mes;
var file;
function Chat () {
this.update = updateChat;
this.send = sendChat;
this.getState = getStateOfChat;
}
//gets the state of the chat
function getStateOfChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'getState',
'file': file
},
dataType: "json",
success: function(data){
state = data.state;
instanse = false;
},
});
}
}
//Updates the chat
function updateChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'update',
'state': state,
'file': file
},
dataType: "json",
success: function(data){
if(data.text){
for (var i = 0; i < data.text.length; i++) {
$('#chat-area').append($("<p>"+ data.text[i] +"</p>"));
}
}
document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
instanse = false;
state = data.state;
},
});
}
else {
setTimeout(updateChat, 1500);
}
}
//send the message
function sendChat(message, nickname)
{
updateChat();
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'send',
'message': message,
'nickname': nickname,
'file': file
},
dataType: "json",
success: function(data){
updateChat();
},
});
}
理论上,当有人开始在不存在的主题中聊天时,这应该在 /logs/ 中创建一个唯一的 topicname.txt 文件。我只是在 process.php 中添加 topicname 代替 chat.txt 时遇到问题。到目前为止,我知道它确实会自己创建一个 chat.txt 文件,所以一旦我正确地回显它,它应该会创建一个唯一的 .txt 文件。
另外,我知道与将消息存储在唯一的 .txt 文件中相比,数据库是一个更好的选择,但这就是我想要的方式。
这是我如何尝试将其添加到我的 process.php 的示例,来自 process.php 的 sn-p)
case('getState'):
if(file_exists('logs/<?php echo $_GET['topic']; ?>.txt')){
$lines = file('logs/<?php echo $_GET['topic']; ?>.txt');
}
^ 这可能甚至不是正确的格式,因为我是 PHP 新手并且犯了很多错误,而且它可能不知道 GET 是什么,因为它不是 chat.php 的一部分......这是一个单独的文件。
【问题讨论】:
-
将用户定义的变量附加到文件系统位置是很危险的
-
为什么?我已经使用不同的方法阻止了所有 XSS,并且在注册时存在用户名限制。
-
我可以将 $_GET 变量更改为我想要访问其他人的聊天日志的任何内容吗?
-
所有聊天都是公开的并且可以轻松加入。
标签: javascript php arrays string