0

I have made a PHP page which shows a list of files in one folder. Now that I don't want some one to access this folder directly, I plan to add a PHP script which redirects to index.php. Simply I don't want this file to appear in the list

Can I add exception to this extension only? or any better idea for that?

if ($handle = opendir('manual')) {
    while (false !== ($entry = readdir($handle))) {
           if ($entry != "." && $entry != "..") {
               echo "<a href='manual/$entry' target='_blank'>$entry</><br>";
           }
     }
     closedir($handle);
}
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • You can add a `.htaccess` rule inside the `manual` folder which will prevent access to that directory -- but then all your links the above code outputs will be broken. In which case, instead of referring to 'manual/$entry' call another file like `foo.php?file=$entry` which foo.php is responsible for getting the contents of `$entry`. This overall is really sloppy. – Kraang Prime Jan 06 '17 at 01:57

2 Answers2

1

If you don't want your index.php file to be displayed in this list then simply use an if statement.

if ($handle = opendir('manual')) {
    while (false !== ($entry = readdir($handle))) {
           if ($entry != "." && $entry != "..") {
               if($entry!='index.php') // Go ahead only if the file is not index.php
                   echo "<a href='manual/$entry' target='_blank'>$entry</a><br>";
           }
     }
     closedir($handle);
}

And, if you want to hide all the php files, then you can use preg_match:

if ($handle = opendir('manual')) {
    while (false !== ($entry = readdir($handle))) {
           if ($entry != "." && $entry != "..") {
               if (!preg_match('/.php/', $entry)) // Go ahead only if the file is not having .php as it's extension
                   echo "<a href='manual/$entry' target='_blank'>$entry</a><br>";
           }
     }
     closedir($handle);
}
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
0

you can simply use a flag variable to identify the status of visitor,if has no the variable,let he redirect. Usually we use session the global variable to do the work

yuen
  • 1
  • 1