hive variables being destroyed between page loads - unsure why?

193 views
Skip to first unread message

Ysguy

unread,
Apr 3, 2018, 9:07:28 PM4/3/18
to Fat-Free Framework
Surprisingly i have quite a site built so far and am loving the framework, however i just learned that unless hive values are set at the router page, they can't be set or recalled from other pages. 

Things i've tested

- I have the SQL (MYSQL) session tracking enabled, seems to work.
On the top of each page i call my main classes file and my dbconnection.php file that has the 

$sess=new DB\SQL\Session($db);
$f3->CSRF=$sess->csrf();

in it


So for example

i have 

index.php (router)
files.php (the page that is rendered when you go to /files
controller-files.php (the page that handles ajax queries from files.php)

I have a ton of action going on and that all works. But i've come up with a neat function that needs to store a variable in the hive and then when called differently, retrieve the hive value.


For example.

files.php has ajax that sends a request to 

/controller/files/downloadfile  (/controller/@controllername/@action)

There is a function within controller-files.php that gets passed the @action parameter. 

if ($f3->get('action') == 'downloadfile') //download file chosen
 
{
  $fid
='22';
  $f3
->set('FID',$fid);
  echo
"/controller/files/fetchfile?token=1vs657xz46eax.1mqc8nthaz5c4";
 
}

essentially it returns a path i pass to hidden iframe that is supposed to request a file for download. However, on firing the iframe it goes to the controller-files.php again and checks this code and fails.


if ($f3->get('action') == 'fetchfile')
 
{
 echo $f3
->get('FID'); //results in nothing
 
if ($f3->exists('FID'))
{
        //do something
        }
        else
        {
        echo "FID doesn't exist";
        }
  
}
 

This always returns "FID doesn't exist". meaning that the $f3->set('FID',$fid); no longer exists!


It would seem unless something is assigned in the router (index.php) and passed to the called pages the hive is destroyed!


Could someone explain what needs to happen on each PHP to keep the hive and session alive?I can see my session in the MYSQL table but even writing to the session doesn't stick from child pages. i.e. $f3->set('SESSION.TEST','working'); cant get called from another rendered page using $f3->get('SESSION.TEST'); or $f3->exists('SESSION');


help!

Ysguy

unread,
Apr 3, 2018, 9:33:08 PM4/3/18
to f3-fra...@googlegroups.com
Update: It appears that if i set any hive values (or modify the session) using any of the regular php files. i.e. as shown below, a file - template.php is called and then that calls in the files.php (called URL is /dashboard/files) 
So any page called like this seems to work. I've checked the headers and it all seems the same between the controller and the pages...

$f3->route('GET|POST /dashboard/@idx',
 
function($f3)
 
{
 $f3
->set('js','/'.$f3->get("PARAMS.idx").'.js');
 $f3
->set('pagetitle',ucfirst($f3->get('PARAMS.idx'))); //Set page name from requested route
 $f3
->set('content','home/'.$f3->get("PARAMS.idx").'.php');
 echo
View::instance()->render('home/template.php');
 
}
 
);
So on the template page i call the "body" php file like so (notice content above)
<?php echo $this->render(Base::instance()->get('content')); ?>

The controllers however, get called like this

// ----  Controller Handler ---
$f3->route('POST|GET /controller/@controller/@action',
function($f3) 
{
if ($f3->get('POST.token') == $f3->get('SESSION.csrf') || $f3->get('GET.token') == $f3->get('SESSION.csrf'))
{
$f3->set('action',explode('&', $f3->get('PARAMS.action'))[0]);
echo View::instance()->render('controller/controller_'.$f3->get("PARAMS.controller").'.php'); 
}
else
{ die("Suspicious Activity Detected!"); }
}
);

So the only difference here is the render is going directly to the controller .php file. All the code works, GET|POST's work etc etc. Except if i define any kind of hive setting or global update it fails miserably... :(. 


Edit: I tried adding a "template.php" to the controllers and call the controller.php from within the template.php like the other files. Nothing. I am at a complete loss. 

ikkez

unread,
Apr 4, 2018, 2:02:29 AM4/4/18
to Fat-Free Framework
the HIVE is not persistent between page loads.. surprise this is completetly normal in PHP world. When you want to store values for a specific user between requests, use a SESSION. BTW: you can also use PHP standard Sessions without the use of a DB Session, if that's what causes the issue for you. Just write/read from SESSION variable in hive.

Ysguy

unread,
Apr 4, 2018, 3:05:12 PM4/4/18
to f3-fra...@googlegroups.com
Thanks for the explanation on the HIVE not being persistent. 

 
Interestingly enough I've narrowed down the problem. It appears that if i comment out ALL my code in the controller and simply have my standard heading and a set for the session it will store it in the session just fine.

 <?php
require_once $_SERVER
["DOCUMENT_ROOT"]."/inc/base.class.php";
$f3
=Base::instance();
$setting
= new SITE_PARAMS;


$_SESSION
['test'] = 'result'.rand(1,100);
?>

Yet when i allow the rest of my code it doesn't set but the code runs fine. As this command runs regardless of the logic further down the file - i'm at a loss as to what is going on....

The main block of code returns JSON output 

echo json_encode($arr);

I guess its time to start commenting blocks of code to isolate the issue.

Ysguy

unread,
Apr 4, 2018, 3:21:08 PM4/4/18
to Fat-Free Framework
SOLVED.... I accidentally (ignorance mostly) had this at the top of my controller code

//Start: Create Mysql connection
$db
= $f3->get('DB');
$db
->begin();


In the rest of my file i never used the rollback() or commit() functions at all. I'm just doing straight $db->exec( calls. It appears that by removing the 

$db->begin();


from my file everything is working now!

Thanks everyone for being my rubber duckie!
Reply all
Reply to author
Forward
0 new messages