+ Reply to Thread
Results 1 to 3 of 3

Thread: Quick question: session variables and functions (PHP)

  1. #1
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    [Closed] session variables and functions (PHP)

    Hi.
    My main_file.php calls functions.php. I'd like to declare a session variable in functions.php (in one particular function, that is) and use it in main_file.php. I can't return it since I'm already returning another function (so why the hell did they think we would always need to return only one variable anyway??).

    My first attempt gave an error like "Unexpected '[' on line whatever" when I tried to make it global. Fine, how am I suppose to declare a session variable if I don't use a '['? I also tried without making it a global variable (since a session variable should be pretty much global anyway), as well as with and without a session_start() in my functions.php. The best result I got was my page echoing "f" when asked for $_SESSION[my_variable], which I assume means false.

    So my second attempt was to bypass that, make a global $my_variable and set it as $my_variable = $_SESSION[my_variable]. I'm explaining it in short of course, it's not my actual code. The result was, well, that it was simply ignored by my main_file.php. In my face I guess.

    So, put very short and simple, how am I supposed to create a session variable inside my function?
    Last edited by Salvatos; 10-08-2008 at 11:56 PM.

  2. #2
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: Quick question: session variables and functions (PHP)

    I'm not sure I understand well. But I want to ask if you used
    Code:
    include "functions.php"
    in your main_file.php?

    and if your "functions.php" has
    Code:
    function fn1()
    {
      return $fn_ret_1;
    }
    function fn2()
    {
      return $fn_ret_2;
    }
    if so there shouldn't be any problems.

    With regards to session variables... did you put a session_start(); in the main_file.php?
    if so just use;
    Code:
     $_SESSION['session_variable_name']=value1;
    if not you might want to try:
    Code:
      $register_me_pls = "I should be registered as a session variable";
      session_register("register_me_pls");
    to use the registered vars.
    Code:
      print  $_SESSION['register_me_pls'];
    Is this what you need?

  3. #3
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: Quick question: session variables and functions (PHP)

    Oh... I'm embarrassed now. I finally figured out it was just a misuse of explode().
    My code looked like this:

    Code:
    $var = explode($stuff);
    $var = $var[0];
    $_SESSION[var] = $var[1];
    I wanted to save a variable by using $var twice but obviously $var[1] didn't exist anymore so that explains why the session returned "f".

    Sorry for the useless thread, a few credits have been sent for your time :happysad:


    We still can't close our threads? Well then, if a mod would be so kind as to do so...

+ Reply to Thread

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. Places to learn php
    By JaWasabi in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  5. Pregunta sobre las Configuraciones PHP
    By Trevelin in forum Soporte
    Replies: 11
    Last Post: 04-28-2008, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers