0

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(); ?>

2
  • Too quick for me :) 3 answers while typing my response. Commented Jun 15, 2009 at 4:01
  • Christ, I cannot believe I missed an extra equals sign lol. Thanks ;) Commented Jun 15, 2009 at 4:04

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

excellent point about adding die() to prevent continued processing of the page
1

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 =]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.