SZT
SZT

Reputation: 1966

Stop directory listing without .htaccess in a PHP website

My website host does not allow .htaccess. Now how do I stop directory listing in this situation?

Is there any option(s) other than index.php/index.html & .htaccess?

Upvotes: 2

Views: 2312

Answers (1)

Shadow
Shadow

Reputation: 9427

As you know, on an Apache server a directory listing is only given if there is no index.htm, index.html, index.php etc in the directory you are going to.

So the easiest solution, is a blank index.php in every folder - especially seeing as you are unable to change the config yourself.

I wrote the following code a little while ago - you put it in every folder you don't want people to get a directory listing of - it returns the user to the index of the site they attempted to visit.

You need to have a README.txt file in the root of the directory for this to work - or just change it to a file that is there.

<?php
    $i=0;
    while(!file_exists('README.txt')){
        chdir("..");
        $i--;
    }
    header("Location: ".substr($_SERVER['PHP_SELF'],0,strrpos(dirname($_SERVER['PHP_SELF']),'/',$i)));
?>

An easier alternative to the above is just:

<?php header("Location: /"); ?>

Upvotes: 3

Related Questions