7

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?

sheetal_158
  • 7,391
  • 6
  • 27
  • 44
  • you should use the full not a relative URI in header-location –  Apr 26 '16 at 23:00
  • 2
    you should also make sure that nothing else has been `echo`'d to the screen before you make the call to `header` – windrunn3r.1990 Apr 26 '16 at 23:05
  • @windrunn3r.1990 he would\should get an error message if that was the case –  Apr 26 '16 at 23:05
  • Oh yeah! Forgot about that. Thanks – windrunn3r.1990 Apr 26 '16 at 23:07
  • 2
    of course a lot of people are silly and develop with error checking\display turned off, so, its not an invalid point :_0 –  Apr 26 '16 at 23:08
  • What is the value of `$query`? – The Codesee May 04 '16 at 15:24
  • I'm confused by what you're trying to achieve and how you know it doesn't work. the `header("Location:` function implies you're trying to redirect to another page, yet you mention `file_put_contents` as if that's the defining point of failure which is used to save a file to disk. this had better not be a [flack overstow](http://stackapps.com/questions/306/flack-overstow-generate-spam-from-stack-exchange-posts) post – Jeff Puckett May 11 '16 at 02:39

11 Answers11

12

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

Webeng
  • 7,050
  • 4
  • 31
  • 59
2

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.

Jehy
  • 4,729
  • 1
  • 38
  • 55
1

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

Community
  • 1
  • 1
shivani parmar
  • 314
  • 2
  • 17
1

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.

yergo
  • 4,761
  • 2
  • 19
  • 41
0

you can try this.I hope it will help

header('Location: http://www.example.com/search.php?q='.$query);
Naisa purushotham
  • 905
  • 10
  • 18
0

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

gmfm
  • 659
  • 8
  • 21
0

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.

Jehy
  • 4,729
  • 1
  • 38
  • 55
illeas
  • 300
  • 3
  • 18
0

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

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!

LipESprY
  • 291
  • 2
  • 8
0

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

Try this one.

header('Location: http://www.example.com/search.php?q='.$query);
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
0

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.