Solved

Session variables retain only the last sesion variable stored

Posted on 2011-09-29
5
242 Views
Last Modified: 2012-06-27
I am using session variables to store data

I am trying to store 4 or 5 variables but seem to only end up with 1 value in the session file

I have attached the session class, some session files and my php.ini
The total contents of a session file where the last thing I set was the the username

$varname|s:8:"johnec50";

The total contents of a session file where the last thing I set was the the user role

$varname|s:5:"Admin";

The total contents of a session file where the last thing I set was the value of "yes" for  a variable called auth

$varname|s:3:"yes";

It looks like the variables overwrite themselves  

Please if you can help


Thanks for reading this

John php.ini
Session.class
0
Comment
Question by:johnecollins
  • 2
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36816851
try replacing the line
$_SESSION['$varname'] = $value;
with
$_SESSION[$varname] = $value;

inside your class
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36816855
(in every place where it appears)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36890846
As implemented, the literal string $varname, not the variable contained in $varname, has become the key into the session array.  Note the different ways of defining strings in PHP.  Please read this page carefully to understand the difference that arises when you use single quotes.
http://php.net/manual/en/language.types.string.php

Also, it looks like the information in the session has been serialized and I do not know of a good design pattern that would include doing that.  The session uses an array named $_SESSION.  Here is everything you need to do to use the session:

<?php
session_start();
$_SESSION["john"] = "collins";
var_dump($_SESSION);

If you're interested in client authentication using PHP, this article shows the essential moving parts.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
0
 

Author Comment

by:johnecollins
ID: 36893852
Roads_Roads
Is it ok to call you Roads?

Thank you for your solution superb - mind you you knew it would work ;-)

Thanks for your help

Ray, another fine contribution to my efforts I shall read it through.

Thank you both

John
0
 

Author Closing Comment

by:johnecollins
ID: 36893902
Cheers

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now