简体   繁体   中英

If i use session_start in function it starts session but doesn't output sessions

The function is called on every possible page, and it's something like main function it checks if user is logedin or not, so it's executed before anything. It just check if user is logedin or have cookie set and if it's logedin or have cookie it starts session and set 2 sessions.

But outputting sessions outside function it outputs that sessions are not set, but shows cookie and session.

To explain a bit more if i echo

$_SESSION['user']
$_SESSION['logedin']

It outputs

Undefined variable: _SESSION

But if i do

print_r($_COOKIE);

It outputs

Array ( [PHPSESSID] => 1rane5lksksp7s4u8p8fh0h194 [user] => fe8dc8f2a7e88746fd8586d489646958 ) 

Which means that both session and cookie are set

But why it shows me Undefined variable when i request a session and also it logout user after first refresh of website.

Is it because session_start is inside a function, if session_start is called inside a function it is called only when that function meets the criteria of else or if statements inside function, and once session_start is called it is remembered in browser or on every page call you need to use session_start to start a session and than you can use sessions that are set. Because if i put session_start outside function it works flawlessly but session is started even if user is not logedin.

A few things:

1) It's better to use require_once('phpfile.php') than require('phpfile.php') . If you require the same file more than once the script will have a fatal error which stops the script from executing. It does the same, except check if the file is already required before.

2) Does it work when session_start() is outside the function? If that's the case just use that.

3) Are you actually setting the session varibales to something?

$_SESSION['user']; //this wont do anything...
$_SESSION['user'] = $user; //This will assign the session variable

4) If it doesn't work, try to to

function userlogin() {
    session_start();
    $_SESSION['user'] = $user;
    $_SESSION['loggedin'] = true;
    session_write_close(); 
} 
session_start();

This enforces that the session variables will be set.

Ok so i got a bit more explanation from the guy who knows php a bit more than me and it seems as a pretty logical explanation.

As you asked from me to post the code it's a quite large and split in few pages but ill try to explain best that i can.

When user goes to my website first page that it's loaded is index.php

In index.php at very first line of code i have code that calls header page.

require('header.php');

And so in header.php file at very first line of code i have code that calls functions page

require('functions.php');

Than in functions file first one is function to check user login.

function userlogin() {
    if(isset($_POST['login'])){
        session_start();
        $_SESSION['user'];
        $_SESSION['logedin'];
        setcookie(stuff);
        and other stuff
    }

}

And than at very first line in header.php i call userlogin() function

So by this logic it should be the very first code that it's called to be executed on website. At least what i thought. But it isn't.

Why?

Well the answer i got is because it's not the very first code in the file, it doesn't matter if i count something that it's executed for the user or just server side check. session_start() must be always at the very first line of code. it doesn't matter how many files i require or include if i put something in between or before session_start() it just won't work. So

index.php -----↴
          header.php -----↴
                     functions.php
                          function userlogin() {              line 1
                            if (isset($_POST['login'])){      line 2
                               session_start();               line 3

Wont work. In other hand:

index.php -----↴
          header.php -----↴
                     functions.php
                               session_start();               line 1

This works.

At least that's what i understood from explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM