我正在运行一个简单的聊天应用程序,它由 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 的片段)
case('getState'):
if(file_exists('logs/<?php echo $_GET['topic']; ?>.txt')){
$lines = file('logs/<?php echo $_GET['topic']; ?>.txt');
}
^ 这可能甚至不是正确的格式,因为我是 PHP 新手,犯了很多错误,而且它可能不知道 GET 是什么,因为它不是 chat.php 的一部分......它是一个单独的文件。
最佳答案
尝试使用 -
'logs/' . $filename . '.txt'
无论你想去哪里。
更新
if (!empty($_GET['topic'])) {
$filename = $_GET['topic'];
} else {
$filename = 'something else';
}
if(file_exists('logs/' . $filename . '.txt')){ $lines = file('logs/' . $filename . '.txt') ....
它已经在 php 中了。所以不需要添加<?php ?>
和echo
。只需简单地将它们连接起来即可。
关于javascript - 在字符串内回显 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838799/