jon.r
jon.r

Reputation: 1058

PHP class issue/Session issue

I'm trying to create an object and store it in a session variable so I can access it on different pages.

I'm having an issue where properties in my object are getting over written inexplicably. Specifically the getMenu method seems to be displaying the wrong property. Cannot figure out why, maybe someone here at a glance will know.

Here is my user.class.php:

    <?php

/**
 * Created by PhpStorm.
 * User:
 * Date: 6/26/14
 * Time: 2:55 PM
 */

class user{
    private $company;
    private $userName;
    private $menuType;
    private $viewState;
    private $gridType;

    public function setGrid($gridType){
        $this->gridType = $gridType;

    }
    public function getGrid(){
        if($this->gridType='salesGrid'){
             include 'gridView.php';
        }elseif($this->gridType='truckingGrid'){
             include 'lActiveLoadView.php';
        }
    }

    public function setCompany($company){
        $this->company = $company;

    }
    public function getCompany(){
        return $this->company;
    }

    public function setUserName($userName){
        $this->userName = $userName;

    }
    public function getUserName(){
        return $this->userName;
    }

    public function setMenu($menuType){
        $this->menuType = $menuType;
    }

    public function getMenu(){
        if($this->menuType='salesMenu'){
            $x = <<< 'Menu'
     <button id='carrierEditor'  class='alignRight' onclick="location.href='gridManager.php?action=alert'">Alerts</button>
     <button id='carrierEditor'  class='alignRight' onclick="location.href='gridManager.php?action=carrier'">Carriers</button>
     <button id='vendorEditor'  class='alignRight' onclick="location.href='gridManager.php?action=vendor'">Vendors</button>
     <button id='productEditor'  class='alignRight' onclick="location.href='gridManager.php?action=product'">Products</button>
     <button id='customerEditor'  class='alignRight' onclick="location.href='gridManager.php?action=customer'">Customer</button>
     <button id='home'  class='alignRight' onclick="location.href='index.php'">Home</button>
Menu;

            echo $x;

        }
        elseif($this->menuType='truckingMenu'){
            $x = <<< 'Menu'
            <button id='generateNewLoad' onclick=\"location.href='index.php?viewState=NewLoad'\">Generate New Load</button>
            <button id='openloads'  onclick=\"location.href='index.php?viewState=All'\">Open Loads</button>
            <button id='viewToday'  class='alignLeft' onclick=\"location.href='index.php?viewState=Today'\">Today</button>
            <button id='viewTomorrow'  class='alignLeft' onclick=\"location.href='index.php?viewState=Tomorrow'\">Tomorrow</button>
            <button id='viewWeek'  class='alignLeft 'onclick=\"location.href='index.php?viewState=Week'\">Week</button>
            <button id='trackingloads'  onclick=\"location.href='index.php?viewState=Tracking'\">Tracking Loads</button>
            <button id='closedloads'  onclick=\"location.href='index.php?viewState=Completed'\">Closed Loads</button>
            <button id='deliveryRequest'  class='alignRight'>Delivery Request</button>
            <button id='bolGenerator'  class='alignRight'>Manage BOLs</button>
            <button id='manageContacts'  class='alignRight' onclick=\"location.href='gridManager.php?action=contacts'\">Manage Contacts</button>
            <button id='btnEmailBlast'  class='alignRight' >Email Blast</button>
Menu;
             echo $x;
        }
    }
    public function setViewState($viewState){
        $this->viewState = $viewState;
    }
    public function getViewState(){
        return $this->$viewState;
    }
}

my authentication page:

