我正在運行一個簡單的聊天應用程序,它由一個process.php文件提供支持,但聊天是在chat.php上。Echo PHP裏面的字符串
基本上人們可以搜索「話題」,它會帶他們去使用domain.tld/chat.php?主題= topicname(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();
},
});
}
理論上這應該創建/日誌獨特的topicname.txt文件/每當有人開始一個話題,是不存在的在聊天。我只是在添加topicname來代替process.php中的chat.txt時遇到問題。到目前爲止,我知道它自己創建了一個chat.txt文件,所以它應該創建一個唯一的.txt文件,只要我正確地回顯它。
而且,我知道,相對於存儲在獨特的.txt文件消息時,一個數據庫是一個更好的選擇,但是這是我要如何做到這一點。
這裏是我如何試圖將其添加到我的process.php從process.php片段)的一個例子
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變量更改爲我想要訪問其他人的聊天記錄的任何內容嗎? –