Advertisement
Guest User

External restriction wordpress

a guest
Jul 13th, 2012
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. $i = 0; // Declaring variable i as "0"
  3. //$ip = '202.89.1.54'; // Test External IP
  4. //$ip = '192.168.0.17'; // Test Internal IP
  5. $ip = $_SERVER['REMOTE_ADDR']; // Gets Actual IP (If on same network as server will return most likely 192.168 address, if not will return public IP)
  6. $ip = (string)$ip; // Convert the IP to a string to make sure the comparison can be done with no data type problems
  7. $accept = 'N'; // Declaring the variable accept as "N"
  8. while ($i != 256 && $accept == 'N') { // while $i is not equal to 256 and accept is equal to "N" do the following; we want it to stop either when accept changes OR $i reaches 256
  9.     $ipallow = '192.168.0.' . $i; // sets the variable ipallow to 192.168.0. and whatever integer $i is at the time so 192.168.0.1 then 192.168.0.2 up to 256 in this case
  10.     $ipallow = (string)$ipallow; // setting the ipallow variable as a string instead of a string and integer
  11.     //echo $ipallow." "; // uncomment to test
  12.     if ($ip == $ipallow) { //during each time it goes through it will check to see if the IP its currently got/checking with matches your IP
  13.     $accept = 'Y'; // if there is a match then it changes the accept variable we set to "Y"
  14.     }else{ // if it didnt match
  15.         $accept = 'N'; //although this isnt needed i put this in for safetys sake to guarantee accept will equal no
  16.     }
  17.     $i++; //after checking the ip it will take the number of $i and add 1 to $i, another way to acheive this is $i = $i +1; but this is a much shorter way to do it
  18. }
  19. if($accept == 'Y' || is_user_logged_in()){ // as i creates this for wordpress i want to block external access using the accept variable or check if the user is logged in delet "|| is_user_logged_in()" and replace with whatever your site requires to limit external access
  20.  //echo $accept;
  21.     global $intauth; // set a global variable if you want to use that
  22.     $intauth = "authtrue"; // defines the global variable
  23.     setcookie('allow', 'Yes', 0, '/'); // i prefer to use a session cookie which will be valid until the browser closes, so depending on how you want to use thif file you could require this file once and the cookie will be valid throughout the entire browsing session
  24.     //echo $_COOKIE['allow'];
  25. }else{ // again if accept is equal to "N" or there is no user logged in
  26.     setcookie('allow', 'No', 0, '/'); // this will set the cookie to "No"
  27.     //echo $_COOKIE['allow']; // this is how you would display a cookie value to get the cookie value you would use $variable = $_COOKIE['allow'];
  28.     //cookie work in this way, after the initial (, the first parameter is the name of the cookie, the second is the value of the cookie, third is the expire time use 0 for session cookie and the / is for the directory the cookie is valid, / is the whole site but if you had a sub dir that it needed to be valid in only you would use /subdirname/
  29. header("Location: /wp-login.php"); //in my case i want to redirect a user to wp-login.php if they arent in the local network, so only registered users can access the site
  30. }
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement