<?php
include "config.php";
if(
$_GET['view_source']) view_source();
if(
$_GET['logout'] == 1){
  
$_SESSION['login']="";
  exit(
"<script>location.href='./';</script>");
}
if(
$_SESSION['login']){
  echo 
"hi {$_SESSION['login']}<br>";
  if(
$_SESSION['login'] == "admin"){
    if(
preg_match("/^172\.17\.0\./",$_SERVER['REMOTE_ADDR'])) echo $flag;
    else echo 
"Only access from virtual IP address";
  }
  else echo 
"You are not admin";
  echo 
"<br><a href=./?logout=1>[logout]</a>";
  exit;
}
if(!
$_SESSION['login']){
  if(
preg_match("/logout=1/",$_SERVER['HTTP_REFERER'])){
    
header('WWW-Authenticate: Basic realm="Protected Area"');
    
header('HTTP/1.0 401 Unauthorized');
  }
  if(
$_SERVER['PHP_AUTH_USER']){
    
$id $_SERVER['PHP_AUTH_USER'];
    
$pw $_SERVER['PHP_AUTH_PW'];
    
$pw md5($pw);
    
$db dbconnect();
    
$query "select id from member where id='{$id}' and pw='{$pw}'";
    
$result mysqli_fetch_array(mysqli_query($db,$query));
    if(
$result['id']){
      
$_SESSION['login'] = $result['id'];
      exit(
"<script>location.href='./';</script>");
    }
  }
  if(!
$_SESSION['login']){
    
header('WWW-Authenticate: Basic realm="Protected Area"');
    
header('HTTP/1.0 401 Unauthorized');
    echo 
"Login Fail";
  }
}
?><hr><a href=./?view_source=1>view-source</a>