Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP IP Address Capture Statement

I am trying to make it so the content is only for my IP address, and someone elses. I tried:

<?php
$ip = $_SERVER['REMOTE_ADDR'];

if ($ip = "FirstIp" && $ip="SecondIp") {
echo "Protect Content";
} else {
echo "You do not have permission to look at this.  Your IP has been recorded. $ip";
$fileofips="list.txt";
$ip = "$_SERVER[REMOTE_ADDR]";

$file=fopen($fileofips, 'a');
fwrite($f, $_SERVER[REMOTE_ADDR]);

fclose($f);
}
?>

And it won't even capture intruding IPs. Please tune up the code ;P

like image 641
Ryan Avatar asked Sep 29 '26 13:09

Ryan


2 Answers

if ($ip = "FirstIp" && $ip="SecondIp") {

A single equals sign is an assignment, not a comparison. Also, you probably meant ||; $ip can't be two things at once.

if($ip === "FirstIP" || $ip === "SecondIP") {

Also, you probably want newlines between your IPs in the file, so add a \n. Also also, putting a variable in double quotes like this:

$ip = "$_SERVER[REMOTE_ADDR]";

is pointless. Use the variable.

like image 108
Ry- Avatar answered Oct 02 '26 01:10

Ry-


Your if statement is overwriting the $ip variable because you're using a single =; Change it to ==, and also the && to an || (one IP can't be two at the same time):

if ($ip == "FirstIp" || $ip == "SecondIp") {
like image 39
newfurniturey Avatar answered Oct 02 '26 03:10

newfurniturey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!