【问题标题】:PHP Echo File Contents With ControlPHP Echo 文件内容控制
【发布时间】:2014-04-11 16:27:56
【问题描述】:

首先我对 php 很陌生。我正在尝试使用 php 显示文本文件的内容。但它会检查用户ip。如果 Ip 等于我的 ip,它将显示到文件中。我使用了这段代码:

<?php
echo file_get_contents( "example.txt" ); 
?>

但我需要隐藏 example.txt 。所以没有人可以去example.txt并显示内容。 谢谢

【问题讨论】:

标签: php file show


【解决方案1】:
<Files example.txt>
    Order Deny,Allow 
    Allow from server.ip.xxx.xxxx 127.0.0.1
    Deny from all
</Files>

将其添加到您的 .htaccess 文件中。

应该这样做

【讨论】:

  • OP 不希望使用 .htaccess 这样做
  • 这是有效的,但 OP 将此问题标记为 php。您应该建议一个 php 解决方案。无论如何我都投了赞成票,但下次请注意标签。
【解决方案2】:

你可以试试这个:

if ($_SERVER["REMOTE_ADDR"] == "MYIP")
{
  echo file_get_contents( "example.txt" );
}

将 MYIP 替换为您的 IP 例如:127.0.0.1

【讨论】:

    【解决方案3】:

    试试这样的。

    <?php
    /**
     * Try and get the client's IP address.
     */
    function getClientIPAddress() {
        $ip;
        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else
            $ip = "UNKNOWN";
        return $ip;
    }
    
    $myIpAddress = "10.1.1.1"; // Your IP Address
    $filename = "example.txt"; // file to get contents
    
    if(getClientIPAddress() === $myIpAddress) {
        echo file_get_contents( $filename  ); 
    }
    else {
        echo "You are not authorized to see the contents of '$filename'";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多