简体   繁体   English

PHP Echo文件内容与控件

[英]PHP Echo File Contents With Control

Firstifully I'm very new to php. 首先,我对php很陌生。 I'm trying to show a text file's content with php. 我正在尝试使用php显示文本文件的内容。 But it will check the user ip. 但是它将检查用户的ip。 If Ip is equals to my ip it will show to file. 如果Ip等于我的IP,它将显示到文件中。 I used this code: 我使用以下代码:

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

But I need to hide the example.txt . 但是我需要隐藏example.txt。 So noone can go to example.txt and display the content. 因此,没有人可以访问example.txt并显示其内容。 Thanks 谢谢

<Files example.txt>
    Order Deny,Allow 
    Allow from server.ip.xxx.xxxx 127.0.0.1
    Deny from all
</Files>

Add it to your .htaccess file. 将其添加到您的.htaccess文件。

that should do it 那应该做

You can try this : 您可以尝试以下方法:

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

replace MYIP with your IP for example : 127.0.0.1 用您的IP替换MYIP,例如:127.0.0.1

Try something like this. 尝试这样的事情。

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

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

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