2014-07-26 47 views
0

我现在正在编写脚本,我有一个名为ban.php的PHP文件,其想法是禁止IP。我有一个名为Banning的课程,它位于inc/class.Banning.inc.php内。由于有禁止单个IP地址和IP范围的能力,我决定请求$_GET。例如:ban.php?type=single将加载单个IP地址的表单,并且ban.php?type=range将加载IP范围的表​​单。不确定为什么我的班级功能无法正常工作

这是禁止类:

/** 
* BANNING CLASS 
*/ 
class Banning { 
    /** 
    * VARIABLES 
    */ 
    private $_Gtype; 
    private $_G_Result; 

    /** 
    * Constructor, set variables to ban.php $_GET 
    */ 
    public function __construct() { 
     $this->_Gtype = isset($_GET['type']) ? $_GET['type'] : null; 
    } 

    /** 
    * GET THE BANNING TYPE FROM $_GET IN BAN.PHP 
    */ 
    public function getType() { 
     // Get the type of banning form from $_GET 
     if ($this->_Gtype == 'single') { 
      $this->_G_Result == 'single'; 
     } else if ($this->_Gtype == 'range') { 
      $this->_G_Result == 'range'; 
     } 
     // Return the value 
     return $this->_G_Result; 
    } 
} 

然后在我ban.php文件我有以下几点:

require_once('inc/class.Banning.inc.php'); 
define('BAN_IP', true); // define the ban constant 

/** 
* DETERMINE WHICH FORM TO DISPLAY REGARDING $_GET 
* Making use of the getType() function in Banning Class. 
*/ 

$getType = new Banning(); 

if ($getType->getType() == 'single') { 
require_once('html/ban.single.php'); 
} 

?> 
<style type="text/css"> 
    <?php require_once('css/css.css'); ?> 
</style> 

我有错误,在我的httpd.conf文件打开,我上运行此我的Mac(MAMP)。 当我运行页面ban.php?type=single我留下一个空白页面。我做了什么有什么不对吗?

+0

不确定,但不应该'ban.php?type = single'是'ban.php?_Gtype = single'或'ban.php?Gtype = single'? –

+1

@ Fred-ii- Gtype变量被分配给构造函数中的$ _GET ['type']。 –

+0

对不起,我的错。 –

回答

2

您可以用双等号设置值。

if ($this->_Gtype == 'single') { 
    $this->_G_Result == 'single'; 
} else if ($this->_Gtype == 'range') { 
    $this->_G_Result == 'range'; 
} 

应该

if ($this->_Gtype == 'single') { 
    $this->_G_Result = 'single'; 
} else if ($this->_Gtype == 'range') { 
    $this->_G_Result = 'range'; 
} 

顺便说一句,你从来不检查是_Gtype值为null或不是。这可能会导致意外的行为。

+0

+1我刚刚说大声笑 –

+0

*“顺便说一句,你永远不会检查_Gtype的值是否为空。”* - 我相信OP是,在使用三元运算符'isset($ _ GET ['type '])? $ _GET ['type']:null' –

+1

我不敢相信我一直这么做,我一直在我的电脑上呆了几个小时,所以我变得有些sl。。谢谢 :) –

相关问题