Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2003, 02:26 AM
Authorized User
 
Join Date: Aug 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default session problem

Is there any problem in the code below?

Always printing Counter initialized, please reload this page to see it increment, no matter how many I refresh also.

By right the counter should increase when I refresh the page.

I'm running PHP Version 4.3.1 on linux machine with apache.

Please help.

<?
session_start();
session_register("SESSION");

if (!isset($SESSION)) {
    $SESSION["count"] = 0;
    echo "<li>Counter initialized, please reload this page to see it increment";
} else {
    echo "<li>Waking up session $PHPSESSID";
    $SESSION["count"]++;
}
echo "<li>The counter is now $SESSION[count] ";
?>

Regards,
ganesh

 
Old August 13th, 2003, 03:07 AM
Authorized User
 
Join Date: Aug 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Working when I change codes the way below... but do not know the actual reason or problem.. can anyone explain..

<?
session_start();
session_register('myvar');

if (!isset($HTTP_SESSION_VARS["myvar"])) {
    $HTTP_SESSION_VARS["myvar"] = 0;
    echo "<li>Counter SDS initialized, please reload this page to see it increment";
} else {
    echo "<li>Waking up session PHPSESSID";
    $HTTP_SESSION_VARS["myvar"]++;
}
echo "<li>The counter is now ";

echo $HTTP_SESSION_VARS["myvar"]
?>

Regards,
ganesh


 
Old August 13th, 2003, 03:52 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Ganesh,
You just have a little typo in there...

You should be using $_SESSION instead of $SESSION.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 13th, 2003, 05:04 AM
Authorized User
 
Join Date: Aug 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. That solved my problem.

 
Old August 18th, 2003, 01:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Careful! Look at the original code -- he passed "SESSION" to session_register. You definitely do _NOT_ want to session_register $_SESSION!!

$_SESSION replaces $HTTP_SESSION_VARS and should NOT be used when session_register() is used.

The simplest correct rewriting of your script is as follows:

Code:
<?php
session_start();

if (!isset($_SESSION['count']))
{
    $_SESSION["count"] = 0;
    echo "<li>Counter initialized, "
       . "please reload this page to see it increment.";
}
else
{
    echo "<li>Waking up session " . session_id();
    $_SESSION['count']++;
}

echo "<li>The counter is now $SESSION[count]";
?>


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Out Problem kasipandian J2EE 3 March 21st, 2008 03:35 PM
session problem MunishBhatia ASP.NET 2.0 Professional 9 October 6th, 2007 04:06 AM
Session problem abdulweb General .NET 3 August 27th, 2007 08:01 PM
session and cookie problem (empty session file) msincan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 27th, 2005 05:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.