php include() not working

  • fional24
  • Graduate
  • Graduate
  • fional24
  • Posts: 125
  • Loc: Scotland

Post 3+ Months Ago

I'm trying a simple example to get php include files to work on my website. The site is hosted by a hosting company, so I have not configured php myself and do not have access to php.ini.

The include file is called 'bob.inc' and looks like this:

Code: [ Select ]
<?php

$user="user";
$host="mysql-02.host.co.uk";
$pass="password";
$database="db";

?>
  1. <?php
  2. $user="user";
  3. $host="mysql-02.host.co.uk";
  4. $pass="password";
  5. $database="db";
  6. ?>


The file i'm trying to include this in is called 'includetest.php' and is sitting in the same folder as bob.inc (the public folder) on the server. This script is meant to connect to the database, using the login details from bob.inc, then print the words 'inlude worked' on screen. All I get is an error saying

"Warning: mysql_connect(): Access denied for user: 'user@host.co.uk' (Using password: NO) in /home/1111/myspace/www.mydomain.com/public_html/includetest.php on line 5
couldnt connect to server

Code:

Code: [ Select ]
<?php

include("bob.inc");

$connection = mysql_connect($host, $user, $password) or die ("couldnt connect to server");
$db = mysql_select_db($database, $connection) or die ("couldnt connect to database");

echo("include worked");

?>
  1. <?php
  2. include("bob.inc");
  3. $connection = mysql_connect($host, $user, $password) or die ("couldnt connect to server");
  4. $db = mysql_select_db($database, $connection) or die ("couldnt connect to database");
  5. echo("include worked");
  6. ?>


As i'm new to php I have no idea if there is anything obviously wrong. I tried includes on another file yesterday and got an error saying it couldn't open the include file. The login information is correct (i have checked it about 40 times already!)

Thanks,
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

try this code

PHP Code: [ Select ]
 
<?php
 
 
 
include('bob.inc');
 
 
 
$connection = mysql_connect($host, $user, $password) or die ("couldnt connect to server");
 
$db = mysql_select_db($database, $connection) or die ("couldnt connect to database");
 
 
 
echo('include worked');
 
 
 
?>
  1.  
  2. <?php
  3.  
  4.  
  5.  
  6. include('bob.inc');
  7.  
  8.  
  9.  
  10. $connection = mysql_connect($host, $user, $password) or die ("couldnt connect to server");
  11.  
  12. $db = mysql_select_db($database, $connection) or die ("couldnt connect to database");
  13.  
  14.  
  15.  
  16. echo('include worked');
  17.  
  18.  
  19.  
  20. ?>


you had double quotes in your include. its single quotes
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Double quotes don't make a difference I use them all the time for includes.

Right, now to the problem. If an include statement says that it can't open the file, in all likely hood there is something wrong with the include path.

Now to fix this is relatively easy.

First, makes sure that bob.inc is in the same directory as the php file you are executing.

if for some reason you need it in a folder then your include statement should be
PHP Code: [ Select ]
 
include("folder_im_in/bob.inc");
 
 
  1.  
  2. include("folder_im_in/bob.inc");
  3.  
  4.  


if the script is in a directory then you need to escape the directory and find the include
PHP Code: [ Select ]
 
include("../bob.inc");
 
//or if bob is in another folder
 
include("../folder_im_in/bob.inc");
 
 
  1.  
  2. include("../bob.inc");
  3.  
  4. //or if bob is in another folder
  5.  
  6. include("../folder_im_in/bob.inc");
  7.  
  8.  


So the moral of the story is, make sure the include statement has the correct path the file :)
  • fional24
  • Graduate
  • Graduate
  • fional24
  • Posts: 125
  • Loc: Scotland

Post 3+ Months Ago

Thanks guys,

It's now working but I don't know why! I made a couple of changes, I put the single quotes in (just to see!) and took the spaces out of the mysql_connect(). I also renamed bob.inc to bob.php and it is connecting fine now.

Is it possible that there could be something set up on my host's php that would cause any of these changes to make a difference?

