How to handle absolute URLs in localhost - php

I'm trying to use absolute URLs throughout my PHP website to make sure that the paths to pages and images are always accurate/working, no matter where you are in the file hierarchy.
I struggle to find the best, most maintainable way to handle that.
Here is an example of what I have at the moment :
require_once 'http://localhost/Website/includes/init.php';
"Website" in this case is the name of the directory I created for this project. The main problem with this method is that if I change the directory name, I'll have to search&replace a thousand of occurences throughout all the pages.
After searching on StackOverflow, I found what seems to be a better way to handle this :
define("LOCAL", "http://localhost/Website/");
define("WEB", "http://foo.bar");
$environment = LOCAL; //change to WEB if you're live
Whenever I want to create a link, I use this synthax :
require_once($environment . 'includes/init.php');
This works quite well, but I was wondering if it really was the correct way to do this.
If it is, I'm strill struggling with one detail : how can I include this portion of code, where the constants and variable are defined, in all of my pages? I can't use an include, because for that include to work everywhere in the website it would need the constants and variables that are in the file that it's trying to call, if that makes sense.

You can do this in your ini or .htacces file with auto_prepend_file. PHP auto include

Related

When working deep in multiple levels of directories how does one assign a variable with the exact path each file needs to be found and accessed?

This has always been a problem for me. I have never learned the proper way to do this.
I am now working on a website where using things like ../../ will not long work. I need to somehow create a variable or resource or constant so that I can call the path from root to the file like this.
JUST AN EXAMPLE
link="<?php ROOT_PATH . "/footer.php" ?>"
and this would be:
link="inc/assets/directory1/directory2/footer.php"
But I would also need to be able to use the same variable or constant without changing its value and it will work no matter what directory the file is in or how many levels deep it is.
The file could be 7 folder deep in the directory structure but I still need it to work.
I've been people up here use similar techniques with DOCUMENT_ROOT. But I've yet to be able to get that one to work for me the way i need it to.
I am so STUCK. PLEASE! lol. Any help would be great, thanks!
NOTE: Before I start getting to far into this project and writing out all these paths by hand I need to wait till a new method has become available.
UPADATE: I simply wan to beable to point to any file in the file/directory structure no matter how many dirs deep it is using just one path variable or constant. there has got to be a wy to define the path of a iel up to a given point and just use that for all your files. Wordpress must do this.
UPDATE: Two more hours digging through pills of internet crap and I can not find my solution. It seems if no one can just give me the answer I'm going to have to take an entire class in server technologies.

Can someone please explain how paths work in PHP include() situations

I don't get it.
When trying to include files from different directories, i'm sure i must be missing something real simple.
Site structure is like this.
if i include("includes/header.php); from inside the /reports/top_sellers_report.php file, the call to the css file doesn't work.
To make it work i must put ../styles/styles.css
But then, if i open "product_dtails.php from the root, it too includes the header, and then the css file won't load and i need to remove the ../ to make it work.
I can't win...
Am i missing something? 4 hours of searching online suggests i am!
Your problem is not really PHP-related. Just look at the URLs:
http://www.example.com/
http://www.example.com/reports/
http://www.example.com/styles/styles.css
Depending on where you are on your site, the relative path to styles.css might change, thus when you access http://www.example.com/reports/ you have to use ../styles/styles.css, wheras in http://www.example.com/ you have to us styles/styles.css.
Probably the easiest way to fix your problem is either by using the absolute URL like http://www.example.com/styles/styles.css or the base-path /styles/styles.css instead of ../styles/styles.css. I recommend the latter.
I think you should look into using an MVC approach as this would probably help you organise your code better:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
The include statements are evaluated on the server, so wherever you are including the file from, you need to specify it relatively from there. The CSS 'include' is being evaluated in the browser, so you need to make the file path relative from that directory.
I suggested looking to the MVC approach as it helps you to manage your code and separate the different parts of your website into distinct parts which should help you avoid this issue.
I suppose you can refer to you stylesheet absolutely though (ie http://www.mywebsite.com/dir/dir2/stylesheet.css) on each page and that would also work.
as far as I know, include is relative to your index so you have root/index.php
within index.php you should use include("styles/style.css")
within includes/header.php you'll need to add the ../styles to redirect you to root and then to your styles folder and so on
if you open directly product_dtails.php it will act like the index.php, so you need to just use styles/style.php

How to get the original path of a symbolic link in PHP?

I have several files in my web folder, including these two:
/mydocroot/include/somesubdir/include.inc.php
/mydocroot/include/settings.inc.php
where somesubdir is a symbolic link to another directory on my disk, to a path named, eh, let's say /anywhere/else.
Inside include.inc.php, something like this is written:
<?php
require_once "../settings.inc.php";
?>
In my opinion, that should include /mydocroot/include/settings.php... But guess what happens: PHP tries to include /anywhere/settings.inc.php, instead of /mydocroot/include/settings.inc.php.
It seems like PHP automatically resolves symbolic links.
How can I avoid this and include my settings.inc.php file?
I just had a similar issue. You can do it by using $_SERVER['SCRIPT_FILENAME'] variable, that displays the requested file, so in your case it will be /mydocroot/include/somesubdir/include.inc.php, even if somesubdir is a symbolic link. To include a file that is one level lower instead of doing
require_once "../settings.inc.php";
do this:
require_once dirname(dirname($_SERVER['SCRIPT_FILENAME'])).DIRECTORY_SEPARATOR."settings.inc.php"
Documentation about $_SERVER variables
The most straight forward solution is to always use absolute paths. There are multiple ways you can do this, from hard coding the path every time you need it, to hard coding the path once in the top of your script and referencing that, to dynamically figuring it out and setting it once at the top of your script.
The third option is what most off the shelf CMSs use to be able to run without complete knowledge of your file structure.
Why is it that you're using a symbolically linked directory in this manner?
The solution is to create a basepath variable. The best way to do this is to include the following at the top of your script and then reference it
$basepath = dirname(dirname($_SERVER['SCRIPT_FILENAME'])).DIRECTORY_SEPARATOR;
You can then reference the basepath in your includes, requires, etc. Therefore,
include "../myscript.php";
Would become,
include $basepath."myscript.php";
If you are back ticking further, you will have this:
include "../../myscript.php";
Would become,
include $basepath."../myscript.php";
You must nest the dir_name functions twice, plus one more time for each additional folder you need to backtick through. You MUST get all the way back to the folder where the symbolic link exists.
I consider this issue a major design flaw with PHP. I can't think of a single instance where accessing backticked files relative to the actual file would be desirable. In all situations, including virtual hosting, it ONLY makes sense to regress back along the linked path, never the target path.
You can use is_link to check if a file is a symbolic link. If it is, use readlink to get the absolute target.
But, in ten years of php development I've never had cause to use symbolic links. As Jason suggests, I've always defined or deduced an absolute path once, then used that throughout my app to make all file paths absolute.

display one page from a site on a different domain name

I have a site complete with CMS etc all working under one domain name. It turns out for legal reasons one page on this site has to sit on a different domain name. The page is hooked into the same CMS as the rest of the site (built using codeigniter). I don't want to have to do another installation just for this page.
Is there any simple way to display just this page under a different domain name without taking it out of the current application?
Thanks a lot
You should look at either (in order):
an include()with correct php.ini configuration
a file_get_content() and printing the variable into your page
an <iframe src="yoururl"> wich would be the easy peasy but unsafe way
using the on-purprose curllibrary
using fopen() wich theorically allows distant files to be opened, but based on my experience, it's not that reliable
Look at this site, it seems rather exhaustive regarding your problem.
Try including the file
<?php include 'http://www.domain.com/url/to/file/page.html' ?>
I think what you need here is a symlink, which is something I don't know too much about. My understanding is that the path displayed to the user does not in fact have to have anything to do with where the file is actually stored, meaning you can set this up to have a completely different URL while keeping it as part of your original application.
A simpler thing is doing a redirect...it's one line of code in your .htaccess file and you're good to go.
include is a possible solution depending on the format of the remote page (ie, this won't work very well if the remote page has a full DOM structure, and you're trying to include the remote page within the DOM structure of your CMS page), however more information about that remote page would be needed to help determine if include() alone would be enough.
Regardless, if include() does, work, you must make sure allow_url_include in php.ini is enabled, as by default script execution will terminate when encoutering a remote URL include statement.

$dbhost=localhost $dbuser=root etc, on each php page, how to fix?

i have been given a php application as an internship project to clean up. The developer before has declared stuff like dbhost,dbuser so many times. On each script page. I was wondering what sort of design php developers use to get around this. i.e making a property file ? etc..
Generally most applications have a common include file, usually named something like "bootstrap", that defines global options and values and sets up some initialisation code. Then each page that is requested includes this file first.
In your case you'd put your database configuration in this bootstrap (perhaps traditionally in /includes/bootstrap.php), then for each page where it is required require "./includes/bootstrap.php";.
As an example, phpBB includes its 'kernel bootstrapper' on each page.
In order to avoid errors you should use require_once:
require_once "./includes/bootstrap.php";
This way even if multiple scripts try to include that specific file it is only included once.
Do not make a property file, or, if you insist, be certain that it cannot be downloaded through HTTP. The advantage of a PHP file is that, even if hackers guess the file name, it won't reveal much.
Put all DB credentials into a separate .PHP file, e.g. db_settings.php and then insert everywhere
<?
...
include "db_settings.php";
... ?>
You may even insert database connection code into the same file.

Categories