Hi, everybody,

I have two php files, this first one is:

Test1.php:
<?
session_start();

$_SESSION["hey"] = "Hey";

header("Location: http://localhost/Test2.php");
?>

and the Test2.php:

<?
session_start();
echo $_SESSION["hey"];
?>

when I tried to access the Test1.php the FIRST time. I got this error:

Notice: Undefined index: hey in C:...\Test2.php on line 3

The weried thing is that refreshing the Test2.php does not help, but if accessing Test1.php the second time makes everything right. Can anybody helps me explain this. This drove me crazy. thanks in advance

Bruce

    thanks, planetsim. but I'm using PHP 4.1.0, I think it's not necessary to explicitly register the variable by using session_register().

    thanks a lot

      Im using php 4.2.3

      In your first script you need to register the session

      session_register('variable');
      $_SESSION['variable'] = $data;

      second script

      session_start();

      once you start the session in the second script it gives you access to whatever you registered them as.

      NOTE THE SINGLE QUOTES

        it doesnt matter what version your using. Im not sure if its stated in the PHP Manual or even in session tutorials. But to use a sessions you

        1) Must start the session ie you must have session_start();
        2) You must register one. You cannot make a session without registering one.

          I think I read in the manual comments that if you use headers they have to be used:

          1. after session_start()
          2. include session_name()=session_id() or SID

          so your code should look like this

          Test1.php:
          <?
          session_start();
          $_SESSION["hey"] = "Hey";
          header("Location: http://localhost/Test2.php?session_name()=session_id()");
          ?>

          or like this

          Test1.php:
          <?
          session_start();
          $_SESSION["hey"] = "Hey";
          header("Location: http://localhost/Test2.php?SID");
          ?>

          From my own experience with SID I would use session_name()=session_id()

            edspace,

            Thanks. You did help me solve the problem though I finally decided to handle the session id manully. There are still some minor side effects when using "session_name()=session_id()" and leaving the burden of identifying session to PHP in my application, and I solved it by handling the session id in the directed page manully.

            Also thanks for all other postings, you did help me a lot on learning these stuff.

            best

            shu

              Write a Reply...