I've been messing arround with this for the past little while and I can't seem to figure out the best way to to about doing this. Essentially, I'm trying to create a three part statement. The first will check is a session variable isset & the user has a rolecd of 2. If not, it will check to see if the session variable isset. If not, then it displays a login message. Any help would be much appreciated and I'm sorry, I just getting use to the isset construct and I'm not as familiar with it. From what I understand; however, session_is_registered is being deprecated in PHP5, so it would be best to use isset.

<?PHP
if (isset($_SESSION['UserID']) and ($_SESSION['RoleCD'] == 2)) {echo "Welcome Back Admin";} 
elseif {echo "Welcome Back <a href=\"profile.php\">".$_SESSION['FName']."</a> | <a href=\"logout.php\">Logout</a>";} 
else {echo "Not Logged In"." | <a href=\"login.php\">Login</a>";}
?>

Dani AI

Generated

— you were on the right track. The two immediate problems in the original snippet were the missing condition on the elseif (that causes a syntax error) and the potential for "undefined index" notices if you try to read $_SESSION['RoleCD'] before confirming the session key exists. Also prefer && over and because and has lower operator precedence and can lead to surprises.

A concise, safer pattern is: start the session, pull session values into local variables (using the null-coalescing operator or isset on older PHP), then check the login status first and the role only afterwards. Sanitize any user-facing strings with htmlspecialchars to avoid XSS.

<?php
session_start();

$userId = $_SESSION['UserID'] ?? null;   // use isset() fallback on PHP < 7
$role   = $_SESSION['RoleCD'] ?? null;
$fname  = $_SESSION['FName'] ?? '';

if ($userId !== null && (int)$role === 2) {
    echo 'Welcome Back Admin';
} elseif ($userId !== null) {
    echo 'Welcome Back <a href="profile.php">' . htmlspecialchars($fname, ENT_QUOTES, 'UTF-8') . '</a> | <a href="logout.php">Logout</a>';
} else {
    echo 'Not Logged In | <a href="login.php">Login</a>';
}
?>

Troubleshooting tips: if you still see notices, verify session_start() runs before output; if role values come as strings, cast before strict compare; and consider regenerating the session ID after login and enforcing a timeout to reduce session fixation risks. Nested if blocks work fine, but the guard-then-compare pattern above is clearer and avoids undefined-index errors.

Wouldn't you know, as soon as I post this, I figured out how to do this with a nested IF Statement as opposed to using ELSEIF. :zzz:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.