Include() not including Class() [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Include() not including Class() [Solved]

Post by MikeCXT »

The Error:
Fatal error: Cannot instantiate non-existent class: mysqlclass in E:\aboveroot\user\domain.com\test\postArray\regTest.php on line 5

The Problem:
Using security tips from these forums, I hid the password.inc.php file above the root. I can still connect to mySQL, so that part works. The mysqldb.class.php file I placed in an "includes" folder. It is finding the file, because I had a different error when I had a typo in the name and it was unable to find the file. But it is not finding the class I assume, thats what the error sounds like. Here is the file mysqldb.class.php

Code: Select all

<?php
class MySQLClass
{
    var $dbhost;
    var $dbuser;
    var $dbpass;
    var $dblink;

    function connect($host, $user, $pass)
    {
        $this->dbhost = $host;
        $this->dbuser = $user;
        $this->dbpass = $pass;

        $this->dblink = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
    }
}
?>
This file is then included by the following code (I have removed all non-important text from this file, I plan on adding substanence back once I get it to work):

Code: Select all

<?php
include_once('/aboveroot/user/password.inc.php'); 
include_once('http://www.domain.com/includes/mysqldb.class.php'); 

$db = new MySQLClass();

$db->connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD); 

mysql_close();
?>
I don't understand why the include statement is finding the correct file (assumed due to absense of error of when it was misnamed and looking for a non-existant file therefore), and yet then saying there is no class by that name. I am calling the class name exactly as I named in mysqldb.class.php.

Note: If I put the Class directly into the final code, without using an include statement, the file works. It is only when I remove the Class and place it in an include that it suddenly 'does not exist'. Where did I go wrong?
Last edited by MikeCXT on Thu Jan 19, 2006 2:43 pm, edited 1 time in total.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Try using require_once() instead of include_once(). This will produce a fatal error if the file isn't found.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

include_once('http://www.domain.com/includes/mysqldb.class.php');
This line is returning the output of the PHP script, not the script. Maybe try ".phps"?
(#10850)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Can't include a class with a HTTP wrapper.
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Post by MikeCXT »

Buddha443556 wrote:Can't include a class with a HTTP wrapper.
Then how can I include it if I can not include the full address? I can't touch include_path as this is a shared server? (3rd party runs the server). And everything I have seen about this, tells you to change the include_path. http://www.domain.com/etc/etc.... just produces another set of errors:

Warning: main(http://www.domain.com/includes/mysqldb.class.php): failed to open stream: No such file or directory in E:\aboveroot\user\domain.com\test\postArray\regTest.php on line 3

Warning: main(): Failed opening 'www.domain.com/includes/mysqldb.class.php' for inclusion (include_path='.;c:\php4\pear') in E:\aboveroot\user\domain.com\test\postArray\regTest.php on line 3

Fatal error: Cannot instantiate non-existent class: mysqlclass in E:\aboveroot\user\domain.com\test\postArray\regTest.php on line 5

The top 2 errors are what I get when the file can not be found, so I assume you can't just type www either as it adds it to its current path or something.


Also, I tried using .phps, and I got simliar errors to this, and require_once produced the exact same fatal error as include_once.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Something like ...

Code: Select all

include_once 'E:/aboveroot/user/domain.com/includes/mysqldb.class.php';
or

Code: Select all

include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/mysqldb.class.php';
$_SERVER['DOCUMENT_ROOT'] should work even though it looks like a Windows box.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

MikeCXT wrote:Then how can I include it if I can not include the full address? I can't touch include_path as this is a shared server? (3rd party runs the server).
From the PHP manual's include page:
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix M for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
I would assume that many hosts would turn off that feature. You should read through the manual page to for include(). The relevant settings and security information is all there, plus examples: http://us2.php.net/manual/en/function.include.php
(#10850)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Code: Select all

include_once('/aboveroot/user/password.inc.php');
include_once('http://www.domain.com/includes/mysqldb.class.php');
I wonder why noone has asked him why he doesn't have the mysqldb.class.php in the same path as his web site... IE...

Code: Select all

include_once('/aboveroot/user/password.inc.php');
include_once('includes/mysqldb.class.php');
Even on a virtual host that would pull the file from the right directory as long as the directory that contains his php files has a directory called includes and the mysqldb.class.php file is in that includes directory.

It sure looks like he is trying to overcomplicate the process.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

MikeCXT wrote:Then how can I include it if I can not include the full address? I can't touch include_path as this is a shared server? (3rd party runs the server). And everything I have seen about this, tells you to change the include_path. http://www.domain.com/etc/etc.... just produces another set of errors:
The simplest thing to do is just copy the file "mysqldb.class.php" into the "/aboveroot/user/" directory and then:

Code: Select all

include_once('/aboveroot/user/password.inc.php');
include_once('/aboveroot/user/mysqldb.class.php');
(#10850)
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Solved

Post by MikeCXT »

Code: Select all

include_once('/aboveroot/user/password.inc.php'); 
include_once('/aboveroot/user/mysqldb.class.php');
This worked perfect.

Code: Select all

include_once('/aboveroot/user/domain.com/includes/mysqldb.class.php');
This also worked. I believe I will use this since I can then place the file in my includes directory. I had tried almost the same, but placed a www in front of the domain, not realizing you can't do that.

Code: Select all

include_once('/aboveroot/user/password.inc.php'); 
include_once('includes/mysqldb.class.php');
This does not work since here is the filepath for the Class file. http://www.domain.com/includes/mysqldb.class.php
The filepath for the page using this is: http://www.domain.com/test/postArray/regTest.php
My other includes have never had classes and so I've always used the absolute path (HTTP wrapped).


Thank you to all who helped. After this post and reading more about includes with this information, I understand how they work better. Thanks again.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: Solved

Post by AKA Panama Jack »

MikeCXT wrote:

Code: Select all

include_once('/aboveroot/user/password.inc.php'); 
include_once('includes/mysqldb.class.php');
This does not work since here is the filepath for the Class file. http://www.domain.com/includes/mysqldb.class.php
The filepath for the page using this is: http://www.domain.com/test/postArray/regTest.php
My other includes have never had classes and so I've always used the absolute path (HTTP wrapped).
Actually that WILL work if you use it as I indicated.

You create a directory called includes in the directory where the your web pages are located and place mysqldb.class.php inside that directory. This way you can move the entire web site directory to any other locations and never have to edit your include statements to point to where the includes are stored.

Example...
[root@dumpy testdiredtory]# ls -lR
.:
total 16
drwxr-xr-x 2 root root 4096 Jan 19 15:10 includes
-rw-r--r-- 1 root root 12178 Jan 19 15:10 index.php

./includes:
total 4
-rw-r--r-- 1 root root 2281 Jan 19 15:10 mysqldb.class.php
[root@dumpy testdiredtory]#
When http://www.testsite.com/index.php is called the index.php is loaded and the include_once I listed in my example will load the mysqldb.class.php from the include directory that is in the same directory as the index.php. In this manner you do not need to know the full path to the file.
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Post by MikeCXT »

Well, I believe you since all my normal html links are built for portability in that manner, but I tried it and it produced an error as you had it. I don't know why, nor do I know if the include_path has anything to do with it (which I have no control over). I think I'll try to include a php file that has the include path, so I can just change the one file and have it reflect in all documents. I'll see if it works tomorrow anyways, time to leave work. Thanks for your help, I had forgotten portability in the face of it just not working.
Post Reply