我希望我的用户只能访问我的PHP文件,如果他们尝试访问我想包括我的网页404文件的文件夹 [英] I want my users to only access my php files, if they try to access the folders I want to include my page 404 file

查看:242
本文介绍了我希望我的用户只能访问我的PHP文件,如果他们尝试访问我想包括我的网页404文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的查询字符串就像你看到下面包括在我的网页我的网页,它做工精细,进出口包括我的网页罚款。

但蹊跷的是发生,我不是找我怎么能解决这个问题。

我会尽量expain我ISSE一个例子:我有一个文件夹老师里面我有两个PDF文档和网页documents.php。 接取此文件的网页,即时通讯acessing:HTP://本地主机/网站/教师/文件,这是工作的罚款

但是,如果我接取。HTP://本地主机/网站/教师/而言,我能为你在我下面的图片看到接取我的PDF文档和我的网页

但我不想这样,我想,如果一些用户尝试接取HTP://本地主机/网站/教师/,我想包括我的404文件( require_once('INC / 404.php');

我的查询字符串:

  @ $ URL = $ _GET ['URL'];
$ URL =爆炸('/',$网址);
$网址[0] =($网址[0] == NULL'指数':$网址[0]);

如果(file_exists('增量/'.$网址[0]。。PHP的)){
     require_once('INC。/'.$ URL [0]PHP);
}
的elseif(file_exists($网址[0]。/。$网址[1]。/。$网址[2]。'的PHP)){
     require_once(。。。$网址[0]'/'$网址[1]'/'$网址[2]。PHP的');
}
的elseif(@file_exists($网址[0]。/。$网址[1]。。PHP的)){
     require_once(。$网址[0]'/'$ URL [1]。PHP的);
}
其他{
     require_once('INC / 404.php');
}
 

你看到什么即时做错了不被有我想要的结果?

我的htaccess文件:

  RewriteEngine叙述上
的RewriteCond%{} SCRIPT_FILENAME!-f
的RewriteCond%{} SCRIPT_FILENAME!-d
重写规则^(。*)$的index.php?URL = $ 1
 

解决方案

有一个简单的解决方案。

将此行添加到你的.htaccess文件的开头:

 选项-Indexes
 

这样一来,你就无法看到该文件夹​​的内容。


编辑:htaccess的规则解决方案(这是在网​​站文件夹)

 的ErrorDocument 404 /website/inc/404.php

RewriteEngine叙述上
的RewriteBase /网站/

重写规则^教师/ $  -  [R = 404,L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$的index.php?URL = $ 1 [L]
 

I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.

But something wrong is happening and Im not finding how I can solve this.

I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php". To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.

But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.

But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)

My query string:

@$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);

if(file_exists('inc/'.$url[0].'.php')){
     require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
     require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(@file_exists($url[0].'/'.$url[1].'.php')){
     require_once($url[0].'/'.$url[1].'.php');
}
else{
     require_once('inc/404.php');
}

Do you see what Im doing wrong to not be having the result I want?

My htaccess file:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

解决方案

There is an easier solution.

Add this line to the beginning of your .htaccess file:

Options -Indexes

This way, you won't be able to see the folder contents.


EDIT: htaccess rule solution (which is in website folder)

ErrorDocument 404 /website/inc/404.php

RewriteEngine On
RewriteBase /website/

RewriteRule ^teachers/$ - [R=404,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

这篇关于我希望我的用户只能访问我的PHP文件,如果他们尝试访问我想包括我的网页404文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