0

How do I go about assigning multiple values for a single variable in PHP?

I'm trying to do this

$IP = '111.111.111.111' || '222.222.222.222' || '333.333.333.333';

if ($_SERVER['REMOTE_ADDR'] != $IP) {
echo "Your IP is not on the list";

Is that even a proper way to do that?

Or should I put them in an array like this

$IP = array('111.111.111.111', '222.222.222.222', '333.333.333.333');

But then how would I go about checking if the $_SERVER['REMOTE_ADDR'] is one of the values inside the array?

Thanks

2 Answers 2

3

Yes, you should put it in array an use in_array() function instead of != operator:

if (!in_array($_SERVER['REMOTE_ADDR'], $IP))
Sign up to request clarification or add additional context in comments.

Comments

1

this is what you're looking for

/** my own array preference **/
$allowedIPS = [ '111.111.111.111', '222.222.222.222', '333.333.333.333' ];

/** alternate syntax **/
$allowedIPS = array ( '111.111.111.111', '222.222.222.222', '333.333.333.333' );

if (false === in_array($_SERVER['REMOTE_ADDR'], $allowedIPS)) {
    echo "Your IP is not on the list";
}

3 Comments

thanks. these [ ] surrounding the IP's causes dreamweaver to give a syntax error.
Dreamweaver probably doesn't understand that array syntax, I added the alternate version in my edit.
Thank you for your help. How could I tell it to match only the first part of the IP address instead of the entire IP? For instance, if the IP is 111.111.111.111 but will match the entire range of 111.111.111.xxx. or 111.111.xxx.xxx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.