<?php
session_start();
require_once "init.php";

$UserID = $_POST['UserID'];
$Password = $_POST['Password'];



// 定义一个函数getIP() 
if (isset($_SERVER)) { 
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
$realip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { 
$realip = $_SERVER['HTTP_CLIENT_IP']; 
} else { 
$realip = $_SERVER['REMOTE_ADDR']; 
} 
} else { 
if (getenv("HTTP_X_FORWARDED_FOR")) { 
$realip = getenv( "HTTP_X_FORWARDED_FOR"); 
} elseif (getenv("HTTP_CLIENT_IP")) { 
$realip = getenv("HTTP_CLIENT_IP"); 
} else { 
$realip = getenv("REMOTE_ADDR"); 
} 
} 





$sql = "select * from admins where UserID='$UserID'";
$result = $DB->query($sql);
$adminInfo = $DB->fetch_array($result);

if($adminInfo && $adminInfo['iStatus']==1){
	$nub=$adminInfo['nub']+1;
	$sql="update admins set dtLogin=now(),Zip='$realip',nub='$nub' where UserID='$UserID'";
    $DB->query($sql);
}
$DB->close();
if($adminInfo && $adminInfo['Password']==md5($Password)){
	if($adminInfo['iStatus']==1){
	   $_SESSION['AdmUserID_S'] = $adminInfo['UserID'];
	   $_SESSION['RealName_S'] = $adminInfo['RealName'];
	   $_SESSION['Perm_S'] = $adminInfo['Perm'];
       if($backUrl)header("Location: $backUrl");
	   else header("Location: index.php");
	}else{
	   $mess=urlencode("用户 $UserID 目前不生效");
	   header("Location: login.php?mess=$mess");
	}
}else{
	$mess=urlencode("用户 $UserID 不存在或密码错误");
    header("Location: login.php?mess=$mess");
}
?>