PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
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  
Old October 2nd, 2002, 02:40 PM
everytime everytime is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: *.*
Posts: 133 everytime User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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.

Reply With Quote
  #2  
Old October 2nd, 2002, 03:03 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: registering session variables within classes

When using $_SESSION, you shouldn't use session register..just assign a value to it...

php Code:
Original - php Code
  1. Function test()
  2. {
  3. $newvar="persist";
  4. session_register("newvar");
  5. }


becomes

php Code:
Original - php Code
  1. Function test()
  2. {
  3. $_SESSSION['newvar'] ="persist";
  4. }


also you shouldn't need to recall session_start() at the top of your inc file....

Reply With Quote
  #3  
Old October 2nd, 2002, 09:02 PM
everytime everytime is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: *.*
Posts: 133 everytime User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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 :-(


Reply With Quote
  #4  
Old October 3rd, 2002, 12:16 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
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()...


Reply With Quote
  #5  
Old October 3rd, 2002, 07:54 AM
everytime everytime is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: *.*
Posts: 133 everytime User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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;
to
Code:
$_SESSION['insideclass']=5;
whilst the bowser session is still open then the value is changed. So i thought it may be something to do with the incrementation - but the code isn't (i hope) different to what's happening to the outsideclass variable.

I appreciate your help on this one

Reply With Quote
  #6  
Old October 3rd, 2002, 08:24 AM
v_jansen v_jansen is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Netherlands
Posts: 25 v_jansen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #7  
Old October 3rd, 2002, 09:12 AM
everytime everytime is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: *.*
Posts: 133 everytime User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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.

Reply With Quote
  #8  
Old October 3rd, 2002, 10:46 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
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...

Reply With Quote
  #9  
Old October 3rd, 2002, 12:00 PM
everytime everytime is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: *.*
Posts: 133 everytime User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > registering session variables within classes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway