So I must upload the CMS that I've been creating in the development environment to my company's server to show the client. But the software I created was built in the document root, and the company wants me to upload it to a folder on the server. Is there any way to set the document root dynamically with PHP, or to include an .htaccess or some such thing in a single folder on a server?
I suppose I could also juse create some global var with
$DOCUMENT_ROOT = dirname(dirname(__FILE__)));
And reference that at the beginning of my paths, but I am looking for a more elegant solution.
You're going to need to define a baseUrl that is available throughout your application. If the client is getting antzy to see your work, tell them to register a subdomain to throw it on.
Related
I need some help.
I was reading the security recommendations of my hosting service and they say that ideally just put the
index file and files like css, js and img inside my root folder, and that all other files should be placed
off, that is, a level above.
I tried doing this in my tests, and I had some problems. The structure of the hosting folders is:
/
/htdocs
Inside /htdocs I put the index.php file and when accessing it through the url exemple.com/index.php works normally.
But putting other test files out of htdocs is what starts the problem. For example, if I have a file called contact.php
and I try to access it through the url exemple.com/contact.php I get the 404 error message.
So the question I have to ask is:
Is it possible to access url files that are outside of htdocs, or better to put all the files that will be accessed by the url inside
of htdocs and leave only configuration files outside this folder, like class, functions, database connection, etc?
And if it is possible to access the files by url, how would I rewrite these urls in htaccess?
and that all other files should be placed off
Yes, this is good practice. However, you're misunderstanding the implementation.
You can not directly access files outside the document root. But you can indirectly access them. I.e., the web server can't see them, but your programming code can.
Ideally, your site would use the front controller pattern. Here, your index.php file would serve every page of your app by intercepting every request and then routing it to the correct end point. I.e., you would never directly request /contact.php, you'd instead request /contact, which would get funneled to /index.php, which would load the required resources from outside the doc root.
I'm building a local web application for my company, and I`m trying to run it in an xampp webserver. My problem is that I want to setup my root folder one time, and reference it in all my files.
For example, my folder structure is as follows:
root/index.php
root/include/includefiles.php
root/reles/ajustes/ajustes.php
root/classes/html/menu.php
root/classes/html/rodape.php
root/img/head.png
All of my files have to include the files menu.php and rodape.php
Using relative paths I would do "include ../../classes/html/menu.php" and "../classes/html/menu.php"
Until there it's ok, but in my menu.php file I have a link to other files, and I cannot utilize relative paths to link to it, because at index.php the link would be "/img/head.png" and at ajustes.php would be "../../img/head.png"
My solution is to define a root path, and I would link all my relative paths to ROOT_PATH."/img/head.png".
I found some solutions for this which worked. My problem appears when I try to access my application externally, from another computer using my host IP address, I can access my website, but the link appears as "c:/xampp/htdocs...", and I don't want that, I want the links appearing as "http://host-ip/img/head.png".
A good practice in defining the include paths is to add the __DIR__ magic constant before the include path. That way the path is always defined relative to the directory of the current file instead of the working directory.
You should have a different root path for public urls and internal server paths. So I'd recommend using the __DIR__ for includes and other internal server paths and another constant to be used in html and other public paths.
Edit: To clarify: the internal server path is the actual path on the server (www_root/foo/bar) and the public path is the one the server software serves through http (http://example.com/foo/bar)
I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here
I have a site on a server running Apache2 that resides at docroot /var/www/html. I want to access some of the files on a separate site at docroot /var/www/vhosts/othersite. Is there a way to access these files from the first site?
Thanks,
Chris Birk
You can include them using the include and require calls, or use symlinks to create a soft link in project 1 from project 2. These obviously depend on what you're actually trying to accomplish.
Edit: Oh, also, you could potentially add the folders you want to PATH.
The right way to do this is with mod_rewrite, and there are several ways of mapping URLs to different paths in the documentation here.
The cheatin' way of doing it would be to create a symbolic link from the directory outside the document root to a directory inside the document root, making sure the user Apache runs has can read that directory, and follow symlinks is turned on.
Yet another way of doing it would be to create a subdomain as a VirtualHost, with a document root of that other directory.
Hi guys I'm in a bit of a pickle here now. Well to start with I built a simple CMS in PHP with an admin panel the directory structure is like this:
root/
->admin/
->images/
It worked fine as is however the client requirements changed and they wanted that instead of having to access the admin folder as a folder within the root it be accessed as a web subdomain. so www.site.com/admin becomes admin.site.com
However this has terrible messed up and destroyed practically all the referencing I had done. Like I upload images on the CMS - however now uploading on ../images doesn't work as its now under a subdomain and I'm all messed up in trying to relatively reference images from there too. I've been trying to hack away at my config file for weeks and can't get to fix this :( - help please - on the front end the site is o.k. but my admin section is all messed up :(
I'm using PHP and MySQL.
Sounds like you've learned how toxic relative paths can be.
Possible quick fix: what happens if you copy/symlink/alias admin.domain.com/images to point at the same images folder that lives on your front-end site? I think that extra "../"es will basically be ignored.
More permanently, and in general, don't use relative paths. They will cause you nothing but pain. A couple of strategies:
1) Define some constant that points at the right location for images, css, etc:
define('IMG_DIR','/images');
define('CSS_DIR','/images');
// ... some time later
echo '<img src="' . IMG_DIR . '/myimage.jpg'"/>';
2) Much better: just maintain one constant that tells you where your application lives.
define('APP_ROOT','/myapp'); //could be chanted to just '/' if it doesn't live in some folder on the server
// ... later that day ...
echo '<img src=\"' . APP_ROOT . '/images/myimage.jpg"/>';
// ... or maybe you need to link to a logout script?
echo 'Log Out';
It's important to assume you application might need to run from the root ("/") or some directory on the server, etc.
The same goes for any filesystem operations you might do purely on the server side. Use absolute filesystem paths. If your main application has a script like "config/config.php", you could stick this at the top:
define('APP_FS_ROOT',realpath(dirname(__FILE__) . '/..'));
Assuming both the frontend and the admin are on the same file system, you will need to use absolute paths for everything in the admin. In the admin's config create a define that maps to the frontend's physical upload/image folder. For example, from the fontend you can access uploads folder with the relative path ./upload but from the admin.example.com site you will be required to use the absolute path /user/example.com/upload.
The fontend's config would look like (www.site.com/config.php):
define("UPLOAD_FOLDER", "./uploads");
define("WEB_UPLOAD_FOLDER", "/uploads");
The admin's config would look like (admin.site.com/config.php):
define("UPLOAD_FOLDER", "/user/site.com/upload");
define("WEB_UPLOAD_FOLDER", "http://www.site.com/uploads");
Then both the frontend and admin would reference the physical folder with:
$filename = UPLOAD_FOLDER . "/myupload.mp3";
And to create hyperlinks to the upload you would use this:
My Upload
Another possible solution would be to define a directory alias in apache for the directories you've moved.
Lets say your sub domain root is
/subdomains/images
<VirtualHost>
...
Alias /images "/subdomains/images"
...
</VirtualHost>
Both www.yourDomain.com/images and images.yourDomain.com would load the same files.
Or, if your using linux, a symlink could accomplish the same thing.