禁止浏览器直接访问程序模板文件代码
很多cms的程序主题模板文件默认情况下在浏览器中输入模板文件的路径可以直接访问,并会显示模板的源代码,因此很容易被一些不良主题模板开发者窃取劳动成果,所以在制作模板或二开模板时对模板文件添加安全保密代码非常有必要,下面是LzxBlog给出的有效防范代码:
第一种方法:把下面的代码添加至主题目录的文件里开始位置(即第一行,所有不想被访问的文件都要添加哦!):
<?php header('HTTP/1.1 403 Forbidden'); echo '<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Your request is illegal!</title><link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"><style>html, body {background-color: #1f2d26;color: #636b6f;font-family:"Nunito", sans-serif;font-weight: 200;height: 100vh;margin: 0} .full-height {height: 100vh} .flex-center {align-items: center;display: flex;justify-content: center} .position-ref {position: relative} .top-right {position: absolute;right: 10px;top: 18px} .content {text-align: center} .title {font-size: 84px} .links > a {color: #636b6f;padding: 0 25px;font-size: 13px;font-weight: 600;letter-spacing: .1rem;text-decoration: none;text-transform: uppercase} .m-b-md {margin-bottom: 30px}</style></head><body><div class="flex-center position-ref full-height"><div class="content"><div class="title m-b-md">Your request is illegal!</div></div></div></body></html>'; exit(); ?>
大部分的php程序模板机制是在激活模板时自动编译生成可读的模板文件,编译时会自动过滤上面的代码,因此不会对网页有影响,代码也不会出现在网页源代码中,只有当用户从浏览器直接访问模版文件时才会生效,所以不必担心会导致出错。
第二种方法:利用伪静态的方法:
location ^~ /zb_users/theme/tpure/template/ { deny all; }
把上面的 zb_users/theme/tpure/template 改成你不想被访问的文件夹路径(文件夹内不要包含css,js,image这些资源哦,否则你的网站可能出现问题的)
博主在Z-Blog程序测试的,效果:https://www.liuzhixi.cn/zb_users/theme/tpure/template/header.php
写在最后:
如果两个方法同时使用的话,第二种的优先级高于第一种!
两种方法各有各的优点,根据自己的需求选择!
版权声明:本文由 LzxBlog 发布,如需转载请注明出处。