Store
Community Documentation

v3 Knowledgebase

Accessing php library functions from outside of phpfox.

The question was asked about how do I tell if a user is logged into phpfox from an external php file. This might be useful if you have a separate program for your site users that you want to restrict to only those who are signed in to phpfox. It may be too difficult or time consuming to incorporate that code into a phpfox module, which would be the best way.

There may be ways to do this with instantiating a new database connection and checking sessions logs and cookies and seeing if they are still active. To me, this sounded like way too much work. The thing is, that info is easy to get to from within phpfox. You simply ask Phpfox::isUser()? But to ask that, you have to be inside phpfox. Or do you?

I looked at the start up process for phpfox. It starts with the index.php file in the root, which does a few things. It defines some constants, requires the init.inc.php file and then runs phpfox. The init.inc.php file sets up the library for phpfox to use. So I wondered what would happen if I set up the library but didn't runn phpfox?

I started out with this code:
PHP:
define('PHPFOX'true);
define('PHPFOX_DS'DIRECTORY_SEPARATOR);
define('PHPFOX_DIR'dirname(__FILE__) . PHPFOX_DS);
define('PHPFOX_START_TIME'array_sum(explode(' 'microtime())));
require_once(
PHPFOX_DIR 'include' PHPFOX_DS 'init.inc.php'); 


This is just the bare bones version of what the index.php file does without the Phpfox::run().

Then, in order to see if this worked, I added some stuff to see if I could get some kind of output. This is what it looked like:

PHP:
echo "This is the beginning.";
define('PHPFOX'true);
define('PHPFOX_DS'DIRECTORY_SEPARATOR);
define('PHPFOX_DIR'dirname(__FILE__) . PHPFOX_DS);
define('PHPFOX_START_TIME'array_sum(explode(' 'microtime())));
require_once(
PHPFOX_DIR 'include' PHPFOX_DS 'init.inc.php');

if (
checkStatus())
{
    echo 
"Logged In";
}
else
{
    echo 
"Logged Out!";
}

echo 
"This is the end";

function 
checkStatus()
{
    return 
Phpfox::isUser();


With this, I was able to tell if a user was logged into phpfox.

It occurred to me that it would be possible to do other things as well. For example, if I wanted to list the first five members of phpfox, I could do this:
PHP:
<head>
<style>
.block_listing_image {
    float: left;
}
.block_listing_title {
}
.clear {
    clear: both;
}
li {
    padding-bottom: 10px;
    position: relative;
}
</style>
</head>
<body>

<?php
echo "<p>This is the beginning.</p>";
define('PHPFOX'true);
define('PHPFOX_DS'DIRECTORY_SEPARATOR);
define('PHPFOX_DIR'dirname(__FILE__) . PHPFOX_DS);
define('PHPFOX_START_TIME'array_sum(explode(' 'microtime())));
require_once(
PHPFOX_DIR 'include' PHPFOX_DS 'init.inc.php');

if (
checkStatus())
{
    echo 
"<p>Logged In</p>";

    
$aFive=Phpfox::getLib('database')->select('full_name, user_image, user_name')
            ->
from(Phpfox::getT('user'), 'u')
            ->
limit(5)
            ->
execute('getSlaveRows'); 

    echo 
'<p>The first five users of this site are ... </p><ol>';

    foreach(
$aFive as $aOne)
    {
        
$sTemp Phpfox::getLib('image.helper')->display(array('user' => $aOne,'suffix' => '_50','max_width' => 50,'max_height' => 50));
        echo 
'    <li>
        <div class="block_listing_image">' 

            
$sTemp 
        
'</div>
        <div class="block_listing_title" style="padding-left:56px;">' 

            
$aOne['full_name'] . 
        
'</div>
        <div class="clear"></div>
    </li>'
;

    }
    echo 
'</ol>';
}
else
{
    echo 
"<p>Logged Out!</p>";
}


echo 
"<p>This is the end</p>";

function 
checkStatus()
{
    return 
Phpfox::isUser();
}

?>
</body>


This was an interesting learning experiment for me and hopefully will be of some use to anybody who needs this capability.

web