Jump to content

[SOLVED] Class object from an include


xiaix

Recommended Posts

Here's the situation:

 

www/config/config.php

<?php
// SITE_ROOT has to be set first
define("SITE_ROOT", $_SERVER['DOCUMENT_ROOT']);

// Class paths
define("DATABASE_CLASS", SITE_ROOT . '/classes/database.class.php');
?>

 

www/index.php

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/config/config.php");
include(DATABASE_CLASS);
?>

// Blah blah blah, some html, then...

<?php include("onlineList.php"); ?>

 

www/onlineList.php

<?php
$db = new database();
$db->dbConnect();
$dbQueryResult = $db->dbQuery("SELECT * FROM characters WHERE loggedIn='1'", $db->dbConID);
$dbRowResult = $db->dbGetRow($dbQueryResult);
?>

 

 

And here's the error:

 

PHP Fatal error:  Class 'database' not found in C:\\Apache\\htdocs\\www\\onlineList.php on line 3, referer: http://www.mywebsite.com/

 

 

Yes, my database class is fine.  In fact, if I do this:

<?php
include('classes/database.class.php'); // <---- SHOULD NOT HAVE TO HAVE THIS
$db = new database();
$db->dbConnect();
$dbQueryResult = $db->dbQuery("SELECT * FROM characters WHERE loggedIn='1'", $db->dbConID);
$dbRowResult = $db->dbGetRow($dbQueryResult);
?>

 

... then everything works fine.

 

 

I would imagine it's a scope issue, but I just don't see why I have to add that include in my onlineList.php file, when it should have been taken care of it it's parent.

 

Any suggestions as to what I'm doing wrong?

Link to comment
Share on other sites

That all works for me. Do you have error_reporting set to E_ALL so that php will tell you why the include is not working?

 

I'm logging my errors.

 

This is the error:

 

  PHP Fatal error:  Class 'database' not found in C:\\Apache\\htdocs\\www\\onlineList.php on line 3, referer: http://www.mywebsite.com/

 

That is the only information that PHP is giving me.

Link to comment
Share on other sites

That all works for me. Do you have error_reporting set to E_ALL so that php will tell you why the include is not working?

 

 

Okay, it does in fact work if I had the following:

 

www/index.php

<?php
   include($_SERVER['DOCUMENT_ROOT'] . "/config/config.php");
   include(DATABASE_CLASS);
?>

   // Blah blah blah, some html, then...
   
   <?php include("onlineList.php"); ?>

 

However, I do not.  I apologize for not being clear.

 

 

Here is what I have:

 

www/index.php

<?php
   include($_SERVER['DOCUMENT_ROOT'] . "/config/config.php");
   include(DATABASE_CLASS);
?>
<SCRIPT src='scripts/testScript.js' type='text/javascript'></SCRIPT>
<SCRIPT LANGUAGE="JavaScript"> function startMe(file) { setTimeout("submitWhosOnline('"+file+"')", 0); } </SCRIPT>

   // Blah blah blah, some html, then...
   
   <DIV id="putItHere">   
   	<script language="JavaScript"> startMe("onlineList.php"); </script>   
   </DIV>

 

 

www/scripts/testScript.js

var xhr = false;
var outMsg;
var urlFile;

function submitWhosOnline(file) 
{
urlFile = file;
if (window.XMLHttpRequest)
{
	xhr = new XMLHttpRequest();
}
else
{
	if (window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e) { xhr = false; alert("There is no support for XMLHttpRequest with this browser."); }
	}
}

if (xhr)
{
	xhr.onreadystatechange = showContents;
	xhr.open('GET', urlFile, true);
	xhr.send(null);
}
else
{
	alert("Sorry, but I couldn\'t create an XMLHttpRequest");
}
}

function showContents()
{
if (xhr.readyState == 4)
{
	if (xhr.status == 200)
	{
		outMsg = xhr.responseText;
		setTimeout("submitWhosOnline('"+urlFile+"')", 5000)
	}
	else
	{		
		outMsg = "There was a problem with the request " + xhr.status;
	}
}

if (window.document.getElementById("putItHere") != null)
{
	window.document.getElementById("putItHere").innerHTML = outMsg;
}
}

 

Link to comment
Share on other sites

It's not working because your AJAX/Javascript is requesting onlineList.php through a http request. That instance of onlineList.php is completely isolated in a separate process on the web server, the same as if you had browsed directly to onlineList.php. onlineList.php needs to be self contained and it would need to include any files it needs to operate.

Link to comment
Share on other sites

It's not working because your AJAX/Javascript is requesting onlineList.php through a http request. That instance of onlineList.php is completely isolated in a separate process on the web server, the same as if you had browsed directly to onlineList.php. onlineList.php needs to be self contained and it would need to include any files it needs to operate.

 

Ah, okay.  Completely understandable.  I did not realize that the httprequest instance was it's own separate process that did not tie into it's caller. 

 

Thank you very much for pointing that out.  :)

Link to comment
Share on other sites

Here is a bit more information on the subject.

 

Web servers are stateless. They don't know (except for entries in the access log) or care what page(s) were requested at any time in the past. All resources used on the server for any page are destroyed when the server finishes serving that page (and even things like persistent database connections only work in very specific server configurations.) By the time the browser is rendering the markup/javascript on your main page and it makes the http request for onlineList.php, the php code on your main page would have long been finished executing and all the resources it used would have been destroyed.

 

The only relationship that exists between any two http requests is in the information that the browser provides when it makes those http requests, i.e. the actual URL, and any POST, GET, and COOKIE (including session id cookie) values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.