PHP Sessions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oceano
    New Member
    • Sep 2007
    • 2

    #1

    PHP Sessions

    I'm simply getting my feet wet in php so...

    I need to set a session id in my 3 main templates and an if statement to check for the id of the page

    OKAY - I've got 3 XHTML webpage templates that have the same top horizontal menu that has 3 tab links to different areas of the site. These 3 templates also have 3 different side menus called by an include in each one. The side menus have links to call via ajax the contents of a #div from a wiki into a #div in a 4th special template.

    So this makes 4 templates: index1.php, index2.php, index3.php and wiki.php and 3 side menus called by php includes: menu1.php, menu2.php, menu3.php.

    I want the special template to check and see which of the 3 main templates the user came from and therefore to insert the same sidemenu into the special template that inserts the contents via ajax.

    Hope that was clear.

    I see how I need to set the sessions id at the top of each index.php template

    And then an if statement at the top of the wiki.php template

    But I'm really quite lame beyond this and time is short....I'm more a designer than php programmer you see.

    Any help on this out there tonight? Thanks /mark
  • karlectomy
    New Member
    • Sep 2007
    • 64

    #2
    Heya,

    I think i see what you're trying to accomplish. You might be able to set a session variable to the template you are coming from when you enter each template. so if you are going to include index1.php:

    [PHP]
    <?
    //Session Variable 'template' is a string like "menu1" or "menu2"
    session_start() ;
    require_once("i ndex1.php");
    if(session_is_r egistered('temp late'))
    {
    $reqString = $_SESSION['template'].".php";
    require_once($r eqString);
    }
    else
    {
    session_registe r('template');
    $template = "menu1";
    }
    [/PHP]

    I have no Idea if that really helps but if you store the file name you want to include into the session variable "template" everytime you switch out menus, it will keep track of which one is current.

    Hope this helps

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi Mark. Welcome to The Scripts!

      I'm not sure I fully understand what you are trying to do.
      As I understood it... You have 3 PHP pages, each of them having it's own unique menu. Each of these 3 pages will have links to a fourth page, which is supposed to have the same menu as the page that linked to it?

      Even if my understanding of your problem is way off this may be helpful.

      First, each of the three main templates should have this at the very top:
      [code=php]
      <?php
      # Create a session and set a variable so the fourth page knows
      # where we were.
      session_start() ;
      $_SESSION['SentFrom'] = "page1"; # Or page2 or page3

      # Do whatever else you may want to do, like say include your menu.
      (@include("menu 1.php")) or die("Menu failed to load!");

      # Note, the @ is there to silence the PHP warning. I do this because
      # the die() function will print a message if and error occurs and we
      # don't want to see both of them.
      ?>
      [/code]

      The fourth page, would have to check which page the client came from and load the appropriate menu.
      [code=php]
      <?php
      # Check which page the user came from and load the menu
      switch(@$_SESSI ON['SentFrom'])
      {
      case "page1":
      (@include("menu 1.php")) or die("Menu failed to load!");
      break;
      case "page2":
      (@include("menu 2.php")) or die("Menu failed to load!");
      break;
      case "page3":
      (@include("menu 3.php")) or die("Menu failed to load!");
      break;
      default
      die("You came from nowhere!");
      }
      ?>
      [/code]

      If this doesn't help please try explaining the problem again. You could include some examples of your pages to help us understand.

      Comment

      • karlectomy
        New Member
        • Sep 2007
        • 64

        #4
        I concur with Atli, I probably shouldn't have spent time working on that script since I only have a vague concept of what you're trying to acheive.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by karlectomy
          Heya,

          I think i see what you're trying to accomplish. You might be able to set a session variable to the template you are coming from when you enter each template. so if you are going to include index1.php:

          [PHP]
          <?
          //Session Variable 'template' is a string like "menu1" or "menu2"
          session_start() ;
          require_once("i ndex1.php");
          if(session_is_r egistered('temp late'))
          {
          $reqString = $_SESSION['template'].".php";
          require_once($r eqString);
          }
          else
          {
          session_registe r('template');
          $template = "menu1";
          }
          [/PHP]

          I have no Idea if that really helps but if you store the file name you want to include into the session variable "template" everytime you switch out menus, it will keep track of which one is current.

          Hope this helps
          Hi.

          You code will of course work. I would recommend, however, avoiding the session_is_regi stered() and session_registe r() functions if you are using the $_SESSION superglobal. They are relics of PHP 4.0.

          As stated in the PHP manual
          Originally posted by PHP Manual
          With $_SESSION, there is no need to use the session_registe r(), session_unregis ter(), session_is_regi stered() functions. Session variables are accessible like any other variables.

          Comment

          • karlectomy
            New Member
            • Sep 2007
            • 64

            #6
            Originally posted by Atli
            Hi.

            You code will of course work. I would recommend, however, avoiding the session_is_regi stered() and session_registe r() functions if you are using the $_SESSION superglobal. They are relics of PHP 4.0.

            As stated in the PHP manual
            Atli, WOuld this work?
            [PHP]
            <?
            session_start() ;
            if(isset($_SESS ION['template'])
            {
            //blah blah blah
            }
            else
            {
            require_once("m enu1.php");
            $_SESSION['template'] = "menu1";
            }
            [/PHP]
            Damn RElics, I feel like INdiana Jones... Lost arc my ass... Probably at www.PHP.net with the rest of the treasure.
            Last edited by karlectomy; Sep 25 '07, 02:41 AM. Reason: Probably should include some relavent info

            Comment

            • oceano
              New Member
              • Sep 2007
              • 2

              #7
              Wow, thank you all. I'll run some tests on these ideas and get right back to you.

              So clarify however:

              I have 3 templates(for three domains of the website) that each have a php include for a different side column menu. index1.php, index2.php, index3.php

              In the side column menu there are (some) links that ajax in the contents of a div in our wiki into a wiki.php template.(pleas e don't ask why this was done...)

              So the task at hand is to simply track which of the three domains(index.p hp templates) the user has come from in the wiki.php so the correct side navigation is included in the final display of the wiki.php.

              Based on my dangerous level of newbie knowledge only I'd say you've perhaps nailed it with the above scripts.

              I'll let you know tonight /mark

              Comment

              Working...