Incorrect value for $_SERVER['DOCUMENT_ROOT'] when open browser from PHPSTORM
Environment:
Windows 7, using WAMP for Apache, MySQL, Php environment install.
the web root path is to "c:\wamp\www"
1. Using PhpStorm to create a empty project:
project name: test
project path: c:\wamp\www\test
2. Creating a file index.php (file location: c:\wamp\www\test\index.php), this file only one line
echo $_SERVER['DOCUMENT_ROOT];
3. In PhpStorm, select "open in browser" (or launch via browser launch bar )
the browser's url is: http://localhost:63342/test/index.php
the echo value for $_SERVER['DOCUMENT_ROOT] is c:/wamp/www/test
now, delete the port part of the browser's url,
the browser's url become: http://localhost/test/index.php
the echo value for $_SERVER['DOCUMENT_ROOT] is c:/wamp/www
The correct value of $_SERVER['DOCUMENT_ROOT] should be
"c:/wamp/www"
but when launch from PhpStorm,the value become
"c:/wamp/www/test"
What's the problem? is this a bug of PhpStorm?
Please sign in to leave a comment.
Hi there,
Everything is correct -- it's not a bug.
The thing is -- the "http://localhost:63342/test/index.php" was NOT served by your WAMP server (Apache) but by built-in simple web server. It requires URL to be in form of "localhost:63342/PROJECT_NAME/index.php" and $_SERVER['DOCUMENT_ROOT'] would be the project root folder indeed.
When you do not have any deployment entry set up for this project, the "Open in browser" action will use built-in server by default.
So .. to make IDE use URLs that will be served by your WAMP, just
1) set up correct deployment entry (Settings | Deployment)
2) mark it as Default for this project.
But anyway!!!
Built-in server use wrong project root for "open in browser". For "run" all fine.
Real project root in local resourses (expl: "/css", "/js", "/img") is "/" === "x:/project_name/." (project folder). But, build-in server project root is "/project_name/.." (parent to project folder). As a result, resources must locate in "/project_folder/project_folder/css" )
P.S. If the PHPStorm environment has a built-in server, why should I use third-party servers?
> Built-in server use wrong project root for "open in browser". For "run" all fine.
I can't reproduce this with the following example:
Even though the generated URI looks like 'localhost:63342/resolve1/index.html?_ijt=m7fqglhcrtrbcol9r8ndl9n199', the resource can be loaded OK.
> P.S. If the PHPStorm environment has a built-in server, why should I use third-party servers?
It's more configurable, and also one doesn't usually test their code in the built-in solutions.
You use relative URI. Try absolute:
<img src="/img/Project%20view.png">
For some resources will be best use absolute URI. (Exmpl: /css/main.css).
For this you need to apply some magic ... to convert
http://localhost:63342/PROJECT_NAME/file.html
to be
http://PROJECT_NAME:63342/file.html
This way resources with leading slash (like /img/Project%20view.png) will work fine
It involves editing your hosts file (or local DNS server you have one) and creating/editing your Deployment configuration (of "In-place" type -- so no file movement is involved). The latest is needed to tell IDE what URL it must use now.
See http://stackoverflow.com/a/32990633/783119
Alternatively: use proper web server (e.g. Apache/nginx/IIS -- it's much better .. plus supports URL rewriting/.htaccess files etc) .. or maybe even PHP built-in web server (where each site will be served from different port .. so no such issues like you have now).
Hi. Again...
I create "In-Place" deployment :
"Web server Root URL" => "http://PROJECT_NAME:63342/", "Local path" => "D:\PROJECT_NAME", "Web path on server local" => "/"...
And write in hosts 127.0.0.1 PROJECT_NAME.
But, root folder in browser is parent to "PROJECT_NAME" => "D:\". Index open only if uri = "http://PROJECT_NAME:63342/PROJECT_NAME/".
Have a nice day...