2

Example 1

<?php function Piso() { $ip = '77.1.1.1.1'; $ip2 = $_SERVER['REMOTE_ADDR']; return ($ip == $ip2); } ?> 

Example 2

<?php function Piso() { $ip = fopen("bune.txt", "r"); $ip2 = $_SERVER['REMOTE_ADDR']; return ($ip == $ip2); } ?> 

I want to read IP address from file bune.txt. If i read it == doesnt work. If i put ip address like example one, it works. But i need example 2 to work. Example two doesnt work, but i dont know how to read it to can $ip == $ip2. Thanks.

2
  • 3
    fopen does not read file contents. It just sets you up for reading. If you want an one-stop-shop for reading, use file_get_contents instead. As an aside, you should have tried var_dump($ip) immediately after you got your first unexpected result. Commented Feb 13, 2014 at 20:43
  • Doesnt work with file_get_contents too, i check it more then 5 times :) $ip = file_get_contents('bune.txt', true); Commented Feb 13, 2014 at 20:46

3 Answers 3

5
<?php function Piso() { $ip = file_get_contents("bune.txt"); $ip2 = $_SERVER['REMOTE_ADDR']; return ($ip == $ip2); } ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

@michaelbn: ha, it was from the OP question.
@AbraCadaver yep, but I read this and accept as right for an answer.
3

Assuming there is only 1 IP address and nothing else in the file:

$ip = trim(file_get_contents("bune.txt")); 

1 Comment

If you want to ensure you got an IP in either IPv4 or IPv6. Read the docs Validation if(filter_var($ip, FILTER_VALIDATE_IP)){ echo "Valid IP!"; }else{ echo "Invalid IP!"; } ?>
3

This is because PHP's fopen() function returns a file handle (type resource) to read (because you passed "r") as stream from the file.

Instead of fopen() use the simple function file_get_contents() which will return the content of the file.

function Piso() { // Absolute path is faster. $file = __DIR__ . DIRECTORY_SEPARATOR . "bune.txt"; // Confirm that the file exists and is readable. if (is_readable($file) === false) { return false; } // Use trim() to remove white space and compare read IP against remote address. return (trim(file_get_contents($file)) == $_SERVER["REMOTE_ADDR"]); } 

6 Comments

Your answer is similar to my, but You prefer to return result in 1 line. That's good, I like it, but in this way, i think we should use @ before file_get_content ;)
That's a hack, you should never use it. I extend my answer.
Why I should not use native PHP error's silincer? Your code looks like production edition, my - dev. In production You should not show any info to potential hacker, so..
he's testing at the place of just hiding errors, if the file is readable file_get_contents won't return an error. dev or production you should repair errors not hide them
@CodeBird You should repair errors before production. At production You must (yeah, must) hide language's errors and catch every error with Your error-logger. If You show Your file structure to hacker - this is a first leak.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.