tags:

views:

52

answers:

2

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

A: 

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 type of yours =]

Galen
+6  A: 

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.

nickf
excellent point about adding die() to prevent continued processing of the page
Jonathan Fingland