Sessions not Working on WAMP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PulsarSL@gmail.com

    Sessions not Working on WAMP

    Hey

    I have a WAMP setup (windows/apache/mysql/php). I'm running the latest
    version of all of them with a default installation of all of them. PHP
    works great with apache (as a module, not cgi), except that sessions
    aren't working. The script at the bottom of this post never increments
    my hit count. Do I need to turn sessions on somehow in php.ini?

    Thanks,

    PulsarSL



    ----

    Works great on a remote web server I have, so it's not the script...

    ----

    <?php
    // Initialize a session. This call either creates
    // a new session or re-establishes an existing one.
    session_start( );

    // If this is a new session, then the variable
    // $count will not be registered
    if (!session_is_re gistered("count "))
    {
    session_registe r("count");
    session_registe r("start");

    $count = 0;
    $start = time( );
    }
    else
    {
    $count++;
    }

    $sessionId = session_id( );

    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head><title>Se ssions</title></head>
    <body>
    <p>This page points at a session
    (<?=$sessionId? >)
    <br>count = <?=$count?>.
    <br>start = <?=$start?>.
    <p>This session has lasted
    <?php
    $duration = time( ) - $start;
    echo "$duration" ;
    ?>
    seconds.
    </body>
    </html>

    ----

  • vdP

    #2
    Re: Sessions not Working on WAMP

    PulsarSL@gmail. com wrote:[color=blue]
    > Hey
    >
    > I have a WAMP setup (windows/apache/mysql/php). I'm running the latest
    > version of all of them with a default installation of all of them. PHP
    > works great with apache (as a module, not cgi), except that sessions
    > aren't working. The script at the bottom of this post never increments
    > my hit count. Do I need to turn sessions on somehow in php.ini?
    >
    > Thanks,
    >
    > PulsarSL[/color]
    [color=blue]
    > ----
    >
    > Works great on a remote web server I have, so it's not the script...
    >
    > <?php
    > // Initialize a session. This call either creates
    > // a new session or re-establishes an existing one.
    > session_start( );
    >
    > // If this is a new session, then the variable
    > // $count will not be registered
    > if (!session_is_re gistered("count "))
    > {
    > session_registe r("count");
    > session_registe r("start");
    >
    > $count = 0;
    > $start = time( );
    > }[/color]

    If you use the latest version of WAMP or PHP, you are using PHP 5. This
    means that register_global s is disabled by default. Therefore you should
    use the superglobal $_SESSION, instead of session_is_regi stered and
    session_registe r. Quoting the manual-page of session_registe r at php.net
    ( http://nl3.php.net/manual/en/functio...n-register.php)
    "If you want your script to work regardless of register_global s, you
    need to instead use the $_SESSION array as $_SESSION entries are
    automatically registered. If your script uses session_registe r(), it
    will not work in environments where the PHP directive register_global s
    is disabled."

    Using $_SESSION the code can be rewritten as (may contain typos)
    if (!isset($_SESSI ON['count'])){
    $_SESSION['count']=0;
    $_SESSION['start']=time();
    } else {
    $_SESSION['count']++;
    }

    Hope this helps.

    vdP

    Comment

    • PulsarSL@gmail.com

      #3
      Re: Sessions not Working on WAMP

      [color=blue]
      >
      > Hope this helps.
      >[/color]

      Yes, thank you.

      PulsarSL

      Comment

      Working...