【问题标题】:Echo PHP inside of string在字符串内回显 PHP
【发布时间】: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


【解决方案1】:

试试 -

'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.ini 中了。所以不需要添加&lt;?php ?&gt;echo。只需简单地连接它们即可。

【讨论】:

  • 这是我尝试做的一个例子。 case('getState'): if(file_exists('logs/.txt')){ $lines = file('logs/.txt');
  • 这是一个开始,但它们保存为 .txt(前面没有名字)
  • process.php 文件与chat.php 是分开的,所以我猜这就是它不知道GET 是什么的原因?
  • 然后添加一个检查。
  • 即使这可行,您也应该非常小心地从请求查询字符串 (GET) 中获取主题值并将其值注入此类函数。这是一个应该解决的安全问题。请清除$_GET['topic'] 的返回并保护您的整个脚本。
【解决方案2】:

你已经在php标签中了..不需要添加额外的php标签

case('getState'):
    if(file_exists("logs/".$_GET['topic'].".txt")){
    $lines = file("logs/".$_GET['topic'].".txt");
}

或者试试这个

case('getState'):
   if(isset($_GET['topic']){
       $filename = "logs/".$_GET['topic'].".txt";
        if(file_exists($filename)){
            $lines = file($filename);
        }
    }
}

【讨论】:

  • 我得到了它的排序工作。,。问题是 GET 发生在 chat.php 上,所以 process.php 不知道它是什么
  • @daltonEdwards 您正在将“文件”从 js 发送到 process.php 您可以在访问 $function = $_POST['function']; 时访问它,您的代码将是 $file_name = $_POST['file'];
  • 我不确定你的意思
  • 通过搜索找到 GET 主题。但是搜索栏除了将您重定向到正确的 URL 之外,并没有以任何其他方式连接到这两个代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
相关资源
最近更新 更多