Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Var won't register 1

Status
Not open for further replies.

rouse01

IS-IT--Management
Sep 10, 2001
143
US
The variable 'start' does not seem to register. Any idea? I'm running php 4.1.2 on linux apache 1.3.x.

<?php
// Create a new session or re-establish an existing one.
session_start( );
// If new session, then $count will not be registered
if (!session_is_registered( &quot;count&quot;))
{
echo &quot;not registered&quot;;
echo &quot;<br />&quot;;
session_register(&quot;count&quot;, &quot;start&quot;);
$count = 0;
$start = time( );
echo &quot;Registering Count: &quot; . $count . &quot;<br/>&quot;;
echo &quot;Registering Start: &quot; . $start . &quot;<br />&quot;;
}
else
{
echo &quot;registered <br />&quot;;
$count++;
echo &quot;Registering Count: &quot; . $count . &quot;<br/>&quot;;
echo &quot;Registering Start: &quot; . $start . &quot;<br />&quot;;
echo &quot;<br />&quot;;
}
$sessionId = session_id( );
?>

<html>
<head><title>Sessions</title></head>
<body>
<p>This page points at session:
(<?=$sessionId?>)
<br>count = <?= $count?>.
<br>start = <?= $start?>.

<p>This session has lasted
<?php
$duration = time() - $start;
echo &quot;$duration&quot;;
?>
seconds.
</body>
</html>

I'm learning php. If possible can you supply an example of how to destroy this session with a button? I tried:
<input type=&quot;button&quot; name=&quot;Destroy&quot;
value=&quot;Destroy&quot;
alt=&quot;Destroy Session&quot;
onclick=&quot;session_destroy()&quot;;>
This would save from having to ssh to server & deleteing the session in the \tmp.
Thanks in advance - Keith
 
You're trying to do it from the client side. PHP is a server-side language. You need to supply a link to a script that will destroy the session.

As far as registering the variable goes, do you get any errors? Does &quot;count&quot; register? Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi sleipnir. No error messages. The first time thru the script, count = 0 & the &quot;not registered&quot; is echoed. Thereafter, count never increments past 1. Start never seems to register - always echos a blank.
I copied this script from another php site as an example to help get me going on session control. I only added the echos to help with debugging.
I was going to use the $_session[] method for the variables, but I tripped over the syntax.
Also, thanks for pointing out my button logic error.
 
The syntax is simple. $_SESSION can be manipulated like any other array.

If you need a new session variable, just issue: $_SESSION['start'] = time();

Then when you need to access the value in future scripts, just reference $_SESSION['start'].

To get rid of the variable, just say unset($_SESSION['start']);

To get rid of all your session variables at once: $_SESSION = array();

I strongly recommend direct manipulation of $_SESSION instead of using session_regiser() and then manipulating variables. $_SESSION is superglobal, so it's available in functions without the use of &quot;global&quot;. It helps you keep track of where a variable came from -- you may not know where $start came from, but $_SESSION['start'] is pretty obvious. You don't have to depend on the setting of register_globals. Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks sleipnir. Apparently php doesn't like $_session, but rather likes $_SESSION. Also note that I switched to &quot;isset()&quot;. I've read &quot;session_is_registered()&quot; is not recommended with $_SESSION variables.
I have created a calculator form which generates a pdf document. Currently using $_POST variables. I need to store the supporting document data in a mysql table. Would you recommend I switch to $_SESSION or does it matter?

Here's the fixed session script, in case anyone's interested...
<?php
// Create a new SESSION or re-establish an existing one.
SESSION_START();
// If new SESSION, then $COUNT will not be registered
if(!isset($_SESSION['COUNT']))
{
echo &quot;not registered&quot;;
echo &quot;<br />&quot;;
$_SESSION['COUNT'] = 0;
$_SESSION['START'] = TIME( );
echo &quot;Registering COUNT: &quot; . $_SESSION['COUNT'] . &quot;<br/>&quot;;
echo &quot;Registering START: &quot; . $_SESSION['START'] . &quot;<br />&quot;;
}
else
{
echo &quot;registered <br />&quot;;
$_SESSION['COUNT']++;
echo &quot;Registered COUNT: &quot; . $_SESSION['COUNT'] . &quot;<br/>&quot;;
echo &quot;Registered START: &quot; . $_SESSION['START'] . &quot;<br />&quot;;
echo &quot;<br />&quot;;
}
$SESSIONID = SESSION_ID();
?>

<html>
<head><title>sessions</title></head>
<body>
<p>This page points at session:
(<?php echo $SESSIONID?>)
<br>COUNT = <?php echo $_SESSION['COUNT']?>.
<br>START = <?php echo $_SESSION['START']?>.

<p>This SESSION has lasted
<?php
$duration = TIME() - $_SESSION['START'];
echo &quot;$duration&quot;;
?>
seconds.

<?php if($_SESSION['COUNT'] > 9)
{
session_unset();
session_destroy();
}
?>
</body>
</html>
 
Yes, although function names are not case-specific in PHP, variable names are.

$_SESSION is only necessary to maintain data between script executions. If you do not need your user's data in any subsequent scripts, then use of session variables is not necessary. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top