2015-12-08 13 views
14

Ho il seguente script PHP (file.php) che mostra l'ora corrente e visualizza l'input dell'utente:Come posso visualizzare il file source.php in HTML?

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> 

Per default pagina mostra questo: default

Se entro esempio <strong>test</strong>, vedo questo: enter_htmlcode1

E se entro <iframe src="file.php"></iframe>, posso ricaricare la pagina in una finestra più piccola: enter_htmlcode2

Così, ora, come avrei potuto visualizzare lo script PHP grezzo (file.php) inviando un certo codice HTML nel campo di testo INPUT?

+3

[highlight_file()] (http://www.php.net/manual/en/function.highlight-file.php) –

+1

iframe non è la vostra risposta. – Krii

+0

@MarkBaker Se inserisco qualcosa come ' ', non vedo nulla. – Andy

risposta

11
<?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> 

enter image description here

+1

thx! per completezza: l'input deve essere '' – Andy

2

Analisi l'ingresso come testo normale dovrebbe visualizzare il file:

<?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; 

?> 

Naturalmente si dovrebbe quindi avere per personalizzare lo script per rilevare quando l'utente vuole visualizzare il file, e solo allora cambiare il tipo di contenuto (altrimenti gli altri input HTML non funzioneranno).

+0

Beh, funziona ... ma speravo davvero di visualizzare il file senza modificare esso. – Andy

+0

Modifica di quale file? Stai parlando di 'file.php'? Perché a meno che lo script * non sia * 'file.php', non lo modificherete. – Krii

+0

sì, intendo il file 'file.php'. Quello che intendo fare è dare un input e quindi restituire il file sullo schermo. – Andy

3

Prima

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> 

Seconda

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> 
2

Invio di codice HTML o PHP per poi visualizzazione:

<?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> 

L'apertura di un file e quindi di visualizzazione:

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

Nome file: test1.php

Scrivi sotto il codice in questo file:

<?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> 

Creare un nuovo file: test6.php

Scrivi il codice di seguito in questo file:

file di

Hit: test1.php

scrittura nel tag input: <iframe src="test6.php"></iframe>

Funzionerà !!

0

Non capisco completamente cosa si sta cercando di ottenere, tuttavia, suppongo che la funzione highlight_file() dovrebbe aiutare.

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