Home » Php » Header Location not working in Php

Header Location not working in Php

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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...".

Answer:

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.

Answer:

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

Answer:

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.

Answer:

you can try this.I hope it will help

header('Location: http://www.example.com/search.php?q='.$query);

Answer:

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).

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);

Answer:

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!

Answer:

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.

Answer:

You need to replace plus(+) by dot(.) to concatenate the string.

Try this one.

header('Location: http://www.example.com/search.php?q='.$query);

Answer:

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.