Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Sessions-PHP回显文件的内容?_Php_Jquery_Ajax - Fatal编程技术网

如何使用Sessions-PHP回显文件的内容?

如何使用Sessions-PHP回显文件的内容?,php,jquery,ajax,Php,Jquery,Ajax,这是我正在使用的当前代码: <?php $blacklist = array("one.jps", "two.txt", "four.html"); $files = array_diff(glob("*.*"), $blacklist); foreach($files as $file) echo "<div class='post'><a href='" . $_SERVER['PHP_SELF'] . "?file=" .

这是我正在使用的当前代码:

<?php

    $blacklist = array("one.jps", "two.txt", "four.html");
    $files = array_diff(glob("*.*"), $blacklist);

    foreach($files as $file)
        echo "<div class='post'><a href='" . $_SERVER['PHP_SELF'] . "?file=" . $file . "'><p>" . $file . "</p></a></div>";


    if(!empty($_GET["file"]) && !in_array($_GET["file"], $blacklist) && file_exists($_GET["file"])) 
        $thesource = htmlentities(file_get_contents($_GET["file"]));

?>

<textarea rows="40" cols="100" placeholder="Source code of file" class="source"><?php if(!empty($thesource))echo $thesource; ?></textarea>
file=并不总是bob.html,它取决于您单击的链接

这意味着用户只需编辑URL并将“bob.html”更改为类似“index.php”的内容,就可以访问所有php代码


如何重写此代码以使URL不受影响-我相信我应该使用会话,但如何执行此操作

您可以在if语句中添加另一个条件。我们需要从url中获取文件名,并检查其是否以“php”结尾。下面就可以了

//checking file type by exploding the variable  
$fileType = end(explode(".", $_GET["file"]));
    if( $fileType != "php" && !empty($_GET["file"]) && !in_array($_GET["file"], $blacklist) && file_exists($_GET["file"])) {

据我所知,用户永远看不到PHP代码。如果“index.PHP”中的PHP代码没有任何回音,用户只会看到一个空页面。@Goudgeld1简而言之,当用户单击其中一个链接(目录中每个文件的链接)时,它会将该文件上的所有内容显示在文本区域中,包括PHP代码。这是因为变量被写入URL,但是,我正在尝试另一种方法来实现这一点。有几种方法可以实现这一点。1-用户可以编辑的白名单文件。2-用户无法编辑的黑名单文件3-不允许读取的黑名单文件扩展名。如果要检查的扩展名更多,则可以反转逻辑<代码>$allowedExtensions=array(“html”、“txt”、“xml”)$fileType=end(分解(“.”,$_GET[“file”]);如果(在_数组($fileType,$allowedExtensions)){//这是一个允许的文件}谢谢,但是用户仍然可以输入URL。例如,如果目录名为test,用户可以在URL中输入“../index.html”并查看网站主页的内容,我如何防止这种情况?不过,我相信将变量存储到会话变量中会比存储到URL中更安全
http://example.com/index.php?file=bob.html *
//checking file type by exploding the variable  
$fileType = end(explode(".", $_GET["file"]));
    if( $fileType != "php" && !empty($_GET["file"]) && !in_array($_GET["file"], $blacklist) && file_exists($_GET["file"])) {