Connecting Tech Pros Worldwide Help | Site Map

Sessions not Working on WAMP

PulsarSL@gmail.com
Guest
 
Posts: n/a
#1: Dec 24 '05
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_registered("count"))
{
session_register("count");
session_register("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>Sessions</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
Guest
 
Posts: n/a
#2: Dec 24 '05

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_registered("count"))
> {
> session_register("count");
> session_register("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_globals is disabled by default. Therefore you should
use the superglobal $_SESSION, instead of session_is_registered and
session_register. Quoting the manual-page of session_register at php.net
( http://nl3.php.net/manual/en/functio...n-register.php)
"If you want your script to work regardless of register_globals, you
need to instead use the $_SESSION array as $_SESSION entries are
automatically registered. If your script uses session_register(), it
will not work in environments where the PHP directive register_globals
is disabled."

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

Hope this helps.

vdP
PulsarSL@gmail.com
Guest
 
Posts: n/a
#3: Dec 24 '05

re: Sessions not Working on WAMP


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

Yes, thank you.

PulsarSL

Closed Thread


Similar PHP bytes