簡體   English   中英

如何在HTML中顯示源文件.php?

[英]How could I display the source file.php in HTML?

我有以下PHP腳本(file.php),它顯示當前時間並顯示用戶的輸入:

Current time:

<?php

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

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

?>

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

默認情況下,頁面顯示: 默認

如果我輸入例如<strong>test</strong> ,我會看到: enter_htmlcode1

如果我輸入<iframe src="file.php"></iframe> ,我可以在較小的窗口中重新加載頁面: enter_htmlcode2

那么,現在,如何通過在INPUT文本字段中提交某些HTML代碼來顯示原始PHP腳本(file.php)?

<?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
$enter=@$_POST['enter'];
echo '<br>Input: '.$enter;

?>

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

在此輸入圖像描述

第一

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>

將輸入解析為純文本應顯示文件:

<?php

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

//show user input
$enter=@$_POST['enter'];

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

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

?>

當然,您必須自定義腳本以檢測用戶何時想要顯示文件,然后才更改內容類型(否則其他html輸入將不起作用)。

提交html或php代碼然后顯示:

<?php

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

//show user input
$enter=@$_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);
?>

文件名:test1.php

在此文件中寫下以下代碼:

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

$enter=@$_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>

它會工作!!

我不完全理解你想要實現的目標,但是,我認為highlight_file()函數應該有所幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM