Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Secure You're Admin Pages Or Any Page With This Code!

(Dec 26, 2014 03:24 AM)Internet Security Wrote:  I was searching the web and i found a few lines of code that ip lock a page to you're ip and redirect you to any link if you are not the owner.

Credits: Guinness

PHP Code

<?php
    $ip 
= $_SERVER['HTTP_CLIENT_IP'];
    if(
$ip!="IP Goes Here") {
        
Header("Location: http://google.com");
?>

I don't know if this has been seen on hf but i had to share it just in case it wasn't :)

Here's one better:
PHP Code

<?php
session_start
();
// Check if hidden behind Cloudflare
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$ip = $_SERVER['REMOTE_ADDR'];
$allowed_ips = array('admin_home' => '255.255.255.255');
foreach(
$allowed_ips as $auth_name => $auth_ip) {
if(
$auth_ip != $ip) {
     exit(
'You are not authorized to see this page.');
} else if(
$auth_ip == $ip) {
     
$_SESSION['auth_name'] = $auth_name;
     
$_SESSION['auth_ip'] = $auth_ip;
}
}
// Display page content down here, since we're authorized to see it.
?>

Now, what if we're not at home?
PHP Code

<?php
$auth_keys 
= array('1234567890abcedefghijklmnopqrstuvwxyz');
$auth_attempt = $_GET['auth']; // using get, but easy change to $_POST
if(!in_array($auth_attempt, $auth_keys)) {
     exit(
'You are not authorized to view this page.');
}
// Assumes we're authorized if we can still view page.
?>
Also don't just use header() add exit or die they can ignore the header location...


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by SchSecJun 24, 2017 03:01 PM 13 667 Jun 24, 2017 03:01 PM
Last Post: Nachfolger
[PHP] by ~RexonApr 28, 2017 01:45 PM 24 698 Apr 28, 2017 01:45 PM
Last Post: elunez
[PHP] by BorisGrigoryanNov 7, 2014 10:43 AM 4 164 Nov 7, 2014 10:43 AM
Last Post: Cathal