Quick question: session variables and functions (PHP)

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
[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:

dickey

New Member
Messages
128
Reaction score
0
Points
0
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?
 

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
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...
 
Top