Session_ID()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Annie

    Session_ID()

    I have a script to check if a session exists, if not then I log the user's info from $_Server
    variables into a database (ip address, time, etc.).

    The problem is that the test:

    if(!session_id( )) // if there is no session started
    {
    session_cache_l imiter('nocache ');
    session_cache_e xpire (20);
    session_start() ;

    include_once ('../DB_functions.ph p');

    ....the the other stuff, like logging goes here...


    doesn't seem to work...the test always produces true...is there something I am missing?

    Thanks,

    Annie
  • Guest's Avatar

    #2
    Re: Session_ID()

    > The problem is that the test:[color=blue]
    >
    > if(!session_id( )) // if there is no session started
    > {
    > session_cache_l imiter('nocache ');
    > session_cache_e xpire (20);
    > session_start() ;[/color]
    [color=blue]
    > doesn't seem to work...the test always produces true...is there something[/color]
    I am missing?[color=blue]
    >[/color]

    Try :
    print ini_get('sessio n.auto_start');

    If it is set, they your session is already started and
    you will always have a session_id. You would then need
    to turn it off in php.ini or .htaccess


    _______________ _______________ ______
    Wil Moore III, MCP | Integrations Specialist | Senior Consultant
    Business | Personal


    Comment

    • Annie

      #3
      Re: Session_ID()

      laidbak69@hotma il.com wrote:
      [color=blue][color=green]
      >>The problem is that the test:
      >>
      >>if(!session_i d()) // if there is no session started
      >>{
      >> session_cache_l imiter('nocache ');
      >> session_cache_e xpire (20);
      >> session_start() ;[/color]
      >
      >[color=green]
      >>doesn't seem to work...the test always produces true...is there something[/color]
      >
      > I am missing?
      >
      >
      > Try :
      > print ini_get('sessio n.auto_start');
      >
      > If it is set, they your session is already started and
      > you will always have a session_id. You would then need
      > to turn it off in php.ini or .htaccess
      >
      >
      > _______________ _______________ ______
      > Wil Moore III, MCP | Integrations Specialist | Senior Consultant
      > Business | Personal
      >
      >[/color]
      Thanks for you advice,

      ini_get('sessio n.auto_start') returns 0, so I assume that the auto start is off. The problem is
      that the If statement runs as if there is never a session.

      When I print the value of session_id () it produces a value (a session id), but !session_id() always
      is true?

      Annie

      Comment

      • Guest's Avatar

        #4
        Re: Session_ID()

        > >>The problem is that the test:[color=blue][color=green][color=darkred]
        > >>
        > >>if(!session_i d()) // if there is no session started
        > >>{
        > >> session_cache_l imiter('nocache ');
        > >> session_cache_e xpire (20);
        > >> session_start() ;[/color]
        > >
        > >[color=darkred]
        > >>doesn't seem to work...the test always produces true...is there[/color][/color][/color]
        something

        What I have found is interesting. You can't evaluate 'session_id()' before
        calling
        session_start() because session_start() is what restores the value that
        session_id()
        would return.

        I know the manual referrs to using session_id() before session_start() ,
        however,
        it is not meant to say that you can retrieve that value... it means it can
        be SET
        only before session_start() is called.

        _______________ _______________ ______
        Wil Moore III, MCP | Integrations Specialist | Senior Consultant
        Business | Personal


        Comment

        • Chung Leong

          #5
          Re: Session_ID()

          I think you've misinterpretted the purpose of session_start() . While the
          function does create a new session when there isn't one, its primary
          function is to initialize the session mechanism (for instance, populate the
          $_SESSION array). It's best to interpret the "start" in session_start as
          "from this point forward, the script will start making use of session
          variables. The function that performs the opposite is session_write_c lose().

          I suggest using a session variable to note whether the user info has been
          saved or not, as follows:

          <?php

          session_start() ;

          if(!@$_SESSION['user info saved']) {
          ... save info in database ...
          $_SESSION['user info saved'] = true;
          }

          Uzytkownik "Annie" <AnnB@newsgroup .net> napisal w wiadomosci
          news:cCS1c.4554 8$PR3.929733@at tbi_s03...[color=blue]
          > I have a script to check if a session exists, if not then I log the user's[/color]
          info from $_Server[color=blue]
          > variables into a database (ip address, time, etc.).
          >
          > The problem is that the test:
          >
          > if(!session_id( )) // if there is no session started
          > {
          > session_cache_l imiter('nocache ');
          > session_cache_e xpire (20);
          > session_start() ;
          >
          > include_once ('../DB_functions.ph p');
          >
          > ...the the other stuff, like logging goes here...
          >
          >
          > doesn't seem to work...the test always produces true...is there something[/color]
          I am missing?[color=blue]
          >
          > Thanks,
          >
          > Annie[/color]


          Comment

          • Annie

            #6
            Re: Session_ID()

            Chung Leong wrote:
            [color=blue]
            > I think you've misinterpretted the purpose of session_start() . While the
            > function does create a new session when there isn't one, its primary
            > function is to initialize the session mechanism (for instance, populate the
            > $_SESSION array). It's best to interpret the "start" in session_start as
            > "from this point forward, the script will start making use of session
            > variables. The function that performs the opposite is session_write_c lose().
            >
            > I suggest using a session variable to note whether the user info has been
            > saved or not, as follows:
            >
            > <?php
            >
            > session_start() ;
            >
            > if(!@$_SESSION['user info saved']) {
            > ... save info in database ...
            > $_SESSION['user info saved'] = true;
            > }
            >
            > Uzytkownik "Annie" <AnnB@newsgroup .net> napisal w wiadomosci
            > news:cCS1c.4554 8$PR3.929733@at tbi_s03...
            >[color=green]
            >>I have a script to check if a session exists, if not then I log the user's[/color]
            >
            > info from $_Server
            >[color=green]
            >>variables into a database (ip address, time, etc.).
            >>
            >>The problem is that the test:
            >>
            >>if(!session_i d()) // if there is no session started
            >>{
            >> session_cache_l imiter('nocache ');
            >> session_cache_e xpire (20);
            >> session_start() ;
            >>
            >> include_once ('../DB_functions.ph p');
            >>
            >>...the the other stuff, like logging goes here...
            >>
            >>
            >>doesn't seem to work...the test always produces true...is there something[/color]
            >
            > I am missing?
            >[color=green]
            >>Thanks,
            >>
            >>Annie[/color]
            >
            >
            >[/color]
            Thanks...yeah the manual does not mention that you need to first run session_start before you check
            session_id, and that does not match the way they suggest using it. I will use a session variable to
            track the first entry into a session.

            Thanks again...

            Comment

            • Tim Van Wassenhove

              #7
              Re: Session_ID()

              On 2004-03-06, Annie <AnnB@newsgroup .net> wrote:[color=blue]
              > Chung Leong wrote:
              >[color=green]
              >> I think you've misinterpretted the purpose of session_start() . While the
              >> function does create a new session when there isn't one, its primary
              >> function is to initialize the session mechanism (for instance, populate the
              >> $_SESSION array). It's best to interpret the "start" in session_start as
              >> "from this point forward, the script will start making use of session
              >> variables. The function that performs the opposite is session_write_c lose().
              >>
              >> I suggest using a session variable to note whether the user info has been
              >> saved or not, as follows:
              >>
              >> <?php
              >>
              >> session_start() ;
              >>
              >> if(!@$_SESSION['user info saved']) {
              >> ... save info in database ...
              >> $_SESSION['user info saved'] = true;
              >> }
              >>
              >> Uzytkownik "Annie" <AnnB@newsgroup .net> napisal w wiadomosci
              >> news:cCS1c.4554 8$PR3.929733@at tbi_s03...
              >>[color=darkred]
              >>>I have a script to check if a session exists, if not then I log the user's[/color]
              >>
              >> info from $_Server
              >>[color=darkred]
              >>>variables into a database (ip address, time, etc.).
              >>>
              >>>The problem is that the test:
              >>>
              >>>if(!session_ id()) // if there is no session started
              >>>{
              >>> session_cache_l imiter('nocache ');
              >>> session_cache_e xpire (20);
              >>> session_start() ;
              >>>
              >>> include_once ('../DB_functions.ph p');
              >>>
              >>>...the the other stuff, like logging goes here...
              >>>
              >>>
              >>>doesn't seem to work...the test always produces true...is there something[/color]
              >>
              >> I am missing?
              >>[color=darkred]
              >>>Thanks,
              >>>
              >>>Annie[/color]
              >>
              >>
              >>[/color]
              > Thanks...yeah the manual does not mention that you need to first run session_start before you check
              > session_id, and that does not match the way they suggest using it. I will use a session variable to
              > track the first entry into a session.[/color]

              From http://www.php.net/manual/en/ref.session.php

              Note: Please note when working with sessions that a record of a session
              is not created until a variable has been registered using the
              session_registe r() function or by adding a new key to the $_SESSION
              superglobal array. This holds true regardless of if a session has been
              started using the session_start() function.

              --

              Comment

              Working...