Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Session variables retain only the last sesion variable stored

Posted on 2011-09-29
5
Medium Priority
?
278 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 2000 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 111

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

885 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