Include?

I am including this php file,


<?php include '../db/conn.php'; ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/masterasp/css/style.css">

which works here,
http://fixmysite.us/masterasp/providers/
But doesn’t here,
http://fixmysite.us/masterasp/register.php
I guess its because of this



<?php include '../db/conn.php'; ?>

How do I change the path so its path would vary depending on what directory im in on my server?

Thanks

It looks like you have gone one too many directories up. I presume the “db” folder is inside of the “masterasp” folder? If so, then try the following:


<?php
include './db/conn.php';
include '/db/conn.php'; #should also work
include 'db/conn.php'; #should again work
?>


By deleting one of the front periods, we are now searching for the “db” folder inside of the current directory, rather than heading one directory up into the main domain folder when searching for you “db” folder.

You could also use an absolute path, instead of calculating the whereabouts of the connection file relative to the file system you are working in.

that worked, thanks.

If the . thing looks in the current folder, why does it work when im in another folder?
http://fixmysite.us/masterasp/providers/
(the include is in the /masterasp/inc folder)
(So inside http://fixmysite.us/masterasp/providers/
I have


<?php include '../inc/head.php'; ?>

The code you have given shows that you are looking at the next directory up from the “providers” folder, into “masterasp” (where the “inc” folder exists). Am I missing something?

Try this:


<?php include $_SERVER['DOCUMENT_ROOT'].'/masterasp/db/conn.php'; ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/masterasp/css/style.css">

Also, please note that it’s not the best idea in the world to make files with db credentials etc world viewable; better to put them somewhere above the document root.
If you have to put them there, make sure to add a .htaccess file in the directory that denies direct access via web (php will still be able to read files in that directory no problem)

.htaccess:


deny from all