简体   繁体   中英

php relative paths and directories

I'm wondering something, but I can't seem to be finding a good, clear answer, or even a solution to this problem:

My PHP website has the following structure:

root
   functions
   generators
   helpers
   scripts
   style
   index.php

These are all folders and one php file. Functions contains a bunch op php files related to database connection and various other database operations such as inserting, deleting, updating,... Generators contains classes to automatically generate web pages to make them look all the same. Helpers are classes that handle login, logout, register, etc. Scripts are javascript and Style is CSS.

In my generators folder, there is a file mainGenerator.php, this generates various pieces of the website:

 private function generateLogin()
    {
        if (!isLoggedIn()) {
            echo "
                <h2>Login</h2>
                <form method='post' action='../helpers/login.php' id='loginForm'>
                <p>
                Username:<br/>
                <input class='search' type='text' name='username'/>
                Password:<br/>
                <input class='search' type='password' name='password'/>
                <input name='login' type='image' style='border: 0; margin: 0 0 -9px 5px;' src='style/login.png' alt='Login' title='Login'/><br/>
                No account yet? <a href='../register.php'>Register</a>
                </p></form>";
        } else {
            echo "
                <h2>Welome, <a href='user.php' style='color: #1293EE;'>" .
                 $_SESSION['user_name'] .
                 "</a></h2>
                <a href='logout.php'>Log off</a>";
        }
    }

This piece of code generates the loginbox on every page, or displays a welcome message if the user is already logged in. As you can see, the action references to "../helpers/login.php", because that's the relative location of the loginhelper from this generator's point of view.

Now, here's the problem: If I press the login button on the index.php ( http://example.com/ProjectName/index.php ) it redirects me to http://example.com/helpers/login.php and says the file isn't found.

I see what the problem is, the post action sees the request coming from index.php and goes up one directory and then looks for /helpers/login.php, which doesn't exist of course.

A fix might be to change the action to

action='/helpers/login.php'

but that gives me the same problem should I need access to generateLogin() from within a certain subdirectory... How can this particular problem be solved, ie: that the reference to /helpers/login.php stays correct, no matter from where I try to access it.

action='/ProjectName/helpers/login.php'

Assuming your root is http://localhost then the above link should always resolve to http://localhost/ProjectName/helpers/login.php whether it's called from http://localhost/ProjectName/index.php or http://localhost/ProjectName/subdir/index.php

If you don't want to hardcode "ProjectName" into many different scripts, you can use a global variable and define it in a config file:

helpers/ConfigOptions.php:

<?php
$ProjectName = "ProjectName";
?>

Then in your scripts, include the config file and use the variable you defined:

index.php:

include $_SERVER['DOCUMENT_ROOT'] . '/helpers/ConfigOptions.php';

...

echo "
  <form action='/$ProjectName/helpers/login.php'>
  ....
";

More about DOCUMENT_ROOT:

DOCUMENT_ROOT is defined in the server configuration file and is the root on the filesystem of where scripts are executing, not the web address you would type in a browser. In the above example, I'm assuming that document root looks something like /home/user/www/ProjectName. If DOCUMENT_ROOT is only /home/user/www then you can change your include path to this:

include $_SERVER['DOCUMENT_ROOT'] . '/ProjectName/helpers/ConfigOptions.php';

or use a relative path. My vote would be on the latter since you wouldn't need to hardcode "ProjectName".

So include() and require() would take either:

  1. An absolute path to the file
  2. A path that's relative to the current executing script. If A includes B and B includes C, when B includes C it needs to provide a path that is relative to A .

So when do you need to use DOCUMENT_ROOT? Usually with templates where a relative path like ../helpers/file.php can resolve to different absolute paths depending on what's including the file. In the above example where index.php is including ConfigOptions.php, you probably don't need to use DOCUMENT_ROOT since index.php is not a template. I used it anyway to be safe, but maybe I opened up a whole can of worms here. I hope I didn't confuse you even more.

If you have a link that starts with a slash it always gets appended to the root and not your current directory...

So if your page is www.example.com/mySite/foo.html and you have a link like this: <a href="/bar.html">Bar/<a> the user will get redirected to www.example.com/bar.html ...

You should just have your form action point to helpers/login.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM