简体   繁体   English

通过php编辑htaccess

[英]edit htaccess via php

I have a script that on certain events blocks someones ip via a htaccess deny from. 我有一个脚本,在某些事件中会通过htaccess拒绝来阻止某人的ip。 This is the code: 这是代码:

file_put_contents('.htaccess', 'deny from ' . $_SERVER['REMOTE_ADDR'] . "\n", FILE_APPEND);

Yet what i want is instead od deny from this user can i have it redirect the user to another page. 但是我想要的却是对此用户的拒绝,我可以让它将用户重定向到另一个页面。

I can do this via: 我可以通过以下方式做到这一点:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.45\.67\.89
RewriteRule \.html$ /alternate_page.html [R=302,L]

But is it possible to do it without opening the htaccess and appending to it. 但是有可能这样做而无需打开htaccess并附加到它。 Instead use the file_put_contents function 而是使用the file_put_contents函数

Thanks A Lot. 非常感谢。

You don't need to use .htaccess for redirecting users to other pages, just use the location header based on what you get from $_SERVER['REMOTE_ADDR'] : 您无需使用.htaccess即可将用户重定向到其他页面,只需根据从$_SERVER['REMOTE_ADDR']获得的内容使用location 标头

header("Location: http://anothersite.com")

and don't forget this (from the PHP manual): 并且不要忘记这一点(来自PHP手册):

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. 请记住,必须先通过常规HTML标记,文件中的空白行或从PHP发送任何实际输出,然后调用header()。 It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. 使用include()或require(),函数或另一个文件访问函数读取代码,并在调用header()之前输出空格或空行,这是一个非常常见的错误。

What you can do is have your .htaccess file like this: 您可以做的是将您的.htaccess文件设置如下:

Options +FollowSymLinks
RewriteEngine on
# ADD RULES HERE
RewriteCond x x # This is a dummy condition that is always true
RewriteRule \.html$ /aternate_page.html [R=302,L]

Then you can have your replacement work like this: 然后,您可以进行如下替换工作:

<?php
file_put_contents(".htaccess",str_replace("# ADD RULES HERE","# ADD RULES HERE\nRewriteCond %{REMOTE_HOST} ^".$_SERVER['REMOTE_ADDR']."$",file_get_contents(".htaccess"));
?>

HTH 高温超导
(Edit: This doesn't seem quite right... So may need a little editing. Hope the general idea helps, though.) (编辑:这似乎不太正确...因此可能需要一些编辑。不过希望总体思路有所帮助。)

The proper way would be to use a gatekeeper function, which checks the IP, and forwards the request to the required page on need. 正确的方法是使用网闸功能,该功能检查IP,然后根据需要将请求转发到所需页面。

<?php

header('Location: http://www.yoursite.com/new_page.php');

?>

You should call this before any output is sent to the browser, including any HTML tags or white space. 在将任何输出发送到浏览器(包括任何HTML标记或空格)之前,应先调用此方法。 The common way is to have a 'forward' function wrapping the header command, and do access control first, before sending any content. 常见的方法是具有“转发”功能,该功能包装标头命令,并在发送任何内容之前先进行访问控制。

I would recommend storing IP addresses you do not want to allow in a database. 我建议将您不想允许的IP地址存储在数据库中。 This makes administration and if you want stats, they can be added easily as well (ie. How many times has this user tried to access the site after being denied, etd). 这样就可以进行管理,如果需要统计信息,也可以轻松添加它们(例如,该用户被拒绝后尝试访问该站点的次数等)。 You could even send certain IPs to different location if you wanted. 如果需要,您甚至可以将某些IP发送到其他位置。

  • Store the IP addresses in a database table 将IP地址存储在数据库表中
  • When a user comes to the page check to see if their IP is in the table 当用户进入页面时,检查其IP是否在表格中
  • If so increment stats counter (if desired) and redirect user to new location 如果是这样,则增加统计计数器(如果需要)并将用户重定向到新位置
  • Or continue loading the current page 或继续加载当前页面

Use php's Header function Header('Location: [url here]'); 使用php的Header函数 Header('Location: [url here]'); to redirect them and remember it must be before any other content is sent to the page. 重定向它们,并记住它必须在任何其他内容发送到页面之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM