Jump to content

php "require" function not working properly


shah

Recommended Posts

Hi,

I have a little problem with php require("header.php"). i have two php file 1) config.php and 2) header.php in includes folder. I have another Stylesheet file stylesheet.css in a folder called CSS. in the header.php file, i have used require'config.php' and linked to the style sheet through href="../CSS/stylesheet.css". Now when i have a file index.php in the parent directory and used require("./includes/header.php"), then the CSS file format of stylesheet.css and the variable of config.php are not displayed although i have included the header file in the index.php. When i investigated the issue, i have found out that when i change the include path of the header file as if it was also in the parent directory, then it works. that is when i change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css" then it works. as you can see, logically, the path of require should be accroding to the header.php file which is in the includes directory , but actually it is accepting the path according to the index.php file which is in the parent directory. this is actually a pain for me, because i am using the header and footer files for reusablity purpose, but it is asking me to change the path according to the caller file. I will appreciate if some one could help me. i have searched a lot on this but could not find anything. i can re explain with example if you can not understand what i mentioned above

 

BR

Syed Arif Ullah Shah

Link to comment
Share on other sites

I think the simplest solution would be to always use an absolute path when including files

 

i.e.

include "/CSS/stylesheet.css";

instead of

include "../CSS/stylesheet.css";

 

that way you dont have to worry about the folder location of the page that is referencing this include file

 

if that doesn't work then as Ken said, post your code and include a simple diagram of what your current folder structure looks like

Link to comment
Share on other sites

Okay, here is a snapshot of my code.........

 

before posting the code, let me give you the folder structure again....

 

                        Parent Directory (index.php)

                                  -

                                  -

                                  -

                                  -

                      - - - - - - - - - - - - - - - - - - - - - - -

                      -                                                  - 

                      -                                                  -

                Includes                                            CSS

(config.php,header.php,footer.php)                  (stylesheet.css)

 

 

The stylesheet.css simple contains the CSS layout which could be used in other files.

 

The code of config.php is given below (according to the folder strcuture given above)

 

<?php

    $dbhost = "localhost";

    $dbuser = "root";

    $dbpassword = "";

    $dbdatabase = "blogtastic";

    $config_blogname = "MC's Home";

    $config_author = "Syed Arif Ullah Shah";

    $config_basedir = "http://127.0.0.1/sites/Myblog/";

?>

 

the code of the header.php is given below (according to the folder structure)

 

<?php

require_once'config.php';

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title><?php echo $config_blogname; ?></title>

<link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" />

</head>

<body>

<div id="header">

<h1><?php echo $config_blogname; ?></h1>

[<a href="index.php">home</a>]

</div>

<div id="main">

 

Since config.php is in the same folder, therefore i use require 'config.php'.

Since stylesheet.css is in the CSS folder which is in the parent folder, i have to go one step above and then go to the CSS folder so i used

"../CSS/stylesheet.css". If you know any other method to do so, kindly mention.

 

When i run the header.php, it works absolutely fine. It displays the variables of config.php and uses the theme of stylesheet.css.

 

Now comes the problem. in the parent directory, i have the index.php. its code is given below

 

<?php

require_once("./includes/header.php");

?>

 

It uses the code of header.php, but theme style of stylesheet.css and variables of config.php which were used in header.php using the "require" is not used here anymore. for this purpose, i have to change the paths of files used in the header.php.

 

change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css". as i have already mentioned, although the header.php is in the includes folder, but for it to be used in the index.php, we have to change the paths according to the index.php.

 

I hope i have explained my problem. Kindly help me now.

 

Kind Regards

Syed Arif Ullah Shah

 

Link to comment
Share on other sites

Okay, here is a snapshot of my code.........

 

before posting the code, let me give you the folder structure again....

 

                        Parent Directory (index.php)

                                  -

                                  -

                                  -

                                  -

                      - - - - - - - - - - - - - - - - - - - - - - -

                      -                                                  -

                      -                                                  -

                Includes                                            CSS

(config.php,header.php,footer.php)                  (stylesheet.css)

 

 

The stylesheet.css simple contains the CSS layout which could be used in other files.

 

The code of config.php is given below (according to the folder strcuture given above)

 

<?php
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpassword = "";
    $dbdatabase = "blogtastic";
    $config_blogname = "MC's Home";
    $config_author = "Syed Arif Ullah Shah";
    $config_basedir = "http://127.0.0.1/sites/Myblog/";
?>

 

the code of the header.php is given below (according to the folder structure)

 

