SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use sessions to maintain menu persistence

    Hello,

    I have created a typical expanding/collapsing menu using HTML/CSS/Javascript. After a user clicks on the menu I would like the menu to persist, stay open, when the new page is loaded. I believe the correct way to do this is to use php sessions. I understand how sessions work, but do not know what variables I would need to store. The menu is four levels deep at places.

    Here is a small portion of the menu.

    HTML Code:
    <li class="treenode">
    	<a href="#">Mayor</a>
    		<ul>
    			<li><a href="#">Contact</a></li>
    			<li><a href="#">Budget</a></li>
    			<li><a href="#">Biography</a></li>
    			<li><a href="#">Message</a></li>
    			<li><a href="#">Newsletters</a></li>
    			<li><a href="#">Quickfacts</a></li>
    			<li><a href="#">City Council</a></li>
    		</ul>
    	</li>
    I am having a hard time wrapping my head around what actually will need to be stored in a session to open the menu to a specific location.

    Thank you for your help.

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I think JavaScript would be a better choice for this, as I take it that your menu expands/collapses without sending anything back to the server (where PHP lives). I suggest taking this question over to the JavaScript forum and I'm sure someone there can hook you up.
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I agree

    I agree it can be done with Javascript but I do not want to use session cookies as some users turn them off. The menu degrades to a list when Javascript is turned off. Using PHP sessions I believe will offer the user a better experience.

    Thank you,

    Max

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP sessions use session cookies, unless you allow them in URL which is strongly recommended against. So you're stuck either relying on cookies (hardly anyone these days turns cookies off), or opening up egregious security holes in your application.
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ive never done this before but assuming javascript is what does the menu expanding/collapsing, you will need to store enough data to let javascript expand the proper nodes. you could create some type of node path i suppose.

    thinking about it, it seems complex to make this work correctly if you may have multiple/different menus and want it to work correctly if the user has multiple browser windows open. i suppose a little serverside assistance could help, for example by outputting a unique id for the menu on each page view, and using that id to select which of the stored node paths to use.

    retrieving the node path could be done either by storing them in a cookie(however beware of data limits with cookies), or possibly storing them serverside, and polling the server with ajax.

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hmm...

    Ok, I have quickly become confused. I will explore doing this with Javascript but at the same time this seems like a perfectly valid use of sessions with PHP.

    Clamcrusher
    outputting a unique id for the menu on each page view, and using that id to select which of the stored node paths to use
    What clamcrusher mentions is what I was thinking about, but still can not get my head around. I will ponder and play with it some more.

    Thank you

  7. #7
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I have quickly become confused. I will explore doing this with Javascript but at the same time this seems like a perfectly valid use of sessions with PHP.
    If you're using SESSIONs, you're using cookies. They are correct -- this is what Javascript is made for. If you use Sessions for this you're going to have to round trip back to the server every time the user expands or collapses a menu, which would be expensive and unneccessary.

    The user closes the menu -- Javascript sets a cookie. The user expands the menu, Javascript sets a cookie. This is what Javascript was made for.


Bookmarks

Posting Permissions

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