I want to know if I can make a PHP function like function john(), without having any parameters. Do PHP functions always have to have a parameter like john($action)?
My script below won’t work because of my empty action. Do I have to pass a NULL value as a parameter?
funcs.php
function is_customer_logged_in() {
if($_COOKIE['HA_CUST_LOGIN'] == 1 && $_SESSION["loggedIn"]=="1"){
return true;
}else{
return false;
}
}
index.php
include("funcs.php");
if(is_customer_logged_in()) {
echo 'logged in';
} else {
echo 'not signed in';
}
login-form.php
/* pseudo code, if form details
match mySQL do this */
$_SESSION["loggedIn"]="1";
$_SESSION["userEmail"]=$username; //form variable
$_SESSION["userID"]=$username; //form variable
setcookie("HA_CUST_LOGIN", 1); // set a cookie and a session from a php salt db check
echo '<script type="text/javascript">window.location="index.php"</script>';
/* else go back to login-form.php */
Or do I have to return a string rather than a boolean?
You are trying to access a SESSION variable in the $_SESSION array. This variable will only be available on two conditions:
session_start()before trying to access the $_SESSION array() (and any output has been sent to the browser).Even though you are using a COOKIE, you still need to call
session_start()for your test to return evaluate true, because you have a logical AND in your IF statement:If session_start() has not been called, and the parameter $_SESSION[“loggedIn”] has not been set, this IF statement will evaluate false. You should also be getting notices/warnings in your PHP logs (and in your browser if you are on a development machine) – if not, then you need to reconfigure your error reporting on you development machine.