trying to sort out a way that i can develop a version of my website on my local machine and then upload the files to the live site. The issue is, i want the local site to be completely dependant on itself and there for the css links, the includes etc need to be linking to the local folders.
I use a file called init.php which is in the root of the web folder and this is included into every page. In this init, i include all the stylesheets, jquery and other includes.
If i didnt have any sub folders on my website i would simply use this init.php included on every page and then simply do eg css/main.css and include 'includes/header.php' etc.
The problem is that i have folders in my website and still wish to just use the one init.php file that is in the root. However, I need this file to be included and also link to the css files etc that are also in the same place.
The folder structure would be:
root
init.php
databaseconn.php
/css/
main.css
/settings/
user_profile.php
If i include the init.php file in the user_profile.php page, the links to the other files will not work. Currently i am including the init.php onto the user_profile.php pages by denoting a double period (../)
The question is how can i always make the files linked in the init.php file always link to the same correct place no matter what folder i am in, but also whilst not being affected when i move the website files to my live host?
I have tried DIR and document root etc but when used per say as:
<?php define( 'BASE_PATH', __DIR__.'/' ); ?>
<link rel="stylesheet" type="text/css" href="<?php echo BASE_PATH; ?>css/main.css">
This (on MAMP) gives me this URL:
/Applications/MAMP/htdocs/website/css/main.css
Which ofcourse wont work.
How can i do this correctly? I dont want to force the http: part niether incase i get SLL in the future.
Any help would be great!
You can use base href tag for this one.
Example code:
<?php
$baseHref = ($isDevelopment ? '//localhost/site/' : '//www.site.com/');
?>
<html>
<head>
<base href="<?php echo $baseHref; ?>" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>
Edit: of course you have to define if you're in dev or prod server in your config / init somewhere.
I would go with $_SERVER['SERVER_NAME']
which captures your domain name (between protocol and folder path). Then use:
function isSecure() {
return
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
That function comes from here:
How to find out if you're using HTTPS without $_SERVER['HTTPS']
if your server doesn't use 443 for the SSL port, you'll need to change the value.
So then:
if (isSecure()) {
$basePath = 'https//' . $_SERVER['SERVER_NAME'];
} else {
$basePath = 'http//' . $_SERVER['SERVER_NAME'];
}
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.