PHP for Dummies (Me being the dummy)

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

    PHP for Dummies (Me being the dummy)

    Maybe it is just cos it is Friday and my head is already gone away for
    the weekend but I just cannot get sessions working!

    Anyway - two pages. Page one starts session and registers a sesison
    variable, Page 2 tries to then print out the session variable...

    Sorry if its obvious but I have stripped down my previously working
    code to just these few lines and still cannot fathom where the error
    lies.

    Simple. BUT IT WON'T WORK! PLEASE PUT ME OUT OF MY MISERY!

    Page 1:
    <?php
    session_start() ;
    session_registe r("test");
    $_SESSION["test"] = "Hello World";
    ?>
    <a href='check_tes t.php'>Check Test</a>


    Page 2:
    <?php
    if ( session_is_regi stered("test") == true ) {
    print "test is a registered session variable";
    } else {
    print "test is NOT a registered session variable";
    }
    ?>

    <br/>

    <?php
    print "test is " . $_SESSION["test"];
    ?>
    <br />
    <a href='set_test. php'>Set Test</a>




    Thanks,

    Rick

  • Ken Robinson

    #2
    Re: PHP for Dummies (Me being the dummy)


    rik wrote:[color=blue]
    > Maybe it is just cos it is Friday and my head is already gone away for
    > the weekend but I just cannot get sessions working!
    >
    > Anyway - two pages. Page one starts session and registers a sesison
    > variable, Page 2 tries to then print out the session variable...
    >
    > Sorry if its obvious but I have stripped down my previously working
    > code to just these few lines and still cannot fathom where the error
    > lies.
    >
    > Simple. BUT IT WON'T WORK! PLEASE PUT ME OUT OF MY MISERY!
    >
    > Page 1:
    > <?php
    > session_start() ;
    > session_registe r("test");[/color]

    Do not use session_registe r( when you use session_start()
    [color=blue]
    > $_SESSION["test"] = "Hello World";
    > ?>
    > <a href='check_tes t.php'>Check Test</a>
    >
    >
    > Page 2:
    > <?php[/color]

    You need to use session_start() in each script where you want to use
    Sessions.
    [color=blue]
    > if ( session_is_regi stered("test") == true ) {[/color]

    Here you should use

    if (isset($_SESSIO N['test']))
    [color=blue]
    > print "test is a registered session variable";
    > } else {
    > print "test is NOT a registered session variable";
    > }
    > ?>
    >[/color]

    Ken

    Comment

    • rik

      #3
      Re: PHP for Dummies (Me being the dummy)

      Ken, you are a beautiful, beautiful man.

      I, am a stupid, stupid man.

      Regards,

      Rick

      Comment

      • JDS

        #4
        Re: PHP for Dummies (Me being the dummy)

        On Fri, 05 Aug 2005 09:01:08 -0700, rik wrote:
        [color=blue]
        > Ken, you are a beautiful, beautiful man.[/color]

        I don't know about that...
        [color=blue]
        >
        > I, am a stupid, stupid man.[/color]

        Not so sure about that, either....
        [color=blue]
        >
        > Regards,
        >
        > Rick[/color]

        I have one tip regarding sessions and PHP > 4.2 (maybe > 4.1 even)

        Don't use "session_regist er()"! ONLY use $_SESSION to get data in and out
        of sessions. session_registe r() is an evil function that needs black
        voodoo magic incantations and the wind blowing in the right direction to
        work (consistently, at least).

        $_SESSION *always* works (well, as close to "always" as you can get when
        talking about computer programming). And is *MUCH* more straightforward
        than session_registe r().

        Just a tip. Of course, you *do* have to remember to do session_start() on
        each PHP page that is going to be "in session".

        I also recommend putting all of your configuration information, including
        the "session_start( )" command, in an external file and calling that file
        using include() or require() at the start of each page within the same PHP
        application.

        later...

        --
        JDS | jeffrey@example .invalid
        | http://www.newtnotes.com
        DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

        Comment

        Working...