Seemingly complicated way of displaying page views I have this code

//TRIGGER MULTIPLE VIEWS, WALL AND FORUM
if(isset($_COOKIE["projects-sorting"])) {
    $_SESSION['projectsDisplayMode'] = $_COOKIE["projects-sorting"];
}
if(isSet($_GET['mode'])) {
    $_SESSION['projectsDisplayMode'] = $_GET['mode'];
}
if(!isSet($_SESSION['projectsDisplayMode'])) {
    $_SESSION['projectsDisplayMode'] = "wall"; //default is wall mode
}
$mode = $_SESSION['projectsDisplayMode'];
setcookie("projects-sorting", $mode, time() + (86400 * 30), "/"); // 86400 = 1 day


Which to me looks overly complicated, but it does work.

What it does:


Checks  if a specific cookie exists, if it does, set session variable projectsDisplayMode to whatever that cookie's content is.
Check if _GET variable mode exists, if it does, set projectsDisplayMode to whatever that variable is (Would overwrite the last variable set if both exist)
Last check if the session variable is set at all, if it's not set it to "wall", as that is the default.
Now set $mode (A specific page variable) to be processed a bit more to show the page properly.


I'm not concerned with the afterwards processing, I just want to see if there is a better way that I can do the first bit that may be faster or more standard, or more pleasant to look at.
First I think that you need to reconcile your use of session vs. cookie.  In your answer you explain technically what is happening in your code, but not what the overall user experience requirement is.  You need to match your usage of cookies vs. sessions vs. HTML local storage (though not used here), which are all means to convey application state to what the actual user experience needs of the application are.

Does the cookie represent a default user preference?  If so, then should you be overriding it when the user passes parameters or otherwise changes mode setting in a session only context?

Why use session at all if you are always going to set a new "mode" cookie which overrides whatever is in session and there is not a session-specific override context?  Most likely you are propagating session ID by cookie anyway, so if user deletes cookies, you are going to lose state anyway.

If for example you remove session from the equation altogether, you may get something like:

$mode = 'wall';
if(!empty($_COOKIE['projects-sorting'])) {
    $mode = $_COOKIE['projects-sorting'];
}
if(!empty($_GET['mode'])) {
    $mode = $_GET['mode'];
}
if($mode !== $_COOKIE['projects-sorting']) {
    setcookie("projects-sorting", $mode, time() + (86400 * 30), "/");
}




You are doing nothing in code shown here to validate/sanitize the inputs. Both $_COOKIE and $_GET are use input and should be validated/sanitized accordingly before passing these values further into the application (or especially placing back into user cookie).

Maybe something as simple as validateMode() function.

function validateMode($array, $key) {
    if(empty($array[$key]) || !is_string($array[$key])) {
        return false;
    }
    switch($array[$key]) {
        case 'wall':
        case 'forum':
            return true;
        default:
            return false;
    }
}


And usage with my suggestion like:

$mode = 'wall';
if(validateMode($_COOKIE, 'projects-sorting')) {
    $mode = $_COOKIE['projects-sorting'];
}
if(validateMode($_GET, 'mode')) {
    $mode = $_GET['mode'];
}
if($mode !== $_COOKIE['projects-sorting']) {
    setcookie("projects-sorting", $mode, time() + (86400 * 30), "/");
}