1

login.php

$yetkili = new YETKILILER();
$yetkili->YETKILI_TC=$YETKILI_TC;
$yetkili->YETKILI_SIFRE=$YETKILI_SIFRE;
$_void = $yetkili->Giris();
$_SESSION['YETKILI'] = $yetkili;

index.php

$yet =  $_SESSION['YETKILI'];
$tc= $yet->YETKILI_TC;
// Output $tc =null :/

Hello everyone. I'm trying to set up a class inside the sessions from php and request it from other pages but when I try to do that, I only get null. Could someone help me please? Thank you.

img1

img2

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1
    Possible duplicate of [How can I store object class into a session in PHP?](http://stackoverflow.com/questions/5578679/how-can-i-store-object-class-into-a-session-in-php) – MrD Apr 17 '17 at 11:18

5 Answers5

1

Can you give a code piece about your Giris() method. You need to set your session on this method. Then get session variables by session_start method.

Have a nice day.

burak1colak
  • 156
  • 1
  • 7
1

a possible explanation: before you get the YETKILI object back from the session with $yet = $_SESSION['YETKILI']; you must ensure that the class definition for YETKILILER is available. i.e. in index.php, make sure you have used include or require on the file that holds the YETKILI class.

ghatzhat
  • 66
  • 7
1

Solution login.php

   $yetkili = new YETKILILER();
   $yetkili->YETKILI_TC=$YETKILI_TC;
   $yetkili->YETKILI_SIFRE=$YETKILI_SIFRE;
   $_SESSION['YETKILI'] = serialize($yetkili); 

index.php

     $yetkili =  unserialize($_SESSION['YETKILI']);
     $test = $yetkili->YETKILI_ID;
1

Solution2 login.php

$yetkili = new YETKILILER();
$yetkili->YETKILI_TC=$YETKILI_TC;
$yetkili->YETKILI_SIFRE=$YETKILI_SIFRE;
$_SESSION['YETKILI'] = serialize($yetkili); 

index.php

 $yetkili =  unserialize($_SESSION['YETKILI']);
 $test = $yetkili->YETKILI_ID;
0

You will need to include your class in the pages where you are accessing the object via the session. I suggest you create a separate file, eg class_lib.php, where you define all your classes and then use include or require in the pages that use it.

Also, you will need to serialize your object before assigning it to a session and then unserialize it on the other end.

EDIT: Also, you will need session_start(); in both pages or in a page that they both include.

MrD
  • 4,986
  • 11
  • 48
  • 90