Как я могу показать исходный файл .php в HTML?

У меня есть следующий PHP script (file.php), который показывает текущее время и отображает ввод пользователя:

Current time:

<?php

$time=time();
$actual_time=date('H:i:s',$time);
echo $actual_time;

//show user input
[email protected]$_POST['enter'];
echo '<br>Input: '.$enter;

?>

<form action="" method="POST">
    <input type="text" name="enter">
    <input type="submit" value="Refresh">
</form>

По умолчанию страница показывает это: default

Если я вхожу, например. <strong>test</strong>, я вижу следующее: enter_htmlcode1

И если я введу <iframe src="file.php"></iframe>, я могу перезагрузить страницу в меньшем окне: enter_htmlcode2

Итак, теперь, как я могу отобразить необработанный PHP script (file.php), отправив некоторый определенный HTML-код в текстовое поле INPUT?

Ответы

Ответ 1

<?php

// Disable a WebKit security feature
// which would prevent from showing the source code.
header('X-XSS-Protection: 0');

if (isset($_GET['source']) || isset($_POST['source'])) {
        $source = file_get_contents(__FILE__);

        // To prevent this control from showing up
        // in the output source code
        // enable the code below.
        /*
        $lines_to_remove = 26;
        $source = explode("\n", $source, $lines_to_remove);
        $source = $source[$lines_to_remove - 1];
        */

        $source = highlight_string($source, true);
        echo $source;

        return;
}

?>
Current time:

<?php


$time=time();
$actual_time=date('H:i:s',$time);
echo $actual_time;

//show user input
[email protected]$_POST['enter'];
echo '<br>Input: '.$enter;

?>

<form action="" method="POST">
    <input type="text" name="enter">
    <input type="submit" value="Refresh">
</form>

введите описание изображения здесь

Ответ 2

Первая

htmlspecialchars — Convert special characters to HTML entities

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new;

//This would be the output
&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

//browser will display
<a href='test'>Test</a>

Второй

htmlentities -Convert all applicable characters to HTML entities

$str = "A 'quote' is <b>bold</b>";

echo htmlentities($str);

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;

echo htmlentities($str, ENT_QUOTES);
// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;

In browser it woulbe displayed:

A 'quote' is <b>bold</b>

Ответ 3

Разбор ввода в виде обычного текста должен отображать файл:

<?php

$time=time();
$actual_time=date('H:i:s',$time);
echo $actual_time;

//show user input
[email protected]$_POST['enter'];

header("Content-Type: text/plain");

echo '<br>Input: '.$enter;

?>

Конечно, вам нужно будет настроить ваш script, чтобы определить, когда пользователь хочет отобразить файл, и только затем изменить тип контента (иначе другие входы html не будут работать).

Ответ 4

Отправка html или php кода для отображения:

<?php

$time=time();
$actual_time=date('H:i:s',$time);
echo $actual_time;

//show user input
[email protected]$_POST['enter'];
echo '<br>Input: <pre>'.htmlspecialchars($enter).'</pre>';

?>

<form action="test.php" method="POST">
    <input type="text" name="enter">
    <input type="submit" value="Refresh">
</form>

Открытие файла, а затем отображение:

<?php
    $myfile = fopen("test.php", "r") or die("Unable to open file!");
    echo '<pre>'.htmlspecialchars(fread($myfile,filesize("test.php"))).'</pre>';
    fclose($myfile);
?>

Ответ 5

Имя файла: test1.php

Введите в этом файле код ниже:

<?php
$time=time();
$actual_time=date('H:i:s',$time): ;
echo $actual_time;

[email protected]$_POST['enter'];
if (isset($enter)) {
    $doc = new DOMDocument();
    @$doc->loadHTML($enter);
    $tags = $doc->getElementsByTagName('iframe');
    foreach ($tags as $tag) {
           $file_name = $tag->getAttribute('src');
    }
    if(isset($file_name)){
        $result ='<iframe src='.$file_name.'></iframe>';        
    }else{
        $result = $enter;   
    }
}
echo '<br>Input: '.$result;
?>
<form action="" method="POST">
    <input type="text" name="enter">
    <input type="submit" value="Refresh">
</form>

Создайте новый файл: test6.php

Введите код ниже в этом файле:

<?php
    $myfile = fopen("test1.php", "r") or die("Unable to open file!");
    echo '<pre>'.htmlspecialchars(fread($myfile,filesize("test1.php"))).'</pre>';
    fclose($myfile);
?>

Хит файл: test1.php

записать в тег ввода: <iframe src="test6.php"></iframe>

Он будет работать!

Ответ 6

Я не совсем понимаю, чего вы пытаетесь достичь, однако я полагаю, что функция highlight_file() должна помочь.

echo highlight_file('file.php',true);