Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Storing Session Variables Generated Dynamically
realthor
post Oct 9 2006, 06:46 AM
Post #1


Super Member
*********

Group: Members
Posts: 202
Joined: 7-November 05
Member No.: 13,928



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
CODE
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...
Go to the top of the page
 
+Quote Post
shadowx
post Oct 9 2006, 05:50 PM
Post #2


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
***********

Group: [HOSTED]
Posts: 1,028
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



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:
CODE

$_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.

Go to the top of the page
 
+Quote Post
realthor
post Oct 10 2006, 08:10 AM
Post #3


Super Member
*********

Group: Members
Posts: 202
Joined: 7-November 05
Member No.: 13,928



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 biggrin.gif

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 wink.gif

This post has been edited by realthor: Oct 10 2006, 08:16 AM
Go to the top of the page
 
+Quote Post
shadowx
post Oct 12 2006, 01:51 PM
Post #4


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
***********

Group: [HOSTED]
Posts: 1,028
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



QUOTE
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 smile.gif

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Session Start() Problem(5)
  2. Transfer Variables To Another Php Script(8)
  3. Suppressing Mysql Error In Php(4)
  4. Converting Characters In A Variable To Individual Values In An Array(2)
  5. Session Variables(4)
  6. Php - Can Anyone Tell Me What "session" Actually Do?(5)
  7. How Do I Pass Php Variables From A Url To A Form?(5)
  8. Sending $_get[] Variables To An Application(3)
  9. Php-fusion Cms(3)
  10. Php Session(6)
  11. Dynamically Select From 2 Tables Based On Id(1)
  12. Php Sessions And Post Variables Issues(1)
  13. Counting Variables?(4)


 



- Lo-Fi Version Time is now: 10th October 2008 - 06:55 PM