Hi,

I am using the following code to read the session variable and set it as value for input textbox:

value="<?php if ($_SESSION["activation_email"] != NULL) echo $_SESSION["activation_email"]; ?>"

it was set in another code like this:

$_SESSION["activation_email"] = $member_email;

but I am getting the following error inside the textbox:

<br /><b>Notice</b>: Undefined index: activation_email in <b>C:\xampp\htdocs\xoompage\activation.php</b> on line <b>21</b><br />

just a note that the above error is NOT shown every time... it's just sometimes but I am not sure why?

    Well, of course, make sure session_start() is being called.

    Then use [man]isset/man on it first:

    <?php
    if (isset($_SESSION["activation_email"])) {
       if ($_SESSION["activation_email"] != NULL) {
          echo $_SESSION["activation_email"];
       }
    } else {
       //the variable isn't set ... something is wrong?  Or do we just log an error ...
    }
    ?>

    EDIT: That will take care of the error.

    The answer to "why" ... probably has something to do with the variable $member_email not being set when you assign it to the $_SESSION array.

      The undefined index error happens any time you try to refer to an array element that has not yet been defined. I.e., the array simply does not have an element with that key/index.

        One gotcha to look out for is accessing some URLs via "www.example.com" and some via just "example.com". I never remember off the top of my head whether it's going from with the sub-domain to without or vice versa, but in whichever case, the session cookie won't be sent by the browser, unless you define the session.cookie_domain to be ".example.com" (note the leading dot). This can be done in your PHP config (best), or by explicitly setting it via [man]session_set_cookie_params/man on a per script basis (obviously a rather annoying option, unless you can stick it in an include file that is already being used by ever script).

          When you set a cookie, that cookie is associated with a domain. The cookie is only delivered to the server by your browser if the domain requested matches the domain of the cookie. example.com and www.example.com are two distinct domains so if you set the cookie for one domain, it will not apply for the other domain. This becomes apparently when dealing with session cookies because going from example.com where you are logged in to www.example.com can result in you appearing to be logged out.

          From the documentation on [man]setcookie[/man]:

          phpdocs wrote:

          The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

            Write a Reply...