Forum Moderators: coopster

Message Too Old, No Replies

Unsure about a line in some PHP code

SERVER['REMOTE_ADDR'] issue

         

gmac6791

8:20 pm on Jan 19, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



Doing some work on a site for someone, and he asked me what this line does, and it's just beyond my PHP skills at this point. Can someone explain what this line actually means, and why it would be there? Is literally the only line in the file, except for the php open tag and some remarks that don't explain it (real IP replaced by x's):
if($_SERVER['REMOTE_ADDR'] != 'xxx.xx.x.xxx') return;


Thanks!

lucy24

8:30 pm on Jan 19, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is the real IP perchance the site owner’s IP address? The first thing that comes to mind is that the code in question is intended to execute for everyone except the site owner. If there doesn't happen to be any code, is it possible this bit is left over from some earlier stage in the site’s development, and they left the empty envelope in case someone in the future wanted to use it again?

gmac6791

8:37 pm on Jan 19, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



The owner says the IP resolves to somewhere in India, and he did have a developer from that country working on the site at one time. I was thinking as well that it's something do to with that person's work, and might not be needed. Just want to be sure before I make that call.

JorgeV

9:52 pm on Jan 19, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

This line of code means that if the IP address is not xxx.xx.x.xxx to "quit" the current "block".

What's the name of the file in which this line appears?

Let's say it's "is_myself.php" , then it can be used this way:


// some code executed for everybody.

require('is_myself.php');

// some code to be executed only if this is the dev. (myself = ip == xxx.xx.x.xxx)


This can also be used within a function.

The advantage, is that, if you want to change the IP address, you just edit the is_myself.php. Or if you want to add other tests, like for example, testing the logged user, etc...

gmac6791

11:07 pm on Jan 19, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



Aha, understood! Thanks JorgeV!

w3dk

11:15 pm on Jan 19, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



...the code in question is intended to execute for everyone except the site owner.


Looks more like the opposite.

Probably for debugging, as @JorgeV suggests. But could also be a backdoor of unsavoury intent (unlikely)!

lucy24

4:20 am on Jan 20, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looks more like the opposite.
Dang. I entirely overlooked the significant return at the end of the line.

JorgeV

4:02 pm on Jan 20, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

some remarks that don't explain it

By curiosity, can you share these remarks?

gmac6791

6:33 pm on Jan 20, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



By curiosity, can you share these remarks?

This:

/**
*
* LICENSE:
*
* This file may not be redistributed in whole or significant part, or
* used on a web site without licensing of the enclosed code, and
* software features. Copyright © [sitename].com
*
*/

lucy24

8:11 pm on Jan 20, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Haha. You’re right: that sheds no light whatsoever :)

:: idly thinking that if this were the kind of site that did casual polls, I would ask how many comment lines people use before switching from
// blahblah
on every line
to
/*
blahblah
*/
enfolding the whole thing ::

JorgeV

9:56 pm on Jan 20, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

Just to add.

Here is the proper way to have multi-line comments. This is the standard and good practice. Code editors are formatting comments like that, by default. This is a legacy of programming languages like C/C++.


/**
*
*
*
*/


This kind of formatting is also the one looked for, when you have automation scripts which are parsing source code.

Using // on every line, also has an advantage. You can comment a large section of code, by using /* */ without minding about internal comments.

If internal comments are /* */, and you want to comment the large block, you'll have problem with opening and closing tags .

gmac6791

11:57 pm on Jan 20, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



Yes. To clarify, I'm not the one who wrote the code in this file, but I do know proper commenting protocols. :)

Thanks everyone for their help. *thumbsup*