473,795 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DisplayConten t{

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 1362
"Jon" <jo***@netins.c om> 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_DisplayConten t{

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_sessio n()
{
if (isset($_SESSIO N['username']) && !empty($_SESSIO N['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_DisplayConten t{

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.r r.com> wrote in message
news:uz******** **********@torn ado.tampabay.rr .com...
"Jon" <jo***@netins.c om> 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_DisplayConten t{

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_sessio n()
{
if (isset($_SESSIO N['username']) && !empty($_SESSIO N['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
4078
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 without closing the browser, I get the first session's information. Also, if a open another browser and log in with a different account I get the session from the first browser. I've tried deleting all cookies and any cached pages, but still get the...
1
281
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 the data, i am trying to use session memory. The problem is that when i try to cast the class that is in session memory into a class on my page and i get NOTHING! Actually, my watch window tells me that the class equals nothing...... Where am...
14
3241
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 go to is a login form, which will set several session variables with the name used to log in, appropriate security level and some other misc variables, and then will go to a main menu for each particular security level using Server.Transfer. ...
4
3049
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 in session variables. I'm hoping that I can do the same thing in C# however I'm doing something wrong since I get the error: Cannot implicitly convert type 'object' to 'datafunct.sessioninfo'
2
2393
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 store the various elements as the user moves through the pages. Rather than storing the preferences directly in the Session object (e.g. Session("LastName") = ...), I created a class <Serializable()> Public Class ReservationInfo
2
2289
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 Session value once it has been stored the first time - namely I can't
12
7314
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". System.Web.HttpContext.Current.Session.Add("myKey", "myValue") Dim a As String a = System.Web.HttpContext.Current.Session.Item("myKey")
5
1974
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 class. Looking on the internet, one solution posted was populating Session in Session_Start in a global.asax file. Again, on postback, the value is
6
2811
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 application and click the back button of Internet Explorer, the page displays. Can anyone guide me plsssss. Thank you, Bhagya
8
996
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 IRequiresSessionState but it does not seem to matter. For a test I created a small solution consisting of a web project and a seperate project to hold a test class. In the test class I reference HttpContext.Current.Session and it works fine with or without...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.