New code is:

Code: [ Select ]
<?php

include('bob.php');

$connection = mysql_connect($host,$user,$password) or die ("couldnt connect to server");
$db = mysql_select_db($database,$connection) or die ("couldnt connect to database");

echo("include actually working. WooHoo!");

?>
  1. <?php
  2. include('bob.php');
  3. $connection = mysql_connect($host,$user,$password) or die ("couldnt connect to server");
  4. $db = mysql_select_db($database,$connection) or die ("couldnt connect to database");
  5. echo("include actually working. WooHoo!");
  6. ?>
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Hmmm well did you test with every change you made?

Maybe they have some sort of access issue with .inc.

Was the file orginally called bob.inc or bob.php?
  • fional24
  • Graduate
  • Graduate
  • fional24
  • Posts: 125
  • Loc: Scotland

Post 3+ Months Ago

Haven't tested every change yet, but I will. File was originally called bob.inc.
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

Looks to me that it was working properly in the first place. The reason the mysql connection was failing was because you had your password var defined as $pass in bob.inc but were referencing it as $password in your main script. In other words, you were trying to connect to mysql with out a password. The fact that the mysql error shows "Access denied for user: 'user@host.co.uk' (Using password: NO)" confirms this.
  • fional24
  • Graduate
  • Graduate
  • fional24
  • Posts: 125
  • Loc: Scotland

Post 3+ Months Ago

Quote:
Looks to me that it was working properly in the first place.


Your're right! I tried it again(!) with a few different include files and it works with the format:

Code: [ Select ]
include("file.inc");


Just as it is supposed to.

I've got a bit of recoding to do now...... :(

Thanks guys for all your help, I think I might actually sleep tonight now.

I'm working on a booking system for work and it's proving to be a lot more of a headache than I thought it would be. It's always these little bugs that waste so much developing time and cause so much stress!

:D
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

another thing to bear in mind is that it is generally bad practice to use .inc as an extension for include files, unless you have them set up to be parsed by php. If not, their source can be viewed in a browser, and you definitely wouldn't want people looking at your db config file.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Hey wheredid that mysql error come from? Wasn't there originally
  • fional24
  • Graduate
  • Graduate
  • fional24
  • Posts: 125
  • Loc: Scotland

Post 3+ Months Ago

The error was there from the start! Although it was stuck between the two bits of code, maybe I should have put it at the end.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Cool, maybe I should just learn how to read :)
  • Tdotwire
  • Proficient
  • Proficient
  • User avatar
  • Posts: 486
  • Loc: Toronto

Post 3+ Months Ago

ScienceOfSpock wrote:
another thing to bear in mind is that it is generally bad practice to use .inc as an extension for include files, unless you have them set up to be parsed by php. If not, their source can be viewed in a browser, and you definitely wouldn't want people looking at your db config file.


what alternative would you sugest to using .inc I use regular php files for all of my db files, what do you use.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Go with something like bob.inc.php.

The problem with inc is that the webserver dishes it up as a plain text file which means people can view everything in it. A php file however will get processed and nothing (unless specified) will be displayed
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

Rabid Dog wrote:
Go with something like bob.inc.php.

The problem with inc is that the webserver dishes it up as a plain text file which means people can view everything in it. A php file however will get processed and nothing (unless specified) will be displayed


As RD said, anything with a .inc extension is treated as a plain text file by apache (unless you have it set to treat it otherwise) What this means is, if your site is http://www.mysite.com and your database config file is db_config.inc, I can view it (your db username and password, and other connection info) by browsing to http://www.mysite.com/db_config.inc. If your db_config file was named db_config.php, that wouldn't happen.
  • SSH-Raj
  • Expert
  • Expert
  • User avatar
  • Posts: 588

Post 3+ Months Ago

file.inc.php :D
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

The way I usually do it is something like

file.inc.php -> for standard includes
file.ini.php -> config settings
file.class.php -> well php classes

Post Information

  • Total Posts in this topic: 17 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.