SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    redundant register session variable

    I read article about session in http://hotwired.lycos.com/webmonkey/...ex2a_meta.html , this article says that

    PHP Code:
    <?
        session_start
    ();
        
    session_register('body_color');
        
    session_register('text_color');
    ?>
    If we just used this at the top of the page, then each time the page is loaded, the value of $body_color and $text_color would be overwritten by an empty string or a newly registered, empty variable.
    If this article true, why this code work correct, because it register new session everytime that reload
    PHP Code:
    <?php
        session_start
    ();
        
    session_register('counter');
        
    $counter++;
        echo 
    "<p>You visited $counter time. Thanks!</p>";
    ?>
    I live in Thailand. My English grammar not well.

  2. #2
    Are you ready for BSD? Marshall's Avatar
    Join Date
    Dec 2001
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this article true, why this code work correct, because it register new session everytime that reload
    PHP's session implementation works by associating any number of values with a unique session key. This session key is passed back to the server with every request from the client via either cookie values or URL rewriting. What your code example does is associate the `counter' variable with the session key assigned to the current client.

    The first time the session_start() function is called a unique session key is generated and passed to the client. For each subsequent call, since the client returns the same valid session key, session_start() does not create a new session but instead reloads the variables previously associated with that client's key. Therefore, every time the client reloads the page with this script, `counter' has the same value that it had after the last time the client accessed that page.

    - Marshall

  3. #3
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you mean that when client returns valid session key, session_start() does not create a new session and skip session_register('counter')?

    My question is when it has counter as a session variable and when reload and get session_register(counter) again, What happen?

    Another question please, Is valid session key means session id on client same as session id on server?
    I live in Thailand. My English grammar not well.

  4. #4
    Are you ready for BSD? Marshall's Avatar
    Join Date
    Dec 2001
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you mean that when client returns valid session key, session_start() does not create a new session and skip session_register('counter')?
    The session_register() function will implicitly call session_start() if it has not already been called, meaning that you don't need to call session_start() directly if you don't want to. session_register() causes PHP to track the variables specified as function arguments and associate those values with the current client's session key. PHP does not skip the session_register() call -- the call just doesn't do anything if the variable is already being tracked. To stop a variable from being tracked you use the session_unregister() function.

    My question is when it has counter as a session variable and when reload and get session_register(counter) again, What happen?
    If the client already has a valid session key, variables associated with that session -- such as `count' in your example -- will be automatically loaded into the script's address space when the script is called. These values will, however, be overwritten if the user passes the save variable name as a query parameter. Calling session_register('count') avoids this possible problem by additionally setting the value of $count to $HTTP_SESSION_VARS['count'].

    Another question please, Is valid session key means session id on client same as session id on server?
    Yes, my use of "session key" is the same as PHP's use of "session id."

    - Marshall

  5. #5
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Marshall, and this quote is wrong

    If we just used this at the top of the page, then each time the page is loaded, the value of $body_color and $text_color would be overwritten by an empty string or a newly registered, empty variable.
    I live in Thailand. My English grammar not well.

  6. #6
    Are you ready for BSD? Marshall's Avatar
    Join Date
    Dec 2001
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If we just used this at the top of the page, then each time the page is loaded, the value of $body_color and $text_color would be overwritten by an empty string or a newly registered, empty variable.
    This quote may be correct, depending on the context. When session_register() is called it overwrites any GET, POST or cookie values for the specified variable with the stored value for the current session. So, if the GET, POST or cookie value is what you want, and the session value is not yet defined, then you will indeed be overwriting the existing values with a blank string.

    - Marshall

  7. #7
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. I see, Thanks again.
    I live in Thailand. My English grammar not well.


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
  •