if ($adldap->authenticate($user, $pass)){
            session_start();
            $_SESSION['loggedInUser'] = $user;

            //Build an array after calling in the inGroup method in /src/adLDAP. In this array we store the KEY as the group name and the VALUE as a boolean value.
            $loginType = array("Administration" => $adldap->user()->inGroup($user, "NY-IT"),
                "Sales" => $adldap->user()->inGroup($user, "ACL-SALES"),
                "Trucking" => $adldap->user()->inGroup($user, "ACL-SALES_ABRIDGED"),
                "PHSales" => $adldap->user()->inGroup($user, "ACL-PH-SALES"),
                "HBroccoli" => $adldap->user()->inGroup($user, "ACL-SALES_BROCCOLI"));
            //Loop through array, if a TRUE value comes up, assign it to the session and redirect to the correct page

            //$userProps = new user();

            //   $company = $_SESSION['userObject']->getCompany();


          //  if (!$_SESSION['userObject'] instanceof User)
          //      $_SESSION['userObject'] = new user();
            $userSession = new user();

            foreach ($loginType as $val) {
                if ($loginType['Administration'] == true) {
                    $userSession->setCompany('Administrators');
                    $userSession->setMenu('salesMenu');
                    $userSession->setUserName($user);
                    $userSession->setGrid('salesGrid');
                    $userSession->setViewState('All');

                    $_SESSION['userObject']=$userSession;

                    $_SESSION['company'] = "Administrators";


                    header('Location: index.php?alerts=null');
                } elseif ($loginType['Sales'] == true) {

                    $userSession->setCompany('Sales');
                    $userSession->setMenu('salesMenu');
                    $userSession->setUserName($user);
                    $userSession->setViewState('All');
                    $userSession->setGrid('salesGrid');

                    $_SESSION['userObject']=$userSession;

                    $_SESSION['company'] = "Sales";
                    header('Location: index.php?alerts=null');

                } elseif ($loginType['Trucking'] == true) {

                    $userSession->setCompany('Trucking');
                    $userSession->setMenu('truckingMenu');
                    $userSession->setUserName($user);
                    $userSession->setViewState('All');
                    $userSession->setGrid('truckingGrid');

                    $_SESSION['userObject']=$userSession;


                    $_SESSION['company'] = "Trucking";
                    header('Location: index.php?alerts=null');
                } elseif ($loginType['PHSales'] == true) {
                    $_SESSION['company'] = "PH Sales";
                    header('Location: cManager.php');
                } elseif ($loginType['HBroccoli'] == true) {
                    $_SESSION['company'] = "HBroccoli";
                    header('Location: index.php?alerts=null');
                } else {
                    header('Location: login.php?alerts=na');
                }
            }
        }
        else{
        header('Location: login.php?alerts=na'); // non authenticated used
        }

Then here is some case examples: A var dump of the object on load will expose this:

    object(user)[1]
  private 'company' => string 'Trucking' (length=14)
  private 'userName' => string 'gpigb' (length=5)
  private 'menuType' => string 'truckingMenu' (length=12)
  private 'viewState' => string 'All' (length=3)
  private 'gridType' => string 'truckingGrid' (length=12)

If I call any of the methods in the class stored in the session variable the properties of the methods get written over with the wrong data for some reason.

$x = $_SESSION['userObject'];
    $x->getMenu();

vardump of the object shows:

object(user)[1]
  private 'company' => string 'Trucking' (length=14)
  private 'userName' => string 'gpigb' (length=5)
  private 'menuType' => string 'salesMenu' (length=9)
  private 'viewState' => string 'All' (length=3)
  private 'gridType' => string 'truckingGrid' (length=12)

See how menu type now shows 'salesMenu'? I never called the setMenu method to change it, yet it has. What gives?

please help

Upvotes: 0

Views: 69

Answers (2)

gpupo
gpupo

Reputation: 982

Your if condition is wrong!

Correct:

//..

if($this->menuType=='salesMenu'){

//..

elseif($this->menuType=='truckingMenu'){

Upvotes: 0

AeroX
AeroX

Reputation: 3443

In your user.class.php file you are assigning (=) to the menuType class/object variable rather then comparing (==) to it.

if($this->menuType='salesMenu'){
    ...
}
elseif($this->menuType='truckingMenu'){
    ....

Should be:

if($this->menuType == 'salesMenu'){
    ...
}
elseif($this->menuType == 'truckingMenu'){
    ....

You're also overwritting the gridType variable in your getGrid() function.

Upvotes: 1

Related Questions