<?php
require_once'config.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $config_blogname; ?></title>
<link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" />
</head>
<body>
<div id="header">
<h1><?php echo $config_blogname; ?></h1>
[<a href="index.php">home</a>]
</div>
<div id="main">

 

Since config.php is in the same folder, therefore i use require 'config.php'.

Since stylesheet.css is in the CSS folder which is in the parent folder, i have to go one step above and then go to the CSS folder so i used

"../CSS/stylesheet.css". If you know any other method to do so, kindly mention.

 

When i run the header.php, it works absolutely fine. It displays the variables of config.php and uses the theme of stylesheet.css.

 

Now comes the problem. in the parent directory, i have the index.php. its code is given below

 

<?php
require_once("./includes/header.php");
?>

 

It uses the code of header.php, but theme style of stylesheet.css and variables of config.php which were used in header.php using the "require" is not used here anymore. for this purpose, i have to change the paths of files used in the header.php.

 

change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css". as i have already mentioned, although the header.php is in the includes folder, but for it to be used in the index.php, we have to change the paths according to the index.php.

 

I hope i have explained my problem. Kindly help me now.

 

Kind Regards

Syed Arif Ullah Shah

Link to comment
Share on other sites

Guys,

i am still waiting for a solid solution. please reply asap as my deadline is drawing near. every thing is complete. i am only having this issue

sorry, didn't know we were on a deadline with you .. you can only expect so much when it's free.

 

i've never seen such over-complication of such a simple task.  at least the run-on paragraphs make it seem over-complicated.

 

personally, i use an absolute path back to the document root on the server, that way, no matter where the script is being processed it can read any included file no problem.

 

to find your document root open your phpinfo file on the server/browser and under Environment you'll see it.

 

give that a go.

Link to comment
Share on other sites

yeah i know you are not obliged to offer me a solution.  Actually the person for whom i am doing it, wants relative paths so that it is usable anywhere, without changing the paths. It will also increase my knowledge to know how to use relative path in re-usable code. We are not using absolute path here. Once again i apologize if any of my statement offended you. It was not intentional.

Link to comment
Share on other sites

you can't use "../CSS/stylesheet.css" as header.php will load into index.php and then when the browser sees the link tag and calls the stylesheet, the path will be wrong as the browser is in the parent directory and not includes.

 

I tested all your code and just changed "../CSS/stylesheet.css" to "./CSS/stylesheet.css" and works fine.

Link to comment
Share on other sites

yeah i know you are not obliged to offer me a solution.  Actually the person for whom i am doing it, wants relative paths so that it is usable anywhere, without changing the paths. It will also increase my knowledge to know how to use relative path in re-usable code. We are not using absolute path here. Once again i apologize if any of my statement offended you. It was not intentional.

i take nothing personally on the internet. no worries.

 

the only way to guarantee re-usability is to point to your root directory, so like stated above, "./directory/file.file" will work no matter where on the site you are.

 

"../directory/file.file" will not as that tells the browser to find the file in question one directory below the current directory.

 

so, if you were browsing www.your-site.com/directory/1/2/3/4/index.php, calling for "../directory/file.file" will tell the browser to look in folder '3'.

 

hope that helps if you didn't already know that.

Link to comment
Share on other sites

So will  "./css/stylesheet.css" work if i am calling it from any subdirectories which is not in the parent directory??

you can't use "../CSS/stylesheet.css" as header.php will load into index.php and then when the browser sees the link tag and calls the stylesheet, the path will be wrong as the browser is in the parent directory and not includes.

 

I tested all your code and just changed "../CSS/stylesheet.css" to "./CSS/stylesheet.css" and works fine.

Link to comment
Share on other sites

the problem here is that php function include and the html in header.php are treated differently.

 

If you look at your header file

<?php
require_once'config.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $config_blogname; ?></title>
<link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" />
</head>
<body>
<div id="header">
<h1><?php echo $config_blogname; ?></h1>
[<a href="index.php">home</a>]
</div>
<div id="main">

 

The php engine is only evaluating the php tags which is the include which operates from the directory in which the file calls if from (ie. includes directory b/c that's where header.php is). Nothing is done with the link tag to the css file.

 

Now when you include the header file in the index that html in the header file is just passed into the index file and not evaluation. The browser will evaluate that html and see the link tag and then make a call for the css file. Browser is operating from the index dir so that is your relative dir, not includes.

 

So to answer your question

So will  "./css/stylesheet.css" work if i am calling it from any subdirectories which is not in the parent directory??

That will work if you include header.php in any file that is in your index dir.

 

I'm not sure if all of the above is true with how the php engine and browser operates but the underlying idea is. Hope this makes sense.

 

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.