|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
registering session variables within classes
hi,
I have a class in which a session variable is registered in the constructor. However looking a the session file all that is stored is the name of the variable and no value. The same code works fine to register the variable if NOT done in a class. here is an example file: ses.php Code:
<?php
session_start();
echo session_id();
echo '<br/>';
if(isset($_SESSION['var']))
{
$_SESSION['var']=$_SESSION['var']+1;
echo $_SESSION['var'];
}
else
{
$var=1;
session_register('var');
}
include("ses.inc");
$example = new test;
$example->sayvar();
?>
file: ses.inc Code:
<?php
session_start();
class test
{
Function test()
{
$newvar="persist";
session_register("newvar");
}
function sayvar()
{
echo '<br/>';
echo session_id();
echo '<br/>';
echo 'no'.$_SESSION['newvar'];
}
}
?>
Has anyone else come accross this? Please help if you can. |
|
#2
|
|||
|
|||
|
RE: registering session variables within classes
When using $_SESSION, you shouldn't use session register..just assign a value to it...
becomes also you shouldn't need to recall session_start() at the top of your inc file.... |
|
#3
|
|||
|
|||
|
RE: registering session variables within classes
hmmm
the following, not in a class file, didn't output anything to the browser Code:
<?php
session_start();
if(isset($_SESSION['value']))
{
$_SESSION['value']+=1;
echo $_SESSION['value'];
}
else
{
$_SESSSION['value'] =0;
}
?>
I thought you had to use session_register the first time you declared a variable you want to be stored in a session? Also still can't get any session variables to be created from within a class :-( |
|
#4
|
|||
|
|||
|
RE: registering session variables within classes
The code in your last post has one too many S's in the last session...other than that it works...
I have tried the class with my test constructor and it works....what version of PHP are you running? It does say in the manual that if you use $_SESSION that you should not use session_register()... |
|
#5
|
|||
|
|||
|
RE: registering session variables within classes
oops about the S's. Good spot. Changing that I STILL cannot get the class stuff to work.
my class file Code:
<?php
class test
{
Function test()
{
$_SESSION['insideclass'] =0;
}
function sayvar()
{
if(isset($_SESSION['insideclass']))
{
$_SESSION['insideclass']+=1;
echo $_SESSION['insideclass'];
}
}
}
?>
my calling file Code:
<?php
session_start();
echo session_id();
echo '<br/>';
include("ses.inc");
$example = new test;
$example->sayvar();
if(isset($_SESSION['outsideclass']))
{
$_SESSION['outsideclass']+=1;
echo $_SESSION['outsideclass'];
}
else
{
$_SESSION['outsideclass'] =0;
}
?>
I'm running php 4.2.3 version on a win2000 platform. The insideclass variable doesn't get incremented. However if I change the code from Code:
$_SESSION['insideclass']+=1; Code:
$_SESSION['insideclass']=5; I appreciate your help on this one |
|
#6
|
|||
|
|||
|
RE: registering session variables within classes
so your output doesn't look something like
36df6ab6e2020688201501009372237c 1 // refresh 36df6ab6e2020688201501009372237c 11 // refresh 36df6ab6e2020688201501009372237c 12 It works fine for me in win98/php 4.2.2 |
|
#7
|
|||
|
|||
|
RE: registering session variables within classes
thanks for trying that out v_jansen. My output looks just like that, but with a different session_id obviously.
But the first number should also be incrementing shouldn't it? That's the problem. |
|
#8
|
|||
|
|||
|
RE: registering session variables within classes
Do you keep refreshing the same page trying to get the insideclass variable to increment? If so, that could be your problem. Each time you refresh, you also start a new instance of the class. When you start the new instance, you are setting the insideclass variable back to 0...
You might want to move the initialization of that session variable to a member function and only call it when needed... |
|
#9
|
|||
|
|||
|
RE: registering session variables within classes
That's it! I have added a check in the constructor to see if the session variable has been registered already. That now works. Thanks Matt, you've done it again.
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > registering session variables within classes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|