user117399
user117399

Reputation:

PHP Class variable question

I have the following class which handles my user logged in / logged out (I only included what's relevant here). I want to redirect users who are logged in who visit login.php to there account page. I do this using....

    $User = new User();
if ($User->loggedin = 'true') header('Location:MyAccountNEW.php');

The issue is this redirects to myaccountnew.php weather I switch it to true or false.. (although it does not when the condition is (2>3). Aslo when I echo $User-loggedin, nothing comes up. I'm kinda stumped...

Heres the class

Class User {

public $loggedin = false;
public $username = "";
public $ShopperID = "";

function __construct() {
    $this->CheckLogin();
}

function CheckLogin() {
    if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
            $this->loggedin = true;
            $this->username = $_SESSION['Username'];
    }
    else {
        $this->loggedin = false;
    }
}

heres what logout.php looks like

<?php include ("base.php");
  include("userclass.php");

  $User = new User();
  $User->loggedin = false;

$_SESSION = array(); session_destroy(); ?>

Upvotes: 0

Views: 116

Answers (2)

Galen
Galen

Reputation: 30170

replace

if ($User->loggedin = 'true')

with

if ($User->loggedin == 'true')

because

if ($User->loggedin = 'true')

is an assignment and will always return true

probably just a typ0 of yours =]

Upvotes: 1

nickf
nickf

Reputation: 546473

You're using a single equals (=) instead of two (==)

Also, I would strongly recommend adding this:

if ($User->loggedIn == 'true') {
    header('location: somewhereelse.php');
    die(); // <-- important!!
}

Also, since that property is a boolean, you should be comparing against the actual bool value true instead of the string "true".

if ($User->loggedIn == true)

// or even shorter:

if ($User->loggedIn)

this is because:

true == "true"
true == "foo"
true == "false"

Any string value apart from an empty string or the string "0" is considered true.

Upvotes: 6

Related Questions