Reputation: 906
I have a functions.php
file where I have a check_login
function, this function is just supposed to check if the session variables have been set on every page load,then it should just do nothing, but at this moment it looks like it does not execute the function.
Am I doing something wrong?
I have added code to allow me to see which side of the if statement executes, but it does not echo either values.
Index page:
require_once 'functions/functions.php';
check_login;
functions.php:
<?
function check_login(){
session_start();
if (isset($_SESSION['user_id']) and ($_SESSION['user_id'] != '')and ($_SESSION['user_id'] != NULL)){
echo '<p>Logged in</p>';
}else{
echo '<p>Not Logged in</p>';
header("Location: login.php");
}
}
?>
Upvotes: 1
Views: 154
Reputation: 6661
use ()
before call function :-
require_once 'functions/functions.php';
check_login();
or session_start
must be on first line :-
<?php
session_start();
function check_login(){
if (isset($_SESSION['user_id']) and ($_SESSION['user_id'] != '') and ($_SESSION['user_id'] != NULL)) {
echo '<p>Logged in</p>';
} else {
echo '<p>Not Logged in</p>';
header("Location: login.php");
}
}
?>
Upvotes: 1
Reputation: 11987
To call a function you should use parenthesis ()
. And while your project is in development, please add this line to track the errors. error_reporting(E_ALL);
this should be
check_login();
instead of
check_login;
Thus,
require_once 'functions/functions.php';
check_login();
functions.php:
<?
function check_login(){
session_start();
if (isset($_SESSION['user_id']) and ($_SESSION['user_id'] != '')and ($_SESSION['user_id'] != NULL)){
echo '<p>Logged in</p>';
}else{
echo '<p>Not Logged in</p>';
header("Location: login.php");
}
}
?>
For more reference see this
Upvotes: 3
Reputation: 344
call your function with round brackets
require_once 'functions/functions.php';
check_login();
Upvotes: 4
Reputation: 8340
If you call a function, you should use ()
after the function name. Try this:
check_login();
instead of:
check_login;
Upvotes: 1
Reputation: 1362
check_login
should be check_login()
.
And also
if (isset($_SESSION['user_id']) and ($_SESSION['user_id'] != '')and ($_SESSION['user_id'] != NULL))
Can be condensed into this
if (!empty($_SESSION['user_id']))
Upvotes: 2