kaleidpixel / geoipallow
一个简化工具,用于生成 .htaccess 配置,允许网站仅从指定国家的 IP 地址进行访问。增强网站的安全性并更有效地定位受众。
1.0.0
2023-10-05 00:59 UTC
Requires
- php: ^7.3||^8.0
- ext-curl: *
- ext-fileinfo: *
- ext-intl: *
- ext-json: *
- ext-mbstring: *
README
一个简化工具,用于生成 .htaccess 配置,允许网站仅从指定国家的 IP 地址进行访问。增强网站的安全性并更有效地定位受众。
文档
编码相当简单。选项很简单,所以你不会迷路。
选项
方法
基本标记
以下是最简单的编码。
示例 1
<?php namespace exampleproject; use kaleidpixel\GeoIPAllow; class JPIPAllow { /** * @var GeoIPAllow */ protected $geoIpAllow = null; public function __construct( array $setting = [] ) { $setting['country'] = 'JP'; if ( empty( $setting['output_path'] ) ) { $setting['output_path'] = dirname( __DIR__ ) . DIRECTORY_SEPARATOR . '.htaccess'; } $this->geoIpAllow = new GeoIPAllow( $setting ); } /** * @param bool $echo * @param bool $force * * @return void */ public function read( bool $echo = false, bool $force = false ): void { $this->geoIpAllow->read( $echo, $force ); } }
示例 2
<?php require_once dirname( __DIR__ ) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use kaleidpixel\GeoIPAllow; $now = date( 'Y-m-d' ); $before_str = <<<EOL ## IP address of its In-House server. Allow from 103.xxx.xxx.xxx Allow from 203.xxx.xxx.xxx EOL; $ip = new GeoIPAllow( [ 'server' => 'apache', 'country' => 'JP', 'output_path' => __DIR__ . DIRECTORY_SEPARATOR . '.htaccess', 'add_before_str' => $before_str ] ); $ip->read(true);
上面显示的源代码可以在内置的 Web 服务器上运行。它也可以在 CLI 中运行,所以选择你喜欢的。
如果你想在内置的 Web 服务器上运行它,执行以下命令,然后通过 Web 浏览器访问。
$ php -S localhost:8080
如果你想在 CLI 中运行它,执行以下命令。文件输出的路径将作为结果显示。
$ php ./example/index.php
许可证
MIT 许可证
版权所有 © 2023 Kaleid Pixel