Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    question about cookies

    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.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You could do something like this:

    PHP Code:
    // 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.

    http://us2.php.net/manual/en/ref.session.php

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you have multiple session_start() on one page?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •