user1411084
user1411084

Reputation: 512

Session creation with php

I have a problem ,

I have a log in page that check user login and when a user has logged successfully , I start a new session :

$loggedIn = new loggedIn();
                    $loggedIn->email = $userdetails["email"];
                    $loggedIn->displayname = $userdetails["display_name"];
                    $loggedIn->username = $userdetails["user_name"];


                                        var_dump($loggedIn->username);

                    //Update last sign in
                                        session_start();
                    $_SESSION["user"] = $loggedIn;

                                        $kk =  $_SESSION["user"];
                                        var_dump($kk->username);

it shows succsessul session creation , but when I go to other page or move back to this page , I have no session :

isset($_SESSION["user"])

why ?

Upvotes: 0

Views: 205

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

In every page you have to insert session_start() if you have to use it if you want to retrieve data from session. session_start() must be called before outputing anything to the browser.

After you can use your function and retrieve information. If you want to see all the data into your session that you have stored you cand print the array session like this:

var_dump($_SESSION);

Another possible problem can be your class loggedIn.
Have you instantiate this class in the right way?
You can have some errors in that class or something like that.

Upvotes: 0

fimas
fimas

Reputation: 568

You need to have session_start(); on top of every page that you want to use session on. Also make sure that the class loggedIn is defined on every page you need it in.

Upvotes: 1

Related Questions