Question

Is it possible to include some message in a PHP header:

header("Location: http://somesite.com");
header("Message: hello");

then on site.com:

$message = some_function(); // "hello"

I am currently using a $_GET parameter in the URL, but looking for an alternative, maybe sending a $_POST?

I'm trying to not use $_GET, or use cookies (I know, those are the best ways..)

Was it helpful?

Solution

It sounds like you are wanting to send some extra data to the page you are redirecting to. No, this isn't possible outside of the query string. You should understand what is happening here.

When you send a 302 or 301 status code along with a Location: header, the browser sees this and then makes a separate request to the URL specified by the Location: header. The server isn't sending anything to that page. It's almost as if the user simply typed in that new URL in their browser.

I say almost because in some circumstances, there is a referrer set by the browser. This isn't guaranteed though.

What you can do is send some sort of token that contains more information. Perhaps your page saves off a message in a database or something, and then you pass the ID in the query string of the URL you're redirecting to.

Also, if you set session/cookie data and you're redirecting to something on the same domain, you can read that information on the page the user eventually lands on.

OTHER TIPS

In addition to what Brad suggested, you can also send some info using # in the url without affecting the query string and then capture it with js.

header("Location: http://somesite.com#success");

in js:

if(window.location.href.indexOf('#success')>0) {
    alert("operation successfully completed");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top