Jump to content

Which way is best?


Aureole

Recommended Posts

Note: I realize part of the file is a class but I'm referring mainly to the function so that's why this isn't posted in OOP.

 

I have two ways (that I can think of) to check if a user is logged in...

 

<?php
class member {

    function is_logged_in() {
	if(isset(intval($_SESSION['mem_id']))) {
		if($_SESSION['logged_in'] == 1) {
		    return true;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}
}
?>

 

The above method would mean everytime I wanted to check if a member was logged in I would have to do something like...

 

<?php
if($swr3->member->is_logged_in()) {
    // Do stuff.
}
?>

 

...or...

 

<?php
class member {

    var $output;

    function is_logged_in() {
	if(isset(intval($_SESSION['mem_id']))) {
		if($_SESSION['logged_in'] == 1) {
		    $this->output = 1;
		}
		else {
			$this->output = 0;
		}
	}
	else {
		$this->output = 0;
	}
	return $this->output;
}

}
?>

 

The above method would mean I could do...

 

<?php
$swr3->member->is_logged_in();
$member->output->is_logged_in = $swr3->member->output;
?>

 

Then anytime I wanted to check if a user was logged in I could use:

 

 

<?php
if($member->output->is_logged_in == 1) {
    // Do stuff.
}
?>

 

Which method is best?

Link to comment
Share on other sites

The first method is (IMO) best, but your code code be reduced quite a bit.

 

<?php
class member {

    function is_logged_in() {
      if (isset($_SESSION['mem_id'])) {
         return $_SESSION['logged_in'];
      }
      return false;
    }
}
?>

Link to comment
Share on other sites

I already reduced it to

 

<?php
function is_logged_in() {
    if(isset($_SESSION['mem_id'])) {
        ($_SESSION['logged_in'] == 1) ? return true : return false;
    }
    else {
        return false;
    }
?>

 

Yours is 6 lines mine is 7 so it's not too big of a deal. :D

 

Anyone else got any input on this? I would have figured the second way was best as it's one less call to a function say if I use the first method and I need to check if someone is logged in 10 times that's 10 calls to the function, if I use the second method it's one call to the function.

 

Logic says the second is better, but correct me if I'm wrong.  ;)

Link to comment
Share on other sites

Was 14 lines...

 

Now 7 lines...

 

I changed it, see.

 

<?php
function is_logged_in() {
    if(isset($_SESSION['mem_id'])) {
        ($_SESSION['logged_in'] == 1) ? return true : return false;
    }
    else {
        return false;
    }
?>

 

EDIT: Original... yes, but I changed it. ???

Link to comment
Share on other sites

just a note, it isn't best practice to issue a return inside an conditional. it keeps the code cleaner and more maintainable.

 

<?php
function is_logged_in() {
    $LoggedIn = false ;

    if (isset ($_SESSION['mem_id'] ) && isset($_SESSION['logged_in']) &&  $_SESSION['logged_in'] == 1) :
        $LoggedIn = true ;
    else :
        $LoggedIn = true ;
    endif ;
    
    return $LoggedIn ;
?>

 

good luck

Link to comment
Share on other sites

freakstyle: I cant imagine creating another variable, then having the extra line of code to return the variable, would be cleaner / more maintainable than just using 'return true/false' as it happens? it's pretty self explanitory what's happening...

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.