0

I am beginner in web development.I want to know is it okay to use whole file php like C:\xampp\htdocs\Performance\login\login.php while including or calling in href link in the other file which are in different folders.

Currency, I am using xampp.If I do like this will it make any difficulty in the future while integrating with our website.

If it will give any problem, how I need to mentions to avoid the problems in future.

Vidya
  • 382
  • 1
  • 6
  • 17
  • 2
    You should generally avoid that and use relative paths instead. What would happen if you host the scripts on a different server with a different installation path? See the comments here about using relative paths: http://php.net/manual/en/function.require-once.php – jspcal May 21 '18 at 06:01
  • 1
    it can be the beast idea at times, but as @jspcal said there is the issue or moving servers\setp. most apps will have a config for the base dir in a variable or constant, making move the scripts easy –  May 21 '18 at 06:12
  • Possible duplicate of [PHP include file strategy needed](https://stackoverflow.com/questions/339202/php-include-file-strategy-needed) – Nigel Ren May 21 '18 at 06:14

3 Answers3

0

When the files are in the same folder, you can refer them with their basename. For example, file1.html and file2.html are in the "login" folder and you want to include file1 on file2 you should do something like this:

include 'file1.html';

If you have to go to the parent directory, you have to use '..' which indicates to go back 1 folder. For example, you have "login" folder and "register" folder, and you have register.php inside the register folder but you want to include file2.html from the login folder, you would have to do something like this:

include '../login/file2.html';

Go back 1 folder, enter on login and include the file2.html

César Escudero
  • 320
  • 2
  • 12
0

You can also create and include a config.php file in all your code. Inside that file you will define a path up to your root directory.

define('ROOT_', '/my/path/to/root/');

Then to call it from another file in another directory:

include ROOT_ . 'path/to/file.php';

You can do the same for your links but that should only be defined from your domain's root.

 define('PUBLIC_', '/public/');

 echo '<a href="' . PUBLIC_ . 'login.php">Login</a>';

Then if you migrate to another site with a different folder structure all you have to do is change the path in the config.php.

Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0

there are different scenarios where you want to choose:

==>There are two types of path,which you just described

1.Absolute path:

--> C:\xampp\htdocs\Performance\login\login.php

2.Relative path:

--> ./login.php

Let's consider some scenarios so you get better understanding which one to choose when:

1.if you working under same root directory most of the time i would suggest go with Relative paths.

2.If you unable to find out included file with relative path in some-cases like multiple dictionaries n all Go with Absolute paths

I personally use Relative paths because it's easy to use and have some of it's own advantages like it's great when we are uploading site on servers, we don't need to maintain same paths n names

when in case of Absolute path you need to take care of All the specific folders and File names, character by charter.

  • Thanks for quick reply and I have query that if I use absolute path will not make problem in the future if I take care of All the specific folders and File names, character by charter. – Vidya May 21 '18 at 06:43
  • @Sheela No , you will not encounter any problem, You good to go. – visana niraj May 21 '18 at 07:44
  • @Sheela If i answered your question plz tick my answer as right one and let me know if u have any other query regarding this. – visana niraj May 21 '18 at 07:47
  • @Sheela plz do take care that drive path will give you errors so sub directories you will manage but there will be no C drive or any other drive when u upload your website to another live server...do take care of that...for now absolute path is ok but then u need to make it relative just upto drive level. – visana niraj May 21 '18 at 10:45