Sending a PHP redirect header (301,302,303 etc) with an embedded variable
As explained in a related document and in many other reviews of processes and code on he website, you can create a redirect or send a Header from PHP with any-one of the 301, 302, 303, etc redirect headers. I will not go into the uses of these kinds of redirects (handled in a different document). The purpose of this document is to add to the already described code for generating these headers.
It is not always that you will know where you want to point the user to. Sometimes you may want to use a variable to determine where the user should be pointed to when they visit a page. For example, within the Drupal CMS, and any other automated system, I needed to create a menu link to a value declared in the profile of a given user. The problem was that you can only call that variable easily from the profile page of a user, and not from auxiliary pages. I could however call the profile page from the auxiliary pages. My strategy was therefore to link back to the profile page with a query-string value telling my code there that I want to pick a value from the profile array (available on the profile page), and use that value in the redirect.
Steps to the solution
My approach was to call the profile page (/user/UID?target=my_site).
On the profile page, I put some PHP code to detect this querystring and build a 301 or 302 redirect header based on the value pulled from the profile page.
<?php
$my_site = $user->profile_mysite;
if (($_GET["target"])==("my_website")) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$my_site);
exit();
}
?>
This approach can e used in many other instances (for you to think them up). I ran into this problem long time ago with classic ASP and it was not able to handle this without some tedious coding... unless my knowledge or ASP (or lack thereof) was the stumbling block


