Show a button based on a ip?

hello everyone i need your help again just a little help please how would you show a button for an ip

this is like admin role but i wanna do it by ip i have find a code that fits my needs
but its not working its a snippet as i read if you can help me fix it or get it working that be great

also Yes i did try this on localhost not working

heres the code

<?php

/*

this Snippet allows you to display content "ONLY" to a visitor from

a specific IP addresses in a page.

Change the $allowed "value" to the IP address you want.

*/

$allowed = 'IP Goes here';

$userip = $_SERVER;

if($userip == $allowed){

echo "This content is only viewed by the IP address you chose...";

}

?>

If you have error reporting turned on and displaying errors the script should show an error something like not being able to compare a string to an array.

Try this to find out the required array value.

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

echo '<pre>';
  print_r( $_SERVER );
echo '</pre>';

Edit:
Added error reporting to be only used locally and not online because potential server information may be used by hackers.

hi it does not show any errors with your error code it just shows me details about the localhost xmapp etc

The code John posted was meant to let you see what keys were available to use from the superglobal.
http://php.net/manual/en/reserved.variables.server.php

If you want to see the errors with your script, temporarily add the debugging lines to the beginning of your script. i.e.

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');
/*
this Snippet allows you to display content "ONLY" to a visitor from
a specific IP addresses in a page.
Change the $allowed "value" to the IP address you want.
*/
$allowed = 'IP Goes here';
$userip = $_SERVER;
if($userip == $allowed){
echo "This content is only viewed by the IP address you chose...";
} 
else { 
echo gettype($userip); 
echo gettype($allowed);
}
?>

If you do not get any error(s), then it follows that $userip does not loosely equal $allowed
The “gettype” lines should give you a clue.

1 Like

thank you i do get a error it says

arraystring

so is there a way to fix this i am grateful for all the help i get

I would personally just use an array and then check to see if the IP is in the array.

3 Likes

An array can not equal a string. They are different types. However, arrays can contain values that are strings. eg.

$some_array = array("key" => "value", "one" => "foo", "two" => "bar"); 
$test_string = "foo"; 
echo $some_array; // Array 
echo $some_array["one"]; // foo 
echo $some_array["two"]; // bar 
echo ($some_array === $test_string); // false 
echo ($some_array["one"] === $test_string); // true

See the problem?
* hint, look at the PHP documentation I linked to previously or your print_r and see what key you want to use.

1 Like

I am procrastinating so…

<?php 
  declare(strict_types=1);
  ini_set('display_errors', 'true');
  error_reporting(-1);

  $sAllowed = '184.22.226.100'; // 'IP Goes here';
  $sUserip  = $_SERVER['REMOTE_ADDR'];
  $aServer  = $_SERVER;

echo '<dl>';
  # RESULTS
    echo '<dt>Variables Submitted: </dt>'; 
    echo '<dd> $sAllowed  ==> '     .$sAllowed .'</dd>';
    echo '<dd> $sUserIp &nbsp; ==> ' .$sUserip .'</dd>';
    echo '<dd> &nbsp; </dd>';

    echo '<dd> gettype($sAllowed)  ==> '     .gettype($sAllowed) .'</dd>';
    echo '<dd> gettype($sUserIp) &nbsp; ==> ' .gettype($sUserip) .'</dd>';
    echo '<dd> gettype($aServer) &nbsp; ==> ' .gettype($aServer) .'</dd>';
    echo '<dd> &nbsp; </dd>';

  echo '<dt> if( in_array($sAllowed, $aServer) ) </dt>';
  if( in_array($sAllowed, $aServer) )
  {
    echo '<dd>NO PROBLEM ==> line ==> '. __line__ .'<dd>';
    echo '<dd> &nbsp; </dd>';
  }else{
    echo '<dd> PROBLEM: &nbsp; line ==> '. __line__ .'</dd>';
    echo '<dd> &nbsp; </dd>';
  }  

  echo '<dt> if($sAllowed === $sUserip) </dt>';
  if($sAllowed === $sUserip)
  {
    echo '<dd> NO PROBLEM: &nbsp; line ==> '. __line__ .'</ddf>';
    echo '<dd> &nbsp; </dd>';
  }else{  
    echo '<dd> PROBLEM: &nbsp; line ==> '. __line__ .'</dd>';
  }

  echo '<dt> if($sAllowed===$aServer) </dt>';
  if($sAllowed===$aServer)
  {
    echo '<dd>NO PROBLEM ==> line ==> '. __line__ .'<dd>';
  }else{
    echo '<dd> PROBLEM: &nbsp; line ==> '. __line__ .'</dd>';
    echo '<dd> &nbsp; </dd>';
  }  

  echo '<dt>if($aServer==$sAllowed) </dt>';
  if($aServer===$sAllowed)
  {
    echo '<dd>OK ==> line ==> '. __line__ .'</dd>';
  }else{
    echo '<dd> PROBLEM: &nbsp; line ==> '. __line__ .'</dd>';
    echo '<dd> &nbsp; </dd>';
  }  

echo '</dt>';

  echo '<pre>';
    # print_r($_SERVER);
  echo '</pre>';

Result:

Variables Submitted:
$sAllowed ==> 184.22.226.100
$sUserIp ==> 184.22.226.100

gettype($sAllowed) ==> string
gettype($sUserIp) ==> string
gettype($aServer) ==> array

if( in_array($sAllowed, $aServer) )
NO PROBLEM ==> line ==> 25

if($sAllowed === $sUserip)
NO PROBLEM: line ==> 36

if($sAllowed===$aServer)
PROBLEM: line ==> 47

if($aServer==$sAllowed)
PROBLEM: line ==> 56

1 Like

this did not help me but something in that link did or the print_r but thanks for all the help i am so happy i got it sorted out

it was
$_SERVER[“SERVER_ADDR”];

i had to use :slight_smile:

1 Like

thanks for the help also but i got it sorted now

1 Like

I think if you read the documentation, you will probably find that this is not the correct one to use, unless I have misunderstood your requirements.

If you’re using the IP address to control who can get into the admin site, don’t forget to have a plan in place for if that IP address ever changes, or becomes unavailable for some reason and you need to get into admin to change or add another allowed IP.

3 Likes

$_SERVER["SERVER_ADDR"]

is your web server’s IP address.

$_SERVER["REMOTE_ADDR"]

might be your user’s IP address.

3 Likes

i have try using this on localhost it dont work so i am using server_addr for now

On localhost, it’s not supposed to show your “actual” IP. It uses 127.0.0.1 because it “thinks” that’s your IP. On a live server, this IP changes to the real user’s IP.

1 Like

yes i understand now i got it working as ive uploaded it to free hosting just to test that part

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.