doubledee Posted March 17, 2012 Share Posted March 17, 2012 I am trying to make sure I don't get an "Undefined Index" error. Will this code work... $ip = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''); Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/ Share on other sites More sharing options...
cpd Posted March 17, 2012 Share Posted March 17, 2012 You should never need to do this because the server should always have an ip address. $ip = $_SERVER['REMOTE_ADDR']; The above should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328492 Share on other sites More sharing options...
doubledee Posted March 17, 2012 Author Share Posted March 17, 2012 You should never need to do this because the server should always have an ip address. $ip = $_SERVER['REMOTE_ADDR']; The above should work fine. Oh... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328495 Share on other sites More sharing options...
cpd Posted March 17, 2012 Share Posted March 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328501 Share on other sites More sharing options...
doubledee Posted March 17, 2012 Author Share Posted March 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328503 Share on other sites More sharing options...
cpd Posted March 17, 2012 Share Posted March 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328504 Share on other sites More sharing options...
scootstah Posted March 17, 2012 Share Posted March 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328547 Share on other sites More sharing options...
kicken Posted March 17, 2012 Share Posted March 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328570 Share on other sites More sharing options...
cpd Posted March 17, 2012 Share Posted March 17, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328571 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328623 Share on other sites More sharing options...
kicken Posted March 18, 2012 Share Posted March 18, 2012 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(). Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328644 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259143-isset_serverremote_addr/#findComment-1328646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.