0
votes

I using xampp to do some php scripts. To test it local, the path is "http://localhost/shopping".

index.php

<?php include 'view/header.php' ?>
<?php include 'view/home.php' ?>
<?php include 'view/footer.php' ?>

It works ok.

login.php

<?php include 'view/header.php' ?>
<?php include 'view/home.php' ?>
<?php include 'view/footer.php' ?>

then i got error for login.php

Warning: include(view/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\shopping\view\login.php on line 2

Warning: include(): Failed opening 'view/header.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\shopping\view\login.php on line 2

Warning: include(view/home.php): failed to open stream: No such file or directory in C:\xampp\htdocs\shopping\view\login.php on line 3

Warning: include(): Failed opening 'view/home.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\shopping\view\login.php on line 3

Warning: include(view/footer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\shopping\view\login.php on line 4

Warning: include(): Failed opening 'view/footer.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\shopping\view\login.php on line 4

index.php and login.php are in the C:\xampp\htdocs\shopping

header.php, home.php and footer.php are in C:\xampp\htdocs\shopping\view

I searched many posts on SOF. i also tried

$_SERVER['DOCUMENT_ROOT']

But for some reason or another, it doesn't work. I think it's something simple but I just cant get it right.

2
just a possibility, is header.php is header.php and not header.php.txt (if the extension is hidden) - vishwakarma09
yes, its header.php . not txt file - gosulove
did you put here your full index.php and full login.php ? - Gar
The answer to your problem is here : stackoverflow.com/questions/36577020/… - Vic Seedoubleyew

2 Answers

2
votes

include 'view/header.php' doesn't work within login.php because that file is already in the view folder.

change your includes to always use full paths instead. The right path depends on the value of your document root but it's likely one of the following:

include $_SERVER['DOCUMENT_ROOT'].'/view/header.php';

Or

include $_SERVER['DOCUMENT_ROOT'].'/shopping/view/header.php';

This way the same include statement will work from all files. You won't need to change paths based on where you're including from.

1
votes

If you are trying to include from same folder so just use file name

<?php include 'header.php' ?>
<?php include 'home.php' ?>
<?php include 'footer.php' ?>