View Full Version : question about cookies

01-25-2007, 04:18 PM
Hello, Ok I have this navigation system that has sublinks in it and I was wondering how I would go about using cookies to track whether or not a user has the menu open or closed and i'd like to have it track that cookie across pages. So if they close the submenu it will stay closed no matter what page they go to until they open it again and vice versa.
Any help would be appreciated.

01-25-2007, 04:27 PM
You could do something like this:

// URL: file.php?action=updatemenu&state=closed

if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];

// first check to see if we are updating the state
// else see if there is a cookie set with the last state it was in
// else set the default
if ($action == 'updatemenu') {
setcookie('menustate', $_REQUEST['state']); // state would either be open or closed
$menuState = $_REQUEST['state'];
} else if (isset($_COOKIE['menustate'])) {
$menuState = $_COOKIE['menustate'];
} else {
$menuState = 'open';

if ($menuState == 'open') {
// echo menu open
} else {
// echo menu closed

You could also use sessions instead of cookies, which might be easier to implement. And usually more reliable too, as sessions work even when the user has cookies turned off.


01-25-2007, 04:31 PM
can you have multiple session_start() on one page?