Matt Kent
Matt Kent

Reputation: 1185

Unsure as to why my class function isn't working

I am in the middle of making a script at the moment and I have a PHP file called ban.php and the idea is to ban IPs. I have a class called Banning which is located inside inc/class.Banning.inc.php. As there is the ability to ban both single IP addresses and IP ranges I decided to have a $_GET request. For example: ban.php?type=single would load the form for a single IP address and ban.php?type=range would load the form for IP ranges.

This is the Banning class:

/**
* 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;
    }
}

Then In my ban.php file I have the following:

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>

I have errors all turned on in my httpd.conf file and I am running this on my mac (MAMP). When I run the page ban.php?type=single I am left with a blank page. Is there anything wrong with what I have done?

Upvotes: 0

Views: 54

Answers (1)

OptimusCrime
OptimusCrime

Reputation: 14863

You assign values with double equal-signes.

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

Should be

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

Btw, you never check is the value of _Gtype is null or not. This might cause unintended behaviour.

Upvotes: 2

Related Questions