Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有控件的PHP Echo文件内容_Php_File_Show - Fatal编程技术网

带有控件的PHP Echo文件内容

带有控件的PHP Echo文件内容,php,file,show,Php,File,Show,首先,我对php非常陌生。我试图用php显示文本文件的内容。但它将检查用户ip。如果Ip等于我的Ip,它将显示到文件。我使用了以下代码: <?php echo file_get_contents( "example.txt" ); ?> 但是我需要隐藏example.txt。因此,没有人可以转到example.txt并显示内容。 谢谢你可以试试这个: if ($_SERVER["REMOTE_ADDR"] == "MYIP") { echo file_get_conten

首先,我对php非常陌生。我试图用php显示文本文件的内容。但它将检查用户ip。如果Ip等于我的Ip,它将显示到文件。我使用了以下代码:

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

但是我需要隐藏example.txt。因此,没有人可以转到example.txt并显示内容。 谢谢你可以试试这个:

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

用您的IP替换MYIP,例如:127.0.0.1

<?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'";
}

命令拒绝,允许
允许从server.ip.xxx.xxxx 127.0.0.1
全盘否定
将其添加到您的
.htaccess
文件中


应该可以了

你在哪里检查IP?你到底想要什么?隐藏文本文件以避免直接访问,或者不想向其他人显示文件的\u get\u content()值?OP不希望使用
执行此操作。htaccess
这是有效的,但OP将此问题标记为
php
。您应该推荐一个php解决方案。我还是投了赞成票,但下次请小心标签。
<Files example.txt>
    Order Deny,Allow 
    Allow from server.ip.xxx.xxxx 127.0.0.1
    Deny from all
</Files>