I'm using Laravel 4.1. I put phpgrid into the vendors directory. Here are my paths using example domains:
Actual file path to website root: /home/.../htdocs/dashboard/public
Actual file path to phpgrid: /home/.../htdocs/dashboard/vendor/phpgrid
The (example) url to the site is: http://www.site1.com/dashboard/
The SERVER_ROOT is set to: http://www.site1.com/dashboard/vendor/phpgrid
phpgrid works when I use that domain. The problem is that I want to use a shorter domain with masking. So the url I want to use is something like: http://dashboard.myotherdomain.com/, and the virtual host has it pointing to the actual file path to the website root directory above. phpgrid builds the table, but then the AJAX fires and can't get to the vendor directory because it is now below the site root of the masked domain. I thought it would use the absolute path, but it doesn't seem to. Any ideas?
It turns out that the issue was with the AJAX calls. AJAX will not let you call a different domain for security reasons, so setting the SERVER_ROOT to a different domain than my masked domain was failing (silently btw, blank error message in phpgrid). So I was forced to move the phpgrid files and folders into the webroot and change the SERVER_ROOT to a relative path.
A simple fix although I would have preferred to keep the files in the vendor directory.
Related
I have my website on my local server (localhost - XAMPP). My website is broken up into three parts: 1)Header 2)Body and 3)Footer/Jscripts.
The header.php calls other basic files for the header section of the webpage. It works fine when I used relative paths, for example: include_once('./_css/main.css'); to call my CSS files or include_once('./_inc/metadata.php); to call my metadata for my pages.
The problem arises when I use an absolute path:
include_once('http://localhost/mywebsite.com/_css/main.css'); the same for the other files.
Why is that? This all started because I want to create subfolders because I had a general page, but now that I'm breaking it down to subpages I need a folder to contain the subpages
I was trying to include the css files and all the other resource files to the subpages, but they break using the relative paths method (./_css/main.css) being pulled from the header file located in the root directory. On the subpages, I'm using include_once('../_inc/header.php') to include the initial header.php file.
I want to avoid having to duplicate all the resource files from the root directory and adding them in the subfolder with their respective relative paths. That's why I was trying to use the absolute path method.
Any insight or clue would be appreciate as to:
Fix the 'Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0' error when using the absolute path.
2)Sharing css files in subfolders.
Using an absolute path may be a bad idea, what if you move from localhost to a real server? You'd need to make massive changes. Changing the project structure so that you could avoid all of this might be better, or if you really want to keep things as they are, use a variable.
<?php
$include_path = "whatever/path"
include('../header.php');
?>
and in header.php
<?php
include($include_path.'/my_css.css');
?>
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 am working on a website in a CMS site. I have provided a link in my footer file e.g blogs.php. This page is at the root like "http://www.example.com/new_cms/blogs.php"
When I moves to other files link like "http://www.example.com/new_cms/forums" the footer link is changed to "http://www.example.com/new_cms/forums/blogs.php" but the blogs.php page resides at path "http://www.example.com/new_cms/" . I tried different $_SERVER[] variables but got no luck to get the above path "http://www.example.com/new_cms/" means the server name with the directory where the project is currently running from.
Any one have idea how to get it done will be a great help.
Thanks
This is probably because your link has a relative url in it, like so :
Blogs
So it looks for the blogs.php file in the current directory.
If you want that link to point to the blogs.php file that is in the new_cms folder, you have to use a link like this :
Blogs
You can use absolute URL or try to rewrite your url with a condition.
You can obtain the server name from $_SERVER['SERVER_NAME'] that is the canonical server name specified in the server/virtual host configuration. Besides that $_SERVER['HTTP_HOST'] gives you the host specified in the HTTP request header field Host. In general, these two values are identical. But since both can be manipulated by user input (see Chris Shiflett’s SERVER_NAME Versus HTTP_HOST) you need to be careful when using these.
As for the path, you will probably need to specify the base URL path on our own. Because if you think of URL rewriting techniques, the physical path (file system path) does not need to be the same as the logical path (URL path). So you can not derive the logical base path from the requested URL path, the script file path and the document root.
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.