Jump to content

isset($_SERVER['REMOTE_ADDR'])


doubledee

Recommended Posts

I'm 99% sure the server majority, if jot all, of the SERVER variables are always set. Therefore, they are always accessible and you do not need to test if they are set.

 

But to be Devil's Advocate, if it wasn't, then my code would error-out, right?

 

 

Debbie

 

Link to comment
Share on other sites

In the hypothetical situation the REMOTE_ADDR server variable wasn't set then your code would error but every seb server requires an ip address. I suopose you could write your own server which doesn't require an ip Address but it wouldn't be able to connect to the Internet.

Link to comment
Share on other sites

In the hypothetical situation the REMOTE_ADDR server variable wasn't set then your code would error but every seb server requires an ip address. I suopose you could write your own server which doesn't require an ip Address but it wouldn't be able to connect to the Internet.

 

$_SERVER['REMOTE_ADDR'] is the IP of the client, not the server.

Link to comment
Share on other sites

I'm 99% sure the server majority, if jot all, of the SERVER variables are always set. Therefore, they are always accessible and you do not need to test if they are set.

 

The variables defined can vary based on the server and the request, but some are fairly standard and very likely to be defined (such as REMOTE_ADDR).  Any index with the HTTP_* prefix may need to be checked as those are usually derived from headers which may or may  not be present in the request.  IIS used to not define DOCUMENT_ROOT, but newer versions do now.

 

If you plan on ever using the script in a CLI environment as well, then you will want to check as a lot of them will not be defined there.

 

Link to comment
Share on other sites

In the hypothetical situation the REMOTE_ADDR server variable wasn't set then your code would error but every seb server requires an ip address. I suopose you could write your own server which doesn't require an ip Address but it wouldn't be able to connect to the Internet.

 

$_SERVER['REMOTE_ADDR'] is the IP of the client, not the server.

 

Oops, getting all confused with the different SERVER possibilities :\. Thank you for correcting me!

Link to comment
Share on other sites

The variables defined can vary based on the server and the request, but some are fairly standard and very likely to be defined (such as REMOTE_ADDR).  Any index with the HTTP_* prefix may need to be checked as those are usually derived from headers which may or may  not be present in the request.  IIS used to not define DOCUMENT_ROOT, but newer versions do now.

 

If you plan on ever using the script in a CLI environment as well, then you will want to check as a lot of them will not be defined there.

 

So what would you recommend I do?

 

Use the code in my OP?

 

 

Debbie

 

Link to comment
Share on other sites

Unless you plan on using the code in a CLI environment, you can assume $_SERVER['REMOTE_ADDR'] will be set and do not need to check it.

 

If you want to be thorough though, never hurts to use isset().

 

The purpose of my OP was whether you could check isset() on a function like $_SERVER['REMOTE_ADDR'] or not.

 

Sounds like you can.

 

Thanks,

 

 

Debbie

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.