5

I'm working on a website and the index page checks if the user is logged in or not with this piece of code:

if (!$_SESSION['login'] && $_SESSION['login'] == "") {
include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");
} elseif ($_SESSION['login'] == 1) {
include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
}

But I want it to look cleaner, then I started wondering if was possible to achieve something like this with a function:

checklogin($_SESSION['login']);

I don't have much experience with functions, so i'm sorry if my question looks stupid, so thanks in advance.

1
  • Don't worry, both snippets you posted are perfectly valid. It's up to you how to structure your code and where to put the logic. But it must be somewhere. So if you just want that function (as in the second example), the logic from the previous example will have to be inside that function. It can't just go away. You should re-use an existing authentication framework whenever possible, because, really, it's complex. For example, take a look at github.com/delight-im/PHP-Auth which is both framework-agnostic and database-agnostic. Commented Oct 21, 2016 at 21:30

5 Answers 5

6

Try this

if(check_login()) {
  echo 'You are in!';
} else {
    header('Location: login.php');
    exit;
}

function check_login () {
    if(isset($_SESSION['login'] && $_SESSION['login'] != '') {
       return true;
    } else {
       false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Just use empty:

if ( empty($_SESSION['login']) ) {
    include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");
} else {
    include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
}

Or condense it:

include_once $_SERVER['DOCUMENT_ROOT'].(empty($_SESSION['login']) ? "/login/" : "/main/");

Comments

2

There is what you need:

function userCheck()
{
    return (isSet($_SESSION['login']) && $_SESSION['login']);
}

if(userCheck())
    include_once($_SERVER['DOCUMENT_ROOT'] . "/main/");
else
    include_once($_SERVER['DOCUMENT_ROOT'] . "/login/");

Comments

1

Disregarding the fact of whether or not your approach makes sense, I think this would do what you expect:

function checklogin($login){
      if (!$login && $login == "") {
          include_once($_SERVER['DOCUMENT_ROOT'] . "/path/");
      }
}


// **** call to the function

       checklogin($_SESSION['login']);  

// ****

Comments

0

You can use this function:

function checklogin() {
  return (isset($_SESSION['login'])) ? true : false;
}

then on pages you want to check whether the user is logged in or not, you can:

if(checklogin() === true){
  //here you would put what you want to do if the user is logged in
} else {
  //this would be executed if user isn't logged in
  header('Location: protected.php');
  exit();
  //the above would redirect the user
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.