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.
Related
I've uploaded my site files to ../public_html directory in cPanel. PHP scripts are in cgi-bin folder, each has 0755 privileges set. Domain is set up.
Html, css and javascript work fine but I get Internal Server Error when I try to execute any of php scripts.
I've tried adding
ini_set("include_path", '/home/*%username%*/php:' . ini_get("include_path") );
to each php source file (as was suggested in the PHP section of cPanel). But I keep getting the same error and the adress bar shows http://%domain-name%/cgi-bin/%script-name%.php
cPanel Error log doesn't show any errors.
cPanel documentation was completely unhelpful (for a beginner like me).
What do I have to do to make the scripts run as intented?
The problem was with relative paths in html files. For some reason they were interpreted differently than on Windows with XAMPP.
I had to write all paths to php scripts starting with / (as relative to the home directory not the html file). And in php scripts I had to write paths to resources as relative to the php file. (Thanks #Sky for pointing me in the right direction.)
This is all very confusing and getting it right involved lots of tweaking.
I have web application. i am using WAMP server.
I have developed web application and put my application files under "login" and then in "www" folder of wamp.i.e. Drive:\wamp\www\login\
now when upload my file to the web host that goes directly to "www" folder and there is no "login" folder there. so my existing links on localhost e.g.
http://localhost:8081/login/dashboard.php
turning to http://example.com/login/dashboard.php.
now as there is no login folder, because of wrong path, link is breaking.
there is one way, that i change every link everytime before uploading to webserver, which is time consuming and prone to errors.
is there any simple way i.e. dynamic way that my links work perfect both on localhost as well as web server.
so that the link on localhost on web server the link should be http://localhost:8081/login/dashboard.php direct automatically to http://example.com/dashboard.php instead of http://example.com/login/dashboard.php
i tried using $_SERVER["DOCUMENT_ROOT"] but it point local drive path and did not work well for me..
any help really appreciated.
Have you tried to work with relative paths?
$path = "/login/etc..."
This way you don't have to change the paths each time. I think php even has a function to turn relative paths to absolute ones.
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.
When I echo "PHP_SELF" in localhost running on xampp, I get the project folder as the echoed text e.g.
// index.php
<?php echo $_SERVER["PHP_SELF"]; ?>
The answer is
/a3-ver-9.0/index.php
but if I was on a server with a domain, that line would just return "/index.php"
Is there any way I can temporarily changes the default root in xampp to reflect this?
The reason is because I use absolute paths from root to include resources e.g. images.
however these absolute paths will not work if the root is not set correctly!
Cheers
EDIT
To make things more clear...
I have a website with php files that return parts of the page e.g. header.php and footer.php etc.
I have an installation of wordpress and in the wordpress template, I include these files. and because im including these files from different directories, the paths inside the included files must be absolute.
however the files in the header.php are included like:
<img src="/images/image1.jpg" />
now this include is correct, and when I've finished the project and upload it to my server it will correctly retrieve the image.
but when working with localhost, the root folder "/" is the folder where ALL my local projects are kept.
So i need a way to temporarily change the localhost root for each project, so i dont have to mess about changing a load of paths when the project is ready for upload!
Hope this made more sense
However when working on local host
Best way to solve this is using virtual hosts for your projects locally.
So something like www.project.local actually shows your project.
Here is a tutorial how to achieve this in xampp.
Okay, now I see what you mean, here you go this will help you, just change the root to the folder of your current project, for example:
"C:/xampp/xampp/htdocs/a3-ver-9.0/"
A while back I asked how to open a file in actionscript to supply information for my flash banner.
It works nicely when I just open the flash file on my computer, but when I upload it to my website it does not show the data from the remote text file...
I know that the file reading bit of the program is correct, as it reads local files on the same web server just fine. So I assume it has something to do with either PHP or the FlashPlayer.
Thanks for any help you can give me!
===
How stupid of me to forget to mention that the file being loaded is from a remote server.
Thus the path to it is an actual URL. As I said this works fine on the computer regardless of whether the file is local or a remote file.
But on the PHP webserver it only wants to read the local files, it doesn't Read the Remote ones...
===Update
I've tried the crossdomain.xml solution, but it seems not to be working.
The banner is located on the actual webserver (http://forum.mydomainhere.com/)
while the text file is located at:
http://files.mydomainhere.com/ - this is a DNS reference to my file server.
The xml file i created looks like:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="www.forum.mydomainhere.com/" />
</cross-domain-policy>
As it said I put it on the http://files.mydomainhere.com/ server.
==
EDIT
I changed the www. simply to a * which made it work. That's when I removed the www. bit and it still works like a charm. my quess is my forum doesn't work at www. but just a http://
Thanks for all the help people :)
You're probably having a path issue, meaning Flash can't find the specified file. Flash loads external files starting from its containing page location, eg: the PHP page that contains it, not where the SWF is located.
Further, path may be handled differently on the web server than on the local file system. If you are using relative paths, try adding "./" onto the path, or removing "./" if it is already there.
If you are using absolute paths, make sure the path is in the same domain as the SWF. If the browser is at http://www.mysite.com but Flash is loading the file from http://mysite.com (no www) it is actually in a different security domain, and outside of your sandbox. Make sure they are the same.
If you post some more details, we'll be better able to identify your problem.
I hope that helps,
Edit
Based on comment below, you need a cross domain file on the other server. Lots of info on the web about those, start here for a good guide: http://kb2.adobe.com/cps/142/tn_14213.html
SWF's have different security rules based on where you are running them, when you are debugging through one of the IDE's the security settings are very relaxed.