i have developed a php application which is running perfect on local server. when i deployed the application on web server the links are not working
1) my site is "abc.myapplication.com" (abc is subdomain)
i defined following variable in config file
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
ROOT_PATH variable shows /home/punjabfo/public_html/abc (which is perfect)
for link i used following code
Add Record
link should go to "abc.myapplication.com/addrecord.php" but link go to
"abc.myapplication.com/home/punjabfo/public_html/abcaddrecord.php"
i tried a lot but could not fin the issue. please help. thanks
What is wrong with Keeping It Simple
Add Record
Let the server do all the work, as it gets it right and you do less messing around.
Try
define('ROOT_PATH', $_SERVER['HTTP_HOST']);
Just use
Add Record
You can try -
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.parse_url($url, PHP_URL_HOST) . '/';
Your issue is "$_SERVER['DOCUMENT_ROOT']". $_SERVER['DOCUMENT_ROOT'] stands for the root directory on the server (dir-path). What you need, is the URL and not the file-system-path.
Take a look on
<?php
echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
?>
Why not to do
Add Record
Of course you do not need ROOT_PATH in the URL. What you do is returning full path of the file, instead of link. And btw, full path is incorrect itself, as you forgot slash before addrecord.php.
Related
I must not be phrasing this question right because I couldn't find an answer to this but surely it's been asked before. How do I get the current filename from a URL if it's the directory's index file?
I.e. This will get index.html if I'm on www.example.com/index.html
$url = basename($_SERVER['REQUEST_URI']);
But that won't work if i'm on www.example.com. The only thing I've come up with so far is something like this:
$url = basename($_SERVER['REQUEST_URI']);
if($url == "") {
$filename = "index.html";
}
But that's obviously a bad solution because I may actually be on index.htm or index.php. Is there a way to determine this accurately?
$_SERVER['SCRIPT_FILENAME'] will determine the full path of the currently executing PHP file. And $_SERVER['SCRIPT_NAME'] returns just the file name.
This is one of the other methods.
$url = basename($_SERVER['REQUEST_URI']);
$urlArray = explode("/",$url));
$urlArray = array_reverse($urlArray);
echo $urlArray[0];
Unfortunately, the $_SERVER array entries may not always be available by your server. Some may be omitted, some not. With a little testing though, you can easily find out what your server will output for these entries. On my servers (usually Apache) I find that $_SERVER['DOCUMENT_ROOT'] usually gets me the base of the URI I'm after. This also works well for me in the production environment I work on (XAMPP). As my URI will have a localhost root. I have seen people encourage DOCUMENT_ROOT before in this situation. You can read all about the $_SERVER array here.
In this example, I get the following results:
echo $_SERVER['DOCUMENT_ROOT'] ; // outputs http://example.com
If you are working in a production environment this is very helpful because you won't have to modify your URL's when you go live:
echo $_SERVER['DOCUMENT_ROOT'] ; // outputs C:/xampp/htdocs/example
'DOCUMENT_ROOT' The document root directory under which the current
script is executing, as defined in the server's configuration file.
You could find the last occurrence of the slash / using strrchr() and simply extract the rest using substr(). The optional parameter in substr() tells where to begin. With one we skip the slash /. If you want to keep it, just set the parameter to 0.
echo substr( strrchr( "http://example.com/index.html" , "/" ) , 1 ) ; // outputs index.html
EDIT: Considering that not every server will provide $_SERVER with entities, my approach might be more reliable. That is, if the URL you pass to strrchr() is reliable. In either case, make sure you test the different outputs from $_SERVER, or your paths you provide.
Might be an easy question for you, but I'm breaking my head over this one.
I have a php file that needs to know it's current directory url to be able to link to something relative to itself.
For example, currently I know to get the current directory path instead of the url. When I use this I get the path:
realpath(__DIR__)
result:
/Applications/MAMP/htdocs/mysite/dir1/dir2/dir3
But this would be my desired result:
http://localhost:8888/dir1/dir2/dir3
Note that this is not the location of the current page. The page calls a file from "http://localhost:8888/dir1/dir2/dir3/myfile.php"
And "myfile.php" has the script from above.
-- edit to elaborate more details --
Thanks for your answers. But I get that I need to add more detail.
http://localhost:8888/index.php calls: "http://localhost:8888/dir1/dir2/dir3/myfile.php"
"myfile.php" needs to know it's place in the universe :) "Where am I"
"myfile.php" should know it's url location is "http://localhost:8888/dir1/dir2/dir3/"
Use echo $_SERVER['PHP_SELF'];
For example if the URL is http://localhost/~andy/test.php
The output would be:
/~andy/test.php
That's enough to generate a relative URL.
If you want the directory your current script is running in - without the filename - use:
echo dirname($_SERVER['PHP_SELF']);
In the case above that will give you /~andy (without test.php at the end). See http://php.net/manual/en/function.dirname.php
Please note that echo getcwd(); is not what you want, based on your question. That gives you the location on the filesystem/server (not the URL) that your script is running from. The directory the script is located in on the servers filesystem, and the URL, are 2 completely different things.
There is also a function to parse URL's built in to PHP: http://php.net/manual/en/function.parse-url.php
If your URL is like this: https://localhost.com/this/is/a/url
$_SERVER['DOCUMENT_ROOT'] - gives system path [/var/www/html/this/is/a/url]
$_SERVER['PHP_SELF'] - gives the route of the current file (after the domain name) [/this/is/a/url]
$_SERVER['SERVER_NAME'] - gives the domain name [localhost.com]
$_SERVER['HTTP_REFERER'] - gives the correct HTTP(S) protocol and domain name. [https://localhost.com]
If you would like to get the full url, you can do something like:
echo $_SERVER['HTTP_REFERER'] . $_SERVER['PHP_SELF'];
However, I do believe in this case, that all you need is the relative path.. and in that case you should only need to use $_SERVER['PHP_SELF'];
I've found a solution here:
https://stackoverflow.com/a/1240574/7295693
This is the code I'll now be useing:
function get_current_file_url($Protocol='http://') {
return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__));
}
Based on your question, I believe this will get you what your want:
$_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/"));
Reference:
$_SERVER['HTTP_HOST'] - In your case this would return: http://localhost:8888
$_SERVER['REQUEST_URI'] - In your case this would return: /dir1/dir2/dir3/myfile.php
With the added substr() and strrpos() methods, you can strip the _myfile.php` off of the end to get the desired result:
http://localhost:8888/dir1/dir2/dir3
So, I made a simple PHP login, but when I tried to redirect like this:
$path = $_SERVER["DOCUMENT_ROOT"];
header("Location: $path/admin/index.php");
it seemed like it did nothing, but after I refreshed the page I was logged in.
After I changed my code to this:
header("Location: ../admin/index.php");
it works.
Could someone please explain this to me?
Ps. sorry for my bad english
The header is sent to the browser, so it is not an internal server maneuver. And with it not being an internal redirect, you don't deal with internal paths. When you use DOCUMENT_ROOT you will get the internal server path to the directory where your files are located.
If you want to reference the root of the site as a URL, just use /.
header("Location: /admin/index.php");
header("Location: /"); # go to homepage, for example
Your .. worked because you probably were on a subdirectory, and .. was translated to the parent directory which is where admin is.
$_SERVER["DOCUMENT_ROOT"];
returns path like /var/www/html/yourfolder/, but you have to redirect to website.com/yourfolder/ or localhost/yourfolder/.
hence that won't work.
Have you tried printing the value of $path?
the value of $path is relative to the actual file location
e.g. $path = '/c/inetpub/sites/example/main/'
You probably wanted something like '/c/inetpub/sites/example/' or '/c/inetpub/sites/example/main/..'
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 3 years ago.
The following statement doesn't seem to work. I am not getting any error messages either.
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q="+$query);
I am using file_put_contents() in search.php file.
Is there a way to figure out what could be wrong?
change this:
+$query
to this:
.$query
Because:
+ is the concatenation operator for JavaScript, where as . is the concatenation argument for php
Also, the path you are sending seems to be incorrect. The parameters inside the header function should be a complete web address, for example starting with http:
header('Location: http://www.example.com/');
While your answer is using a local file path: "Location: /home/shaliu/Projects/Nominatim...".
Please try:
You can use:
header("Location: http://www.example.com/search.php?q=".$query);
exit();
Or if your search.php file is on root directory:
header("Location: /search.php?q=".$query);
exit();
It may help you.
put error_reporting(E_ALL);
use relative path.
change this with
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q="+$query);
In php for concatenation use ".","+" is used in javascript not in php
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Please make Your path correct for file
After fixing + to ., as said previously by #Webeng.
If you are visiting your page via typing /home/shaliu/Projects/Nominatim/website/index.php in your browser, headers wont work at all, because you are not in web context, but in local file view context.
If you are visiting it via http://localhost/Nominatim/index.php, you will be in web context, but your header wont work, because you are technically sending redirection to path of http://localhost/home/shaliu/Projects/Nominatim/website/search.php, which probably does not exists at this location. Instead of that you should change it to: /search.php if your website/ folder is linked to http://localhost/ or anything adequate, depends on your environement configuration.
you can try this.I hope it will help
header('Location: http://www.example.com/search.php?q='.$query);
header( ) is used to send http headers (http://php.net/manual/en/function.header.php). It looks as though you are trying to access a php file using a local file path and not an http (web) address. "/home/shaliu/Projects/Nominatim/website/search.php" needs to be web accessible and that web address is what needs to be used in header() (see #Purushotham's answer).
If you are using
/home/shaliu/Projects/Nominatim/website/search.php
In this case your location is starting with / that means home folder (directory) should be inside root folder of the server. For example as a local server if we consider XAMPP then home folder should be inside C:\xampp\htdocs (in general) and if we consider WAMP then home folder should be inside www folder.
If your home folder is inside the folder where your current page is, then you should use
home/shaliu/Projects/Nominatim/website/search.php
No / (forward slash required).
Second thing is you need to replace + by . to concatenate the string.
SO, if your home folder is inside root directory of server then you should go with
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Otherwise, you should go with
header("Location: home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Try this code with a test "if the file exists":
if(is_file("/home/shaliu/Projects/Nominatim/website/search.php")) {
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
} else {
echo "Error!";
}
Remember: In php for concatenation use . (#shivani parmar)
Remember: If the header is called after any HTML element, the php returns error!
header("Location:https://www.example.com/search.php?q='<?php echo $get_id; ?>'");
Please try this php code.
if you use header in php first header and location then question mark your search get id.
You need to replace plus(+) by dot(.) to concatenate the string.
Try this one.
header('Location: http://www.example.com/search.php?q='.$query);
Aside from the obvious concatenation issue that's already been pointed out, it looks like this path /home/shaliu/Projects/Nominatim/website/search.php is a *NIX path; given the /home/<user> pattern it starts with.
If this is the case, and you are trying to open a file outside of the web server root like that, yours is most likely a permission's issue. Moving your search.php to a location accessible to the web server process and running chmod/chown on the file you are trying to have the web server process open may sort you out. In addition, you may have to specify which OS you are using just in case you also need to run a chcon.
Oh, I would have made this a comment, but it seems like I do not have the necessary rep to add comments.
So, I've found a way to get the current directory using dirname(__FILE__) and getting the domain with $_SERVER['HTTP_HOST']. While both of these are well and good, they aren't quite what I need them to be.
For instance, if I have a script on http://mydomain.com/scripts/myscript.php, I'd like to get http://mydomain.com/scripts/. I feel like there should be an easy way to do this and that I've somehow overlooked something.
As an aside, I am currently using the script in a cloud shared hosting environment, so the directory structure is somewhat odd.
Try:
<?php
echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
The only problem with that is that dirname returns the parent directory, so if you access http://domain.com/scripts/ directly you'll just get http://domain.com/ withouth the scripts. http://domain.com/scripts/script.php resolves correctly to http://domain.com/scripts/ though.
Try:
<?php
echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
function url_part(){
$http=isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$part=rtrim($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME']));
$domain=$_SERVER['SERVER_NAME'];
return "$http"."$domain"."$part";
}
echo url_part;//htts://www.example.net/test