Storing Session Variables Generated Dynamically

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Storing Session Variables Generated Dynamically

realthor
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...

 

 

 


Reply

shadowx
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.


Reply

realthor
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

Reply

shadowx
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


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : storing session variables generated dynamically

  1. Counting Variables? - (4)
  2. Transfer Variables To Another Php Script - (8)
    Hello, I've one registration page where the users fills in their information, is it possible to
    trasnfer the things the fill in on the registration page to another script that does someting and
    returnes something to the first page like true/false and then the registration gives an error
    messange if the other php script returned false? Something like the script "activates" another
    script that does something and returnes the result back to the original script. Best Regards ...
  3. Php Sessions And Post Variables Issues - My script dosent seem to work as intended (1)
    You can test it out for yourself at http://sonesay.trap17.com/application.php I've been
    working on this page locally and it seems to be working fine but when I upload it to my trap17
    account the post variables dont get saved properly. Fill in some fields and submit it, the form
    will come up as a empty field yet when you resubmit it without any modifications and the data you
    entered in orginally will now magically appear, resubmit it again and it will be gone. This is
    really annoying as I have no clue why it would be doing this when it seems to work fine locally....
  4. Dynamically Select From 2 Tables Based On Id - dynamically select from 2 tables based on id (1)
    hi there, im working on a performance request system, and i have three tables that im working with
    right now. the performances table holds the info about the performance (date, times, status, etc.),
    and has two columns, org_type and org_id, which both hold an int value to tell me where the
    organization info is. there are two org types which have respective tables, because we collect
    different information based on the type of org that is registering. is there any way to select info
    from all three tables (dynamically selecting between the two org tables based on org_type,...
  5. Php Session - (6)
    Okay, I just got all my site stuff transferred over to Trap17, so I'm ready to resume coding.
    I'm currently working on my members' pages, and wish to use the PHP Session tags and include
    it in a function so that on every page, I just include the function to check and see if that user is
    still logged in and is allowed to view that page. Somebody told me I need to use the isset( blah
    blah blah and check to see if they were logged in from the login page (not sure how to do that),
    then query the $_SESSION so I can check the 'userlevel' to check if ...
  6. Php-fusion Cms - No session_start() used? (3)
    A while back i was looking for free hosting and a forumn suite for my own comunity. I ended up using
    hostingdirect.co.nz and php-fusion from http://php-fusion.co.uk It was pretty easy to setup and
    run. But my site required more specific functions other then generic CMS could offer and I wanted to
    learn PHP so I start reading tutorials and such to learn. I was working on them locally and they
    seem to run fine on my own server. When i upload my own site to the hostingdirect site I find they
    do not have safe_mode off. I got an error in my index.php file when i try and ru...
  7. Sending $_get[] Variables To An Application - (3)
    I'm trying to work out if it is possible to send variables to a application on server. E.g
    send the variable of id which is equal to 10 to an application - test.exe?id=10 Any ideas to see if
    this is possible?...
  8. How Do I Pass Php Variables From A Url To A Form? - I'm about read to tear my hair out with this one (5)
    Ok, what I need is for users to be able to select what address they want (e.g. mydomain + .com),
    click submit that then sends the variables 'domain' and 'ext' through the URL e.g.
    mywebsite.com/example.php?domain=mydomain&ext=com (the variables being mydomain and com) where they
    are displayed in a form mail as "Domain Selected: mydomain.com" that can then be submitted and sent
    to an email. In my form so far I have CODE <input name="domainname"
    type="text" id="domainname" value ="<?php echo "$domain"; ?...
  9. Php - Can Anyone Tell Me What "session" Actually Do? - (5)
    php - can anyone tell me what "session" actually do? I was looking at this page
    http://www.w3schools.com/php/php_sessions.asp this paragraph here: A PHP session solves this
    problem by allowing you to store user information on the server for later use (i.e. username,
    shopping items, etc). However, session information is temporary and will be deleted after the user
    has left the website. If you need a permanent storage you may want to store the data in a database.
    Especially this part below: QUOTE A PHP session solves this problem by allowing you to store
    user in...
  10. Session Variables - Sessions in PHP behaving strangely (4)
    Hi. I am part of a development team working in PHP and MySQL. The site is using SSL, and users
    have to log to use the site. When users log in, their important details are retrieved from the
    database and stored in session variables (functionality in the site is permissions specific).
    Lately, sessions are "disappearing" for no apparent reason. Users will log in, and at some point
    (the length of time will vary unpredicatably) the sessions will lose their value (the variables are
    empty) and this causes the site to evict the user. This is very frustrating for the user b...
  11. Converting Characters In A Variable To Individual Values In An Array - turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ...
  12. Suppressing Mysql Error In Php - How to suppress auto generated mysql err (4)
    I am testing my website on my local machine. It is still in development stage and I am using
    php/mysql. If there is a mysql connection error, I am checking for the connection variable and
    displaying a proper error message. But even before it displays my error message, it displays an
    error message on browser thrown by mysql (MySQL Connection Failed: Access denied for user). Can I
    suppress this message and display only my message?...
  13. Session Start() Problem - Need help with an error (5)
    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started
    at /home/ridouan/public_html/Reviews.php:5) in /home/ridouan/public_html/Reviews.php on line 6
    Can't seem to fix the error (im a php noob) So i'd appreciate some help /smile.gif'
    border='0' style='vertical-align:middle' alt='smile.gif' /> If you need it: Gamer Online then
    click on the Reviews link...
  14. How Long Do $_post Variables Stay Stored? - (6)
    let's say that for whatever reason, let's say for a survey, i want to make a quiz like
    w3schools.com has, only with PHP. i would want to put all the questions on different pages, also
    like w3schools. could each of the submissions stay stored inside PHP's $_POST for 20
    pages? if not, how could i get around it? PS. i really don't know anything about ASP other than
    the fact that w3schools uses it. so please don't explain to me with ASP-specific terms......
  15. Quick Question About Session Vars - something I can't find anywhere (1)
    Can you save stuff like classes and arrays in sessionsvariables?...



Looking for storing, session, variables, generated, dynamically

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for storing, session, variables, generated, dynamically

*MORE FROM TRAP17.COM*
advertisement



Storing Session Variables Generated Dynamically



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE