0

I created some PHP code where I collect data from another server. I confirmed the data is ok with a bunch of print statements like:

print "<br /><br />headline: " . $user->headline ;
print "<br /><br />industry: " . $user->industry ;

That all works fine. But instead of using print to show the contents, I want to redirect to another URL on a different server and send that info over. So I commented out the print lines and instead created a set of query parameters and used the header function to try to redirect, as follows:

$params = '?success=ok&uname=' . $_SESSION['uname']  . '&odistate=' . $_SESSION['odistate'];
$params .= '&firstName=' . $user->firstName . '&lastName=' . $user->lastName . '&headline=' .   $user->headline . '&industry=' . $user->industry;
$params .= '&summary=' . $user->summary . '&publicProfleUrl=' . $user->publicProfileUrl . '&emailAddress=' . $user->emailAddress;
$params .= '&numRecommenders=' . $user->numRecommenders;

$url = 'http://www.example.org/profileInfo/' . $params;
header("location: $url");
exit;

Nothing happens though. I can go to the page, but the redirection doesn't take place. I know there should be no source prior to the redirect, and I check the source and it is empty. There are no errors in the PHP log. The code was checked for syntax errors and there are none.

If I comment out the redirect section and use the print statements instead, it works fine. But if I comment out the print statements and try to use the redirect section instead nothing happens. Just a blank screen.

If I slip a

print $url;

in just before the header() call naturally it won't redirect then, but I can confirm that the URL appears to be properly formed. There are some spaces in some of the query values though. Could it be that it has to be URL encoded to turn the spaces into %20 or some problem like that?

What might cause the redirect to just not happen?

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
Doug Lerner
  • 1,383
  • 2
  • 17
  • 36
  • 1
    1. take care to urlencode() all values you place inside your parameter string. 2. use your browsers development console (typically F12), and check what the problem is. That is what the development console is for. 3. make sure to not accidentally echo any content before setting your header. A reliable way to do this is by using phps output buffering feature and throwing away any output buffered before your `header()` statement. . – arkascha Jan 10 '15 at 08:21
  • I will next try urlencode() for all the values and see if that works. I'll also try to find the development console. As you might guess from the nature of my question, I'm not that experienced in PHP development. And thanks for the heads up about the output buffering feature. It is similar to something I use in server-side javascript to clear the "response buffer" before attempting a redirect. That might come in handy in the future. I will be back after trying some of your suggested tests. Thanks. – Doug Lerner Jan 10 '15 at 08:32
  • My guess about URL encoded and your confirming that, and providing me with the name of the urlencode() function, solved the problem! It is now redirecting and I see all the results at the other server. You might want to rephrase your suggestion in the form of an answer so I can mark it as such. In Firefox, I opened all the consoles in the Web Developer menu but none gave any useful ino. The URL encoding is a bit unexpected though. All the spaces were replaced by + symbols. Normally I would expect them to be replaced by %20. But I can deal with that. Thanks again! – Doug Lerner Jan 10 '15 at 08:46
  • 1
    The "+" encoding for spaces is what the standard is. Maybe you want to start reading a bit about url rules and the like. It will safe you a lot of hassle. Oh, and: great you sorted out your issue! – arkascha Jan 10 '15 at 08:55
  • You certainly should see the "broken" header in the "Network" tab of your developer console. It visualizes headers of request and response. That broken one should be shown in the response header (not the response tab, since there is no content in the response). – arkascha Jan 10 '15 at 08:58
  • I think for my purposes rawurlencode() was better than urlencode() for better compatibility with my other server, which needs to decode it in server-side JavaScript. It doesn't work well with the "+" encoding and prefers the "%20" encoding. When I switched out everything was perfect. Still have seen any header info in the "Network" tab of the dev console, but will look some more. – Doug Lerner Jan 10 '15 at 12:46

3 Answers3

1

Setting headers doesn't work if you output anything, even a space character before setting the header.

Make sure you don't have any output beforehand like print, echo, var_dump and others as well as make sure you don't have a space, line break or any other characters before <?php at the beginning of the file or any other include()'d or require()'d file.

If that is not the case, see if the header is being properly set by opening your page and the developer tools in Chrome or Firefox.

  • Open the page
  • Open developer tools (ctrl+shift+j)
  • Refresh the page
  • Go to Network section of developer tools and open your request and the Headers tab
  • See if your header is set and if it's URL is formed properly.
Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • Right. I realized there can't be anything output before setting the header. That's why I mentioned I was careful not to, and also examined the source of the page "created" and there was nothing in it at all, and there are no include() or require() statements. But of course I will double-check. Thanks. – Doug Lerner Jan 10 '15 at 08:30
  • @DougLerner you should not check the source of the page, but rather the headers of the HTTP response, [see here](http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome) – Sergey Telshevsky Jan 10 '15 at 08:31
  • thanks for pointing those out to me. In this particular case, the problem did not turn out to be output before setting the header. The params just needed URL encoding. On my Mac in Firefox the ctrl+shift_j combo doesn't work, but I can open the developer tools via the menu and go to the Network section. Inside there I don't see a Headers tab, though I do see some AJAX calls running in the background. This is not at my PHP page at the moment because I'm getting redirected away from there of course! But I will play around some more and see what I can find. Thanks! – Doug Lerner Jan 10 '15 at 09:08
0

Copied here from my comment above:

  1. take care to urlencode() all values you place inside your parameter string.

  2. use your browsers development console (typically F12), and check what the problem is. That is what the development console is for.

  3. make sure to not accidentally echo any content before setting your header. A reliable way to do this is by using phps output buffering feature and throwing away any output buffered before your header() statement.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • As mentioned above, you are correct that all the values needed to be url encoded with urlencode(). I sort of suspected that (as mentioned in my question), but your answer pointed me to the correct function to use and confirm that, and because of that I was able to quickly fix it and it just works. So I appreciate the time you took to respond. Thank you! – Doug Lerner Jan 10 '15 at 09:01
-1

Try header("location: " . $url); instead of header("location: $url");.

First solution concats the content of your $url var to redirection, whereas other one redirects your website to "$url" path which doesn't exist, obviously.

Adrien Cadet
  • 1,311
  • 2
  • 15
  • 21
  • 2
    Sorry, but this is total nonsense. The effect is _exactly_ the same: a concatenated string sent as a header. The string is composed of the literal string and the value of variable `$url`. – arkascha Jan 10 '15 at 08:19
  • Aw, sorry for that then. However I don't get it. Usually, in PHP, "location: $url" does not append your `$url` var to the string. It only appends the "$url" string, right? – Adrien Cadet Jan 10 '15 at 18:07
  • 1
    No! Do yourself a favor and do two thing: 1. start reading at least the most basic documentation about things you use, especially if you want to give advice to others! 2. simply try it yourself! Not that difficult, two lines of code and you see the result. In general: such variable notations inside strings _enclosed in double quotes_ are resolved, so replaced by their value. Just give it a try. – arkascha Jan 10 '15 at 18:24
  • You are completely right. I have never noticed this feature before. Was it added in PHP5? – Adrien Cadet Jan 10 '15 at 18:27
  • A short look into the documentation would have told you: no, it is nothing new, not at all. It was there from the beginning of php back in the early 90th. _Start reading documentation!_ You _cannot_ learn to program without that. – arkascha Jan 10 '15 at 18:28