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.
Related
I have the following folder structure
/main/site/
the redirect script is in the following dir
/main/site/backend/
header('Location: ../register.php');
returns to /main/register.php/ when it should go to /main/site/register.php
It seems all ok in the code , since ../ should go back one dir,
someone know what is wrong?
It does not matter where your script resides - every Location instruction applies to what the outside looks like. If the script is requested thru https://www.example.com/main/site/backend/filename.php then it must redirect to ../../register.php or even better /main/register.php.
Try changing to this
header('Location: ./../register.php');
./ Forces PHP to look only in current directory
See this post
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/..'
I'm trying to dynamically detect the root directory of my page in order to direct to a specific script.
echo ($_SERVER['DOCUMENT_ROOT']);
It prints /myName/folder/index.php
I'd like to use in a html-file to enter a certain script like this:
log out
This seems to be in bad syntax, the path is not successfully resolved.
What's the proper approach to detect the path to logout.php?
The same question in different words:
How can I reliably achieve the path to the root directory (which contains my index.php) from ANY subdirectory? No matter if the html file is in /lib/subfolder or in /anotherDirectory, I want it to have a link directing to /lib/logout.php
On my machine it's supposed to be http://localhost/myName/folder (which contains index.php and all subdirectories), on someone else's it might be http://localhost/project
How can I detect the path to application root?
After some clarification from the OP it become possible to answer this question.
If you have some configuration file being included in all php scripts, placed in the app's root folder you can use this file to determine your application root:
$approot = substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']));
__FILE__ constant will give you filesystem path to this file. If you subtract DOCUMENT_ROOT from it, the rest will be what you're looking for. So it can be used in your templates:
log out
Probably you are looking for the URL not the Path
log out
and you are not echoing the variable in your example.
Your DOCUMENT_ROOT is local to your machine - so it might end up being c:/www or something, useful for statements like REQUIRE or INCLUDE but not useful for links.
If you've got a page accessible on the web - linking back to a document on C: is going to try and get that drive from the local machine.
So for links, you should just be able to go /lib/logout.php with the initial slash taking you right to the top of your web accessible structure.
Your page, locally - might be in c:/www/myprojects/project1/lib/logout.php but the site itself might be at http://www.mydomain.com/lib/project.php
Frameworks like Symfony offer a sophisticated routing mechanism which allows you to write link urls like this:
log out
It has tons of possibilities, which are described in the tutorial.
Try this,
log out
This jumps to the root directly.
DOCUMENT_ROOT refers to the physical path on the webserver. There is no generic way to detect the http path fragment. Quite often you can however use PHP_SELF or REQUEST_URI
Both depend on how the current script was invoked. If the current request was to the index.php in a /whatever/ directory, then try the raw REQUEST_URI string. Otherwise it's quite commonly:
<?= dirname($_SERVER["SCRIPT_NAME"]) . "/lib/logout.php" ?>
It's often best if you use a configurable constant for such purposes however. There are too many ifs going on here.
I'm trying to figure this out for PHP as well. In asp.net, we have Request.ApplicationPath, which makes this pretty easy.
For anyone out there fluent in PHP who is trying to help, this code does what the OP is asking, but in asp.net:
public string AppUrl
{
get
{
string appUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
if (appUrl.Substring(appUrl.Length - 1) != "/")
{
appUrl += "/";
}
// Workaround for sockets issue when using VS Built-int web server
appUrl = appUrl.Replace("0.0.0.0", "localhost");
return appUrl;
}
}
I couldn't figure out how to do this in PHP, so what I did was create a file called globals.php, which I stuck in the root. It has this line:
$appPath = "http://localhost/MyApplication/";
It is part of the project, but excluded from source control. So various devs just set it to whatever they want and we make sure to never deploy it. This is probably the effort the OP is trying to skip (as I skipped with my asp.net code).
I hope this helps lead to an answer, or provides a work-around for PHPers out there.
i have some problem i try to get the uri in php.
I'm using:
$_SERVER['REQUEST_URI']
It works just fine if i do it in the index.php, but, i NEED to get the url in a include file, but, when i do it, it takes the FILE adress, i mean, it shows something like this
adress bar: www.webpage.com/index.php
$_SERVER['REQUEST_URI'] output: webpage/includefile.php
I am explaining myself here? Thanks!
How are you including the file? If it's being included via an HTTP reference then it's actually being served as a page and the functionality you are seeing is correct. If the include path is a local file, you shouldn't be seeing this behaviour
Found this whilst trying to solve the same issue.
My solution that worked is to use $_SERVER['HTTP_REFERER']
This worked well in that it also included the parameters (e.g. ?this=that&foo=bar)
Maybe somewhere in your code (or in another include file) the value is overwritten.
I know how to find out the current domain name in PHP already, the problem is when I put this code into a file and then include it from another server it shows the domain name of where the file is located. Is there any way for it to find out the domain or the site containing the include() code?
Are you doing something like:
include 'http://example.com/script.php';
?
NB: This approach generally considered to be a bit of no-no from a security point of view.
Anyway, the included script is actually being executed on the other server, then the output of the script is being executed on the current server. You can get around this by echoing actual code, something like this:
Currently:
<?
//do something
echo '$v = '.$_SERVER['HTTP_HOST'].';'
?>
Other way:
<?
//do something
?>
$v = $_SERVER['HTTP_HOST'];
But then maybe I'm misunderstanding your question.
You can run it locally using "eval" then it should use the proper domain
store your script as a text file then download it and then execute:
eval(file_get_contents("http://someDomain.com/somePhpscript.txt"));
If you include a PHP page from another server, the page will get parsed by the original server and the result will be sent to you - the page you receive is nothing but text, no PHP code included.
This is a crude hack, but on the remote server, you could look up the domain name of $_ENV['REMOTE_HOST'].
This would be the domain name of the guy doing the "include" from the perspective of the remote server.
I assume you have some reason for wanting to implement this strange topology--restrictions in a virtual host environment, or something. I would suggest looking into alternative infrastructure if possible.