SESSION variables in functions

Discussion in 'HTML & Website Design' started by highandwild, Feb 24, 2013.

  1. #1
    Whilst I have read some responses to the same question in other forums, I still do not understand this.

    All I want to so is call a function and declare a session variable in that function which is available in the calling code.

    I have file1.php which has session_start and then file2.php is included into this file.
    A function is called from within file2.php and the session variable needs to be available in file1.php.

    I have session_start(); in file2.php too.

    I am sure that this is fair easy, just not clicked with me yet.

    Thanks
     
    highandwild, Feb 24, 2013 IP
  2. PHPascal

    PHPascal Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    You can't do more than one session_start in many file, an easy way to prevent this to apen is to set a $_SESSION var after session start and check if exist

    if (!isset($_SESSION['open_session']))
    {
    session_start();
    $_SESSION['open_session'] = 'yep';
    }
    PHP:
    For your other bug how to retreive info from file2.php in file1.php in use fonction juste return the info you need in your function.

    In file1.php
    $receive_var = function_in_file2(....);

    In file2.php
    function function_in_file2(...)
    {
    return $var;​
    }
     
    PHPascal, Feb 24, 2013 IP
  3. manwink

    manwink Greenhorn

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #3
    I think SESSION thing is a global stuff so it doesn't matter inside or out side functions, but what u do with it will affect to this variable by the order of the code which is executed logically. the function session_id() may be able help if you aren't sure whether your session is already there or not. here you can surf for more info: http://www.php.net/manual/en/function.session-id.php
     
    manwink, Feb 27, 2013 IP
  4. TestingWhiz1

    TestingWhiz1 Member

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    The session is not dependent on any function as it is globally defined.Although if it is possible to call any session through function call, I am also interested to know about it.
     
    TestingWhiz1, Feb 27, 2013 IP