user3315515
user3315515

Reputation: 11

PHP Echo File Contents With Control

Firstifully I'm very new to php. I'm trying to show a text file's content with php. But it will check the user ip. If Ip is equals to my ip it will show to file. I used this code:

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

But I need to hide the example.txt . So noone can go to example.txt and display the content. Thanks

Upvotes: 0

Views: 117

Answers (3)

Jentel
Jentel

Reputation: 154

<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.

that should do it

Upvotes: 1

mehmetseckin
mehmetseckin

Reputation: 3107

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

Upvotes: 0

zeomega
zeomega

Reputation: 355

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

Upvotes: 0

Related Questions