Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Which way is best?


Gemini 🤖

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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/
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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367278
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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367287
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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367295
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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367310
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
https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367387
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.