0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
gosulove
  • 1,645
  • 2
  • 26
  • 40

2 Answers2

2

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.

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • no. it doesn't work. i get the same error. it shortage of "shopping" . Warning: include(C:/xampp/htdocs/view/header.php) . If i change to include $_SERVER['DOCUMENT_ROOT'].'/shopping/view/header.php'; <- this one is hard coding. I want soft coding thats working. – gosulove Aug 11 '16 at 13:07
  • 1
    More generally there is a troubleshooting checklist for this problem here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 20 '16 at 08:41
  • What is with hosting when path with $_SERVER['DOCUMENT_ROOT'] doesn't work? It mean it exist in variable and path is made properly but it work only by IP or website name. Is this server configuration? – arrowman Oct 19 '16 at 08:30
  • @arrowman run a php script that just does: `printr($_SERVER)` and examine what is printed. Maybe you will find another array key you can use instead of `DOCUMENT_ROOT` for the same purpose. – BeetleJuice Oct 19 '16 at 15:34
  • @BeetleJuice thanks. But generally I do nothing and it started to work. Provider is changing OS on servers, maybe it is a reason:/ Since one moth there are crazy things with servers... Thank you. – arrowman Oct 21 '16 at 09:06
1

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' ?>
Drone
  • 1,114
  • 1
  • 12
  • 31
  • this way is hard coding. What i want is soft coding. any idea? – gosulove Aug 11 '16 at 13:08
  • Untill you change your folder structure, I think its not hard coding. BTW what do mean by `soft coding` ? – Drone Aug 11 '16 at 13:19
  • when we download those open source and upload to server or test it locahost, we only fill in $db_host; $db_name; $db_user_name; $db_pass; , but we dont change those files in the header right? so the same thing apply to my question. – gosulove Aug 11 '16 at 13:25