I've got a bit of a dilemma, and it's been bothering me for quite some time. I have a local testing server that's set up like so: 127.0.0.1/
My website in offline mode looks like this:
127.0.0.1/websitename/index.php
My live version of the website looks like this:
websitename.com/index.php
I've got the base script for almost all of the links, except for the including header and footer files.
The links in the footer and header files work except for the home page (on the root of the website).
Could anyone redirect me to a proper method of doing multi sub directory base URL linking for both offline and online?
I’ve tinkered around with most of the $_SERVER[] tags and attributes, as well as parse_url().
There's an easy and cool thing you can do in local server called: Virtual host, which allows you to create subdomain to access your local website without entering subdirectories in the url.
Example:
You can do something like the following to access your files:
mysite.localhost/ which is exactly the same as localhost/mysite/index.php
In that way, you don't have to worry about subdirectories when moving you website to the online server.
Links for virtual host:
WAMP
XAMPP
I’ve tinkered around with most of the $_SERVER[] tags and
attributes, as well as parse_url().
Don’t tinker with them. There’s no clean/automated way to do what you are doing. Just set a base path manually in a config file & don’t worry about it—relative paths—ever again. And if you need to set a base URL, the process is similar.
So as far as a file base path goes, you should explicitly set a $BASE_PATH like this:
$BASE_PATH = '/full/path/to/your/codebase/here/';
If you don’t know what your file system base path is, just place this line of code in your PHP code; like index.php:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
Then load that page. Somewhere near the top will be this text:
Your path is: /full/path/to/your/codebase/here/
Then with that set you can change your code to be something like this:
And then set your include_once like this:
include_once $BASE_PATH . 'includes/myfile.php';
Some might say you should use $_SERVER['DOCUMENT_ROOT'] or even dirname(__FILE__) directly with the implication being that you can simplify code portability that way. But the way file paths are set for installs can vary so it just never works well & the chances of you getting snagged on an odd server quirk is high.
It’s always best to just to manually set a $BASE_PATH in a config file when you move code than deal with the headaches caused by PHP constants like $_SERVER not being consistent between installs, setups & configurations.
And as far as a base URL goes, just follow the same thinking with this being on your local development setup:
$BASE_URL = '/websitename/';
And this being on your production server:
$BASE_URL = '/';
So with that $BASE_URL set then you can just do this:
I’ve got the base script for almost all of the links, except for the
including header and footer files.
Now just prepend any path you might need requested via a URL with $BASE_URL & you should be good to go.
I suggest you move to a development environment which more closely reflects the live system. For this, you can run a WAMP server and configure it to serve your web site as a domain like mysite.local and then you simply edit your hosts file so that mysite.local resolves to your 127.0.0.1. Then you just type mysite.local into your browser, it resolves to your local PC, and make sure apache is configured for virtual hosts and listening on port 80.
Your hosts is a local DNS lookup file found in windows\system32\drivers\etc. You may need to open it in Notepad which is run as administrator in order to be able to edit it.
Related
I have ran into a problem where by using /js/filename.js or /images/image.jpg etc is not loading anything on my WAMP Server. What it seems like is that its not getting the base url properly here. Screenshots will give you better idea.
Trying to reference the files with "/" in the start but it fails to load anything. I need to have it because i would be working with URL Rewrites so in that case it should load the files from there. Please let me know about any wamp server setting that is causing this issue.
Regards.
You are referring to your files with a root-path like "/my-file.txt" (i.e. having a slash at begin),
which would result to "C:/wamp/www/my-file.txt" being requested.
Change all your paths to be relative paths, for example to "./my-file.txt" path (i.e. with both dot and slash ./),
That would change the file request to something like "C:/wamp/www/my-project/public/my-file.txt" (if your HTML file is in public directory that is).
Actually I would not recommend you using relative paths like ./myfile.txt or something else, as it can get somewhat messy when you try MVC in your code.
Instead I would recommend you set up a virtual host on your wamp machne.
Here you can read more about setting up virtual host in wamp
https://john-dugan.com/wamp-vhost-setup/
I'm sure this has been asked and answered before, however, I am new to the lingo ad couldn't find the answer.
I have made a php site with several pages. To access the site and links on my mac, all links and photos and such must point to http://localhost:8888/ To test on my linux machine, all links and photos must point to a different local host address. And when I load the site to my web server, of course the links and photos must point to the web address.
There aren't that many places in the 5 page site that need to be changed, but I am sure there must be a better way than hand editing.
How does one change the host location globally?
Thanks
I am not sure if this is what you are asking for but you probably should use relative path for your links and images.
For example if you have all your images in an "images" directory in the root of your site, you should do this
<img src="images/myimage.png">
instead of
<img src="http://siteaddress/path/to/image/image.png">
This way you don't have to change anything if you change the webserver.
Ideally you would want to have a configuration file that is specific for each environment storing the values in constants. So in your code you include the configuration file (as long as you need it) and you use the constants.
In the constants you can store values such as the connection to the database or the values of the root path/url that are specific to each environment so for example on your local machine you can have:
define('CONFIG_ROOT_URL', 'http://localhost:8888/');
whilst on the live website you might have:
define('CONFIG_ROOT_URL', 'http://your.domain.com/');
but in your code you just use:
$str_upload_page = CONFIG_ROOT_URL . 'upload.php';
echo $str_upload_page
//this should output 'http://your.domain.com/upload.php' on the live version
//and 'http://localhost/8888/upload.php' on your local machine
I am a newbie to web development and I am trying to learn best practices as I go along.
I code and run my website locally using Dreamweaver on a local Apache server; Then I deploy it remotely and I test it there.
I use for my website (html, php and some js).
I have been facing an issue since I started this and I don't know any good practices to resolve it.
DOCUMENT_ROOT is a php variable that changes; Locally it points to xampp/htdocs (and not my actual website root xampp/htdocs/myWebsite) and remotely it points to my actual document root.
So right now I can't use this for the above reason so I end up locating files using the absolute path on the remote server.
Is there a good practice to avoid this?
PS:
I know I can change php.ini config file to change where DOCUMENT_ROOT points to but I don't want either..
Also, I can't hard code relative paths (as commentators have suggested) because I have a scripts running from different directories... So I can't hard code relative paths there
Another way to go about it. Create a configuration file and determine if the site is running on a local site or live site, then include that configuration file on each page. You can also set settings like error reporting and database connections for each this way.
//Determine if Server is local system or live site
if(stristr($_SERVER['HTTP_HOST'],'local')||(substr($_SERVER['HTTP_HOST'],0,7)=='192.168')){
$local=TRUE;
}
else {
$local=FALSE;
}
if($local){
define('BASE_URL','http://localhost/localsite/');
define('BASE_URI','C:/localserver/htdocs/localsite/');
}
else{
define('BASE_URL','http://www.example.com/');
define('BASE_URI','server/directory/pathToRoot/');
}
Then you can use the defined constants in your php pages to define your paths. For example:
include(BASE_URI.'directory/filename.php');
Some Link
And it will work correctly on each deployment.
I've just got a new PC, running Win7/64. My current projects are all on my Macbook. Of course I could just move the projects over but I like the portability so my ideal scenario is my work syncing across the computers. This is simple with most types of files using LAN file sharing, but when it comes to my Wordpress sites I hit some problems. Here's what I've tried so far:
Sharing my htdocs folder and then accessing it in the browser through http://my.ip.address:portnumber/directory. This works except a crucial problem - <?php bloginfo('template_url); ?> thinks the site's root is localhost instead of my.ip.address, which breaks all my root links like stylesheets.
Creating a shortcut to my Macbook's shared MAMP/htdocs folder inside my PC's XAMPP/htdocs folder. Not actually sure if it's possible to access shortcut files at all through the browser, I tried a few different combinations of /s that didn't work. This was a longshot anyway really.
I understand I need to somehow get the PHP generated after it arrives to my computer, but I really don't know how I'd go about making that happen.
Any help much appreciated.
Add the following to your wp-config.php file, and it will fix your issue in #1
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
I am developing a website on php, I have installed wamp on my personal computer and my website files are in the www folder of wamp.
now considering www as my root folder i have a template folder in the root folder and header.inc.html file in the template folder. when I try to include this header.inc.html file in any other php file using an absolute path include('/template/header.inc.html'); it gives me error "Failed to open stream: No such file or directory", but when I create a simple html link using the same absolute path it works perfectly and opens the file. below is my test code
<?php
echo 'headerfile';
include('/template/header.inc.html');
?>
if I give the full path for example C:/wamp/www/template/header.inc.html to the include function it works fine.
I am confused that this problem is occurring on my wamp server only and it would work perfectly on any webhost server, or maybe the same problem will exist on a webhost
I would appreciate any help that would clarify my confusion, Thanks.
Absolute paths on the server start from the server's hard disk (C:\).
Absolute paths on the client start from the root of the website (http://example.com/).
You can make use of __DIR__ to make some file on disk relative to the php-file on disk itself:
include(__DIR__.'/template/header.inc.html');
This should solve your issue.
The difference is not that easy to explain because both types of paths - even related - are two pair of shoes. I suggest you start with a very basic HTML website tutorial that explains how to link on your website and where files are located and how that is related to the webserver configuration.
HTML pages live in the client's browser that know nothing about your server's folder structure, and they're relative to the domain name eg. http://example.com/.
PHP programs run on the server side and they deal with the server folders. You shouldn't hardcode full paths in your php programs, because it will cause problems whenever you'll move them between the development server and the live host (just to name an example). Therefore in php files you should either use relative paths to your file, or use the __DIR__ magic constant that gets substituted with the directory where the php file is.
1.) First approach: include('template/header.inc.html');
2.) Second approach: include(__DIR__ .'/template/header.inc.html');
In your case (working on a development machine) both the client and the server is the same box, that might be confusing you.