Include Custom PHP on Every Page

JonathanW

Well-known member
Hello,

I just need to know how I can include my functions.php file in every page of my template.

I have my website integrated, and login box and everything, and I need to be able to use my functions if the users is logged in or not to display the right information.

Thanks,
Jonathan
 
Hello,

I just need to know how I can include my functions.php file in every page of my template.

I have my website integrated, and login box and everything, and I need to be able to use my functions if the users is logged in or not to display the right information.

Thanks,
Jonathan

Let me get this right. You need a file to be included in all and every page ?
Say, in the forum home, and the threads list, and the thread detail, and the search page ... etc ?

I guess for that scenario you could just include that in the index.php FrontController
Having whatever variables you provide accessible in the templates would be a whole another thing, of course
 
Hmm yes that's exactly what I need to do...and I will need to get some variables into it...

Any ideas?
 
Any ideas guys? I have done some poking around and can't seem to get anything working. Just end up with lots of errors.

All this is, is the PHP code to run the login bar that goes along with my site that the XF board is integrated into. If the user is logged in, it will show the bar with some of their info, and if they're not logged in it will show login boxes. Simple if statement....I just can't find how to include the files correctly and then call my variables in the template.
 
Ok I have gotten to this point, however, when the code is reached in my functions file that requires an sql_query, I am popped back an error about call to a member function on a non-object blah blah.

Do you know of any ways around this?
 
Scopeproblem;)

It seems that you're using a variable as a object without initialicing it....
What's the code you're using?
 
Do you want my functions file that I'm calling, or the XF code for my "addon" that calls the functions file?
 
Ok I have gotten to this point, however, when the code is reached in my functions file that requires an sql_query, I am popped back an error about call to a member function on a non-object blah blah.

Do you know of any ways around this?

That would be in your code
 
Ok here's my functions.php file that I'm trying to call in.

I know the code is a mess...I haven't got a chance to clean it up yet lol.

Line 144 is where it says it's having the problem...with the sql_query statement.

This works fine in my script, just not when being called inside of XF.

Code:
<?


//skip the functions file if somebody call it directly from the browser.
//if (ereg("functions.php", $_SERVER['SCRIPT_NAME'])) {
//    Header("Location: index.php"); die();
//}

// Initate Sessions!
@session_start();

// Report all errors and ignor notices
error_reporting(E_ALL ^ E_NOTICE);

// Disable magic_quotes_runtime
// deprecated in php >= 5.3
//set_magic_quotes_runtime(0);

if (!ini_get("register_globals")) {
    import_request_variables('GPC');
}

$phpver = phpversion();
if ($phpver < '4.1.0') {
	$_GET = $HTTP_GET_VARS;
	$_POST = $HTTP_POST_VARS;
	$_SERVER = $HTTP_SERVER_VARS;
}
$phpver = explode(".", $phpver);
$phpver = "$phpver[0]$phpver[1]";
if ($phpver >= 41) {
	$PHP_SELF = $_SERVER['PHP_SELF'];
}


if(isset($admin)){
$admin = base64_decode($admin);
$admin = addslashes($admin);
$admin = base64_encode($admin);
}
if(isset($user)){
$user = base64_decode($user);
$user = addslashes($user);
$user = base64_encode($user);
}

foreach ($_GET as $sec_key => $secvalue) {
	if ((eregi("<[^>]*script*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*object*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*iframe*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*applet*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*meta*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*style*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*form*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*img*\"?[^>]*>", $secvalue)) ||
	(eregi("<[^>]*onmouseover*\"?[^>]*>", $secvalue)) ||
	(eregi("\([^>]*\"?[^)]*\)", $secvalue)) ||
	(eregi("\"", $secvalue))) {
		die ("not allowed");
	}
}
foreach ($_POST as $secvalue) {
	if ((eregi("<[^>]*onmouseover*\"?[^>]*>", $secvalue)) ||
        (eregi("<[^>]script*\"?[^>]*>", $secvalue)) ||
        (eregi("<[^>]meta*\"?[^>]*>", $secvalue)) ||
        (eregi("<[^>]style*\"?[^>]*>", $secvalue))) {
		die ("not allowed");
	}
}

//set root path
$ROOT_DIR = realpath(dirname(__FILE__));
$ROOT_DIR = str_replace('\\', '/', $ROOT_DIR);

include ("$ROOT_DIR/config.php");

include("$ROOT_DIR/mysql.class.php");

$db = new sql_db($db_host, $db_username, $db_password, $databse_name, false);
if(!$db->db_connect_id) {
      //if connection to database/login faild, print error.
      echo "<br><font color=\"red\"><h5><br><center>Error:</b><br><hr><br>
            <b>Connection to database has faild!<br>
            check mysql server/database name/username/password </center>
            <br><br><br><br><br><br><br><br><br>";
              echo mysql_error();
      die();
}



//load the site options and info from db.
$options_sql = $db->sql_query("SELECT * FROM ".$prefix."_options");
$options = $db->sql_fetchrow($options_sql);

$site_name = stripslashes($options['site_name']);
$site_email= stripslashes($options['site_email']);
$site_url = stripslashes($options['site_url']);
$site_info = stripslashes($options['site_info']);
$language = stripslashes($options['language']);
$validate = intval($options['validate']);

//load the language
include ("$ROOT_DIR/lang/$language.php");

// SMARTY !!!! :)
require("./libs/smarty/Smarty.class.php");
$smarty = new Smarty();

$smarty->setTemplateDir("templates");
$smarty->setCompileDir('libs/smarty/templates_c');
$smarty->setCacheDir('libs/smarty/cache');
$smarty->setConfigDir('libs/smarty/configs');

$smarty->assign("user_logged_in", is_logged_in($_COOKIE['user']));

$user_information = @mysql_fetch_assoc($db->sql_query("SELECT * FROM `{$prefix}_users` WHERE username='" . get_username() . "';"));

$smarty->assign("current_first_name",$user_information['first_name']);
$smarty->assign("current_last_name",$user_information['last_name']);
$country_code = @country_to_code($user_information['country']);
$smarty->assign("user_country_code", $country_code);
$login_date = date_create($user_information['lastlogin']);
$smarty->assign("lastlogin", date_format($login_date, 'F j, Y'));

function find_userid($username){
	global $db, $prefix;
	$find_userid = mysql_fetch_assoc($db->sql_query("SELECT * FROM `{$prefix}_users` WHERE username='$username';"));
	$found_userid = $find_userid['userid'];
	return($found_userid);
}


//global function for checkig whethar user is logged in or not.
function is_logged_in($user) {
    global $db,$prefix;

    $read_cookie = explode("|", base64_decode($user));
    $userid = addslashes($read_cookie[0]);
    $passwd = $read_cookie[2];
    $userid = intval($userid);
    if ($userid != "" AND $passwd != "") {
        $result = $db->sql_query("SELECT password FROM ".$prefix."_users WHERE userid='$userid'");
	$row = $db->sql_fetchrow($result);
        $pass = $row['password'];
	if($pass == $passwd && $pass != "") {
           return 1;
	}
    }
    return 0;
}

function get_username() {
    	$read_cookie = explode("|", base64_decode($_COOKIE['user']));
    	$user = addslashes($read_cookie[1]);
    	return($user);
}

function get_userid() {
    	$read_cookie = explode("|", base64_decode($_COOKIE['user']));
    	$userid = $read_cookie[0];
    	return($userid);
}

function count_rows($userid){
		global $prefix;
		$result = mysql_query("SELECT userid FROM ".$prefix."_feedback WHERE userid=$userid");
		$total_feedback = mysql_num_rows($result);
		return($total_feedback);
}

function is_logged_in_admin($admin) {
    global $db,$prefix;

    $read_cookie = explode("|", base64_decode($admin));
    $adminid = addslashes($read_cookie[0]);
    $passwd = $read_cookie[2];
    $adminid = intval($adminid);
    if ($adminid != "" AND $passwd != "") {
        $result = $db->sql_query("SELECT password FROM ".$prefix."_admin WHERE adminid='$adminid'");
		$row = $db->sql_fetchrow($result);
        $pass = $row['password'];
		if($pass == $passwd && $pass != "") {
    	       return 1;
		}
    }
    return 0;
}


function msg_redirect($msg,$url,$seconds){
         global $site_name, $site_url;

         echo "<html dir=\""._LTR_RTL."\">\n"
              ."<head>\n"
              ."<title>$site_name</title>\n"
              ."<meta http-equiv=\"Refresh\" content=\"$seconds; URL=$url\">\n"
              ."<meta http-equiv=\"Content-Type\" content=\"text/html; charset="._CHARSET."\">\n"
              ."<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\">\n"
              ."</head>\n\n"
              ."<body>\n"
              ."<br />\n"
              ."<br />\n"
              ."<br />\n"
              ."<br />\n\n\n"
              ."<div align=\"center\">\n"
              ."<table cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"70%\" bgcolor=\"#E1E1E1\">"
              ."<tr>"
	      ."<td bordercolor=\"#808080\">"._REDIRECTING."</td>"
              ."</tr> "
              ."<tr> "
	      ."<td align=\"center\" bgcolor=\"#FFFFFF\">"
	      ."<blockquote> "
              ."<p>&nbsp;</p>"
	      ."<p><h3>$msg</h3></p>"
              ."<p><a href=\"$url\"> "
	      .""._CLICK_HERE_BROWSER_REDIRECT."</a></p><br />"
              ."</blockquote>"
	      ."</div>\n"
	      ."</td>\n"
              ."</tr>\n"
              ."</table>\n\n\n"
              ."</body>\n"
              ."</html>";
}

function user_exists($user) {
    global $db,$prefix;
    $user_check = mysql_fetch_assoc($db->sql_query("SELECT username FROM ".$prefix."_users WHERE username='$user'"));
	if(trim($user_check['username']) == "") {
		return false;
	} else {
		return true;
	}
}
?>
 
That error "call to a member function on a non-object"... sounds familiar. It's mostly down to bad application design/architecture. Are you relying on global variables for storing your objects? vBulletin was similar in this respect, and I could never get it to initialize from inside a function.
 
Well it wouldn't surprise me if my design is bad...I'm still fairly new to PHP and trying to make "good" code.

To be perfectly honest I'm not sure what your asking, but I don't think so?
 
Are you using a different database as to what's already being used? i.e. XF's db?

Code looks ok, Zend Studio doesn't notice any facetime code faults.

This is the code we're looking at now:

PHP:
function is_logged_in($user) {
    global $db,$prefix;

    $read_cookie = explode("|", base64_decode($user));
    $userid = addslashes($read_cookie[0]);
    $passwd = $read_cookie[2];
    $userid = intval($userid);
    if ($userid != "" AND $passwd != "") {
        $result = $db->sql_query("SELECT password FROM ".$prefix."_users WHERE userid='$userid'");
    $row = $db->sql_fetchrow($result);
        $pass = $row['password'];
    if($pass == $passwd && $pass != "") {
           return 1;
    }
    }
    return 0;
}

Specifically:

PHP:
$row = $db->sql_fetchrow($result);

How is the function sql_fetchrow() defined? Is it a simple fetch_array or ..?
 
I have my website integrated, and login box and everything, and I need to be able to use my functions if the users is logged in or not to display the right information.
Sounds poweful.
I'd love to hear more what you are trying to accomplish.
Is this for pages outside the Forums ?
 
Guess I was right. Reliance on global variables by your functions and your script in general.
And using register_globals!
eek.png


Please, please don't spend any more time trying to integrate this script in your XenForo forum.
 
@Shamil,

Yes I'm currently using two databases. I have my scripts database, and then the XF database. They are kept in sync by userid's. I have disabled many features in my forum such as change email address, password, etc, and all of this is manage through my script which runs dual sql queries for most everything. The Login/Logout functions are integrated as well so it starts dual sessions. Pretty much I keep everything in separate databases without the user ever knowing.

sql_fetchrow:
Code:
function sql_fetchrow($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$this->row[$query_id] = @mysql_fetch_array($query_id);
			return $this->row[$query_id];
		}
		else
		{
			return false;
		}
	}

@Digital Doctor,

I have integrated my forums into my site, which if you read above will know that it is also "integrated" function-wise.
In my layout, I have a main login bar in my header, which if the user is not logged in, will display login boxes, and if they are logged in, it will display a few buttons and a logout button, and say welcome *username* etc.

I am simply trying to get this working within the forums, but I'm just having problems calling the PHP to run it.
 
Can you try replacing line 144 with this

PHP:
$row = mysql_fetch_assoc($result);

You might get the same error as before, if you do, then that's fine. Tell me what you get. If memory serve me correct, you don't want to use array, unless you reference it as $row[0], so use assoc

register_globals/GPC is very dangerous - I don't recommend you have/use it.
 
Still the same error. That is supposed to be on line 145, the error is on 144 which is

Code:
$result = $db->sql_query("SELECT password FROM ".$prefix."_users WHERE userid='$userid'");

It does not like the face that sql_query is there.

I did replace mysql_fetchrow with the mysql_fetch_array like you posted, on line 145.
 
Guess I was right. Reliance on global variables by your functions and your script in general.
And using register_globals! :eek:

Please, please don't spend any more time trying to integrate this script in your XenForo forum.

Shadab I am still quite new to the concepts of proper PHP. Can you please post exactly what is wrong with the code, and perhaps post a section of mine, and then correct it and show me what it would look like so I can research it more?

Thanks.
 
Top Bottom