Jump to content
xisto Community
Sign in to follow this  
realthor

Storing Session Variables Generated Dynamically

Recommended Posts

i have many pool (quiz) modules on my site with the information taken from a database. The modules are processed by the same function with different parameters and i need to save the pool's status to prevent a user vote several times. I do this setting $_SESSION[ 'variable' ] to 1 , variable being the name of the quiz, which is taken from the database. So i get to having a code like this:
....................
$name= $row->quiz_name;
$_SESSION[$name]=1

The problem is that if i call $_SESSION[$name] from another part of the site (another page) the variable is always zero and if i check the $name by writting

echo '$_SESSION['. $name .']= '.$_SESSION[$name] I get as output  $_SESSION[]=0

If i store the value in a variable like this $_SESSION['quiz_1']=1 then it shows as this everywhere.
One problem could be that that the $name, actually defined in a function, isn't global and thus it exists only in the function but even if i declare it global $name in the function it still doesn't show up on another page.

But in the function the variable $name is generated every time the function is called and it's the same for one quiz but it's value isn't stored even among two calls of the function. If i vote for one option then the variable is set to 1 then i refresh the page and it votes again.
Can u give me some advice as this is the first time i'm using this in php...

Share this post


Link to post
Share on other sites

humm, abit confusing but i get the idea that the session variable name isnt stored properly.

 

The first thing i notice is that you have "$_SESSION['$name]" personally i have never used a variable inside an array like that so you might want to try it without the dollar sign so its not a variable within an array. That might help.

 

I think what you are trying to do with the cade is you have a variable called $name which contains a users name and you want this into a session variable?

 

if so just have code like:

$_SESSION['name'] = $name;

but either way try dropping the dollar sign and putting "name" into single ' ' quotes that might help.

 

Its what i would try first.

Share this post


Link to post
Share on other sites

i solved the problem the variable creation was put in an IF statement not always true so not always creating it. God it took me 3 hours to find this :)not really, it's ok putting the variable inside like $_SESSION[ $name ] and becouse in my script this thing is inside a function called by many pages every new call needs a new name so the name is generated inside the function, and also is verified inside the function with the generated name:$id = $_POST['id'] ; -from the form$session = $name.$id ;if ( $_SESSION[$session]=1 )...than the visitor has been here before and did once whatever the function does :)

Edited by realthor (see edit history)

Share this post


Link to post
Share on other sites

not really, it's ok putting the variable inside like $_SESSION[ $name ]

i learn something new everyday!

Glad to see you got it in the end :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.