473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Session/Class help

Jon
All,

I'm currently building a custom Content Management system for a site we're
working on, and am stuck. Currently, I am using a couple of classes to run
most of the queries throughout the application. Well, I'm pretty stuck now.

What I need to do is use a variable throughout my classes that is a Session
variable. I really can't find another solution. The syntax I was using for
this variable before (I actually hard coded it during my testing and
development before this point) was this:

class C_DisplayContent{

var $username = "S_";

function foo(){
//code that uses the variable as $this->username
}//end foo

}//end class

So, what I really need now is to have the variable look like this:

var $username = $_SESSION['username'];

and be able to use it like normal using the $this->username syntax. However,
PHP seems to blow up at it. I've tried numerous ways of doing this
syntactically and I always end up with errors. The current error I'm getting
is:

Parse error: parse error, unexpected T_VARIABLE in

The line is of course the line where I'm assigning the variable.

What am I missing in regards to using session variables within a class? Any
help is appreciated.
Feb 28 '06 #1
3 1344
"Jon" <jo***@netins.com> wrote in message
news:du**********@news.netins.net...
All,

I'm currently building a custom Content Management system for a site we're
working on, and am stuck. Currently, I am using a couple of classes to run
most of the queries throughout the application. Well, I'm pretty stuck now.
What I need to do is use a variable throughout my classes that is a Session variable. I really can't find another solution. The syntax I was using for
this variable before (I actually hard coded it during my testing and
development before this point) was this:

class C_DisplayContent{

var $username = "S_";

function foo(){
//code that uses the variable as $this->username
}//end foo

}//end class

So, what I really need now is to have the variable look like this:

var $username = $_SESSION['username'];

and be able to use it like normal using the $this->username syntax. However, PHP seems to blow up at it. I've tried numerous ways of doing this
syntactically and I always end up with errors. The current error I'm getting is:

Parse error: parse error, unexpected T_VARIABLE in

The line is of course the line where I'm assigning the variable.

What am I missing in regards to using session variables within a class? Any help is appreciated.

Since $_SESSION is a superglobal do you really need to assign it to
$username?

or at least try:

var $username = '';

function set_user_session()
{
if (isset($_SESSION['username']) && !empty($_SESSION['username']))
{
$this->username = $_SESSION['username'];
}
else
{
$this->username = NULL; // or whatever you want
}
}

Norm
Mar 1 '06 #2

Jon wrote:
All,

I'm currently building a custom Content Management system for a site we're
working on, and am stuck. Currently, I am using a couple of classes to run
most of the queries throughout the application. Well, I'm pretty stuck now.

What I need to do is use a variable throughout my classes that is a Session
variable. I really can't find another solution. The syntax I was using for
this variable before (I actually hard coded it during my testing and
development before this point) was this:

class C_DisplayContent{

var $username = "S_";

function foo(){
//code that uses the variable as $this->username
}//end foo

}//end class

So, what I really need now is to have the variable look like this:

var $username = $_SESSION['username'];

and be able to use it like normal using the $this->username syntax. However,
PHP seems to blow up at it. I've tried numerous ways of doing this
syntactically and I always end up with errors. The current error I'm getting
is:

Parse error: parse error, unexpected T_VARIABLE in

The line is of course the line where I'm assigning the variable.

What am I missing in regards to using session variables within a class? Any
help is appreciated.


You can't initialize a class member that way with anything other than a
constant (for the most part). So function calls and other variables
are out. You can, however, initialize it through the constructor:

class foo {
public $username;

public function __construct() {
$this->username =& $_SESSION['username'];
}
}

Note that, because $this->username is assigned by reference, changes to
$this->username will also be reflected in $_SESSION (since they are two
variables pointing to the same value).

Mar 1 '06 #3
Jon
"Norman Peelman" <np******@cfl.rr.com> wrote in message
news:uz******************@tornado.tampabay.rr.com. ..
"Jon" <jo***@netins.com> wrote in message
news:du**********@news.netins.net...
All,

I'm currently building a custom Content Management system for a site
we're
working on, and am stuck. Currently, I am using a couple of classes to
run
most of the queries throughout the application. Well, I'm pretty stuck

now.

What I need to do is use a variable throughout my classes that is a

Session
variable. I really can't find another solution. The syntax I was using
for
this variable before (I actually hard coded it during my testing and
development before this point) was this:

class C_DisplayContent{

var $username = "S_";

function foo(){
//code that uses the variable as $this->username
}//end foo

}//end class

So, what I really need now is to have the variable look like this:

var $username = $_SESSION['username'];

and be able to use it like normal using the $this->username syntax.

However,
PHP seems to blow up at it. I've tried numerous ways of doing this
syntactically and I always end up with errors. The current error I'm

getting
is:

Parse error: parse error, unexpected T_VARIABLE in

The line is of course the line where I'm assigning the variable.

What am I missing in regards to using session variables within a class?

Any
help is appreciated.

Since $_SESSION is a superglobal do you really need to assign it to
$username?

or at least try:

var $username = '';

function set_user_session()
{
if (isset($_SESSION['username']) && !empty($_SESSION['username']))
{
$this->username = $_SESSION['username'];
}
else
{
$this->username = NULL; // or whatever you want
}
}

Norm


Yup - that did it. I actually just killed the username var off and used
$_SESSION['username'] throughout my application. I guess I didn't really
think about it being global already... I'm new to PHP classes, so it's good
to get this knowledge in. Thanks a ton for the help guys :)
Mar 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Darrin J Olson | last post by:
I am trying to end a session for a site without having to completely close the browser to end it. When I access the site and log in it works fine. If I log out and in with a different account...
1
by: Michael Albanese | last post by:
I am building an ASP.Net web application that records employee incident data over several screens. I have built custom classes to hold this information as the user enters data. In order to persist...
14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
4
by: Fred Nelson | last post by:
Hi: I'm a VB.NET programmer who is attempting to write my first C# web application. Everything is going VERY well however I have hit one snag: In the VB.NET world we can easily save classes...
2
by: Boban Dragojlovic | last post by:
I'm building a complex web-based reservations system. Gathering the user's data requires between 8 and 15 pages (depending on which options they are interested in). I use the "Session" object to...
2
by: Stuart | last post by:
Hi there I am using several processes within an .asp application that store variables in to session - typically: Session("UniqueName") = Value I am having a hell of a time overwriting the...
12
by: Thomas Andersson | last post by:
Hi, How can I access a session variable within a Public Class? I have tried the below code, but I get a server error "Object reference not set to an instance of an object". ...
5
by: Steven Blair | last post by:
I have the following code: Session = new CurrentUser("TEST"); When I postback to the server, the Session is null. My guess is a only the refence to my actual class is stored, rather than the...
6
by: Bhagya | last post by:
Hello, On the LogOut Page i have done Session.Abandon(); And on every Page, In the Page_Load Event i check if the session exists and only then display data. Now the problem is after i logout from...
8
by: eric | last post by:
I have a 2.0 asp.net project. In a class contained within a seperate project, I am trying to reference HttpContext.Current.Session but Session is always null. I've tried implementing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.