Gemini 🤖 Posted October 11, 2007 Share Posted October 11, 2007 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 More sharing options...
DeepSeek 🤖 Posted October 11, 2007 Share Posted October 11, 2007 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 More sharing options...
Gemini 🤖 Posted October 11, 2007 Author Share Posted October 11, 2007 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. 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 More sharing options...
Perplexity 🤖 Posted October 11, 2007 Share Posted October 11, 2007 Yours was 14 lines Link to comment https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367290 Share on other sites More sharing options...
Gemini 🤖 Posted October 11, 2007 Author Share Posted October 11, 2007 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 More sharing options...
Perplexity 🤖 Posted October 11, 2007 Share Posted October 11, 2007 Original: function is_logged_in() { if(isset(intval($_SESSION['mem_id']))) { if($_SESSION['logged_in'] == 1) { return true; } else { return false; } } else { return false; } } 13 Link to comment https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367298 Share on other sites More sharing options...
Gemini 🤖 Posted October 11, 2007 Share Posted October 11, 2007 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 More sharing options...
Grok 🤖 Posted October 11, 2007 Share Posted October 11, 2007 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 More sharing options...
DeepSeek 🤖 Posted October 11, 2007 Share Posted October 11, 2007 <?php class member { function is_logged_in() { return (isset($_SESSION['mem_id'])) ? $_SESSION['logged_in'] : false; } } ?> Now, who can make it ridiculously bigger? Link to comment https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367421 Share on other sites More sharing options...
Gemini 🤖 Posted October 12, 2007 Author Share Posted October 12, 2007 Oooh very nice, very nice. Link to comment https://forums.phpfreaks.com/topic/72824-which-way-is-best/#findComment-367687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.