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
Related
I have designed a website and uploaded it successfully on a server. The HTML pages links is no more working.
The website is www.rentacar-mauritius.com the "Home" works well but the other ones do not like "Tours, Rent a Car" and so on. By viewing my source on the browser, you'll see my code.
Live server is case sensitive (Linux Hosting)
I change your URL to http://www.rentacar-mauritius.com/Rentacar.html and its working
Check your file name(s), Spellings
so your URL(s)
http://www.rentacar-mauritius.com/rentacar.html
http://www.rentacar-mauritius.com/tours.html
http://www.rentacar-mauritius.com/gallery.html
http://www.rentacar-mauritius.com/about.html
http://www.rentacar-mauritius.com/contact.html
should be
http://www.rentacar-mauritius.com/Rentacar.html
http://www.rentacar-mauritius.com/Tours.html
http://www.rentacar-mauritius.com/Gallery.html
http://www.rentacar-mauritius.com/About.html
http://www.rentacar-mauritius.com/Contact.html
Like wise check your whole site
Rent A Car
Filenames are case-sensitive for Linux environment. If your hosting server is running Linux, make sure that your file names match the href values exactly (including the case).
I have been using "Delphi PHP XE2". The file structure has worked fine for over a year on the development machine and the production.
The folders are organised as follows.
src/app_login.php
src/add
src/css
src/js
src/fnc
src/db
src/images
etc.
I am just trying out PHPSTORM. The include files work fine. When I run it in Firefox it can find all images, JavaScript files, etc. But it can not find the CSS files as it is looking for them in
css
rather than
src/css
I haven't changed anything.
Why is the browser now looking at absolute path rather than relative?
How did PHPStorm manage to tell it that?
And how do I coerce it to treat them as relative paths?
I see that in the browser it runs it as:
http://localhost:63342/SRC/app_login.php
instead of
http://localhost:63342/app_login.php
I guess this the problem. How do I coerce it to move down one directory level?
I am trying to set up IIS, I think this might be best for later on. Here are the screen shots:
I configured the hosts file so that when I type wys.com in the browser it tries to run it and shows a blank page. If I type in 192.168.1.0 it just shows a blank page. View Source shows nothing. I was expecting it to run the index.html at d:\wys\src. Why is the browser not running it?
You are using PhpStorm's own simple built-in web server which uses URLs like http://localhost:63342/ProjectName/app_login.php. You cannot make http://localhost:63342/app_login.php using such server as it will not be able to tell what files to serve.
Either use your own Apache/IIS/whatever web server .. or the best you can get with built-in server would be http://ProjectName:63342/app_login.php(IDE needs to know what site/files to serve somehow). For that:
Edit your hosts file (or local DNS server, if preferred and have one) and point ProjectName to your computer's IP (e.g. 127.0.0.1).
Create Deployment entry of correct type (In Place should do), configure it (provide desired URL etc -- http://ProjectName:63342/) and mark it as Default for this project -- now IDE will use URL from there when generating "open in browser" URLs.
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.
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 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.