Anchor tags break Query Strings with php?
Posted: Mon Feb 06, 2012 9:30 am
My goal is to redirect from Site 1 to Site 2 while telling Site 2 it was redirected without the User directly seeing what is going on yet Site 2 knowing the history so it can provide the proper content. Adding a URL parameter works fine except when there is an anchor tag (#blahblah) for hyperlinks.
I didn't know that if an anchor tag appears in the URL the client will strip the full query tag. I can't use $_POST because it is a php header redirect to another server.
For example the $_SERVER array for a query like http://mysitesucks.com/test.php#anchor?redirect=1 shows the query blank. I thought I was going crazy until I looked at the Firefox headers and saw it strip out the anchor tag and the other items. (While I haven't tested it, I assume the query probably breaks the anchor tag too).
[text]Array
(
[QUERY_STRING] =>
[REQUEST_METHOD] => GET
[REQUEST_URI] => /test.php
[SCRIPT_NAME] => /test.php
[PHP_SELF] => /test.php
)[/text]
So I hacked together a PHP/JS solution, but I don't like it because the JS reposts the client query string every time. This causes the page to load twice all the time. I was curious if anyone had any better thoughts. What I have is similar to this idea:One fix is for Site 1 to strip the anchor tag and reinsert it as a URL parameter and then doing some Javascript on Site 2 to refocus the page on the anchor.... I feel like I'm missing something simple here though. 
I didn't know that if an anchor tag appears in the URL the client will strip the full query tag. I can't use $_POST because it is a php header redirect to another server.
For example the $_SERVER array for a query like http://mysitesucks.com/test.php#anchor?redirect=1 shows the query blank. I thought I was going crazy until I looked at the Firefox headers and saw it strip out the anchor tag and the other items. (While I haven't tested it, I assume the query probably breaks the anchor tag too).
[text]Array
(
[QUERY_STRING] =>
[REQUEST_METHOD] => GET
[REQUEST_URI] => /test.php
[SCRIPT_NAME] => /test.php
[PHP_SELF] => /test.php
)[/text]
So I hacked together a PHP/JS solution, but I don't like it because the JS reposts the client query string every time. This causes the page to load twice all the time. I was curious if anyone had any better thoughts. What I have is similar to this idea:
Code: Select all
//Any link with an anchor tag # will not pass the query
//string to PHP. Javascript can be used to find it and repost
//the value to the php code as $_POST['query']. Then rebuilds
//into the normal $_GET array.
if(isset($_POST['query'])) {
$chars = preg_split('/&/', $_POST['query']);
foreach($chars as $query)
{
$split=preg_split('/=/', $query);
$_GET[$split[0]]=$split[1];
}
}
if(!isset($_POST['query']))
{
echo
<<<TEXT
<script language="javascript">
var str=window.location.href;
var query=str.split("?");
if(query[1]!=null)
{
//Query string present, report via dynamic form
var myForm = document.createElement("form");
myForm.method="post" ;
myForm.action = window.location.href;
var myInput = document.createElement("input") ;
myInput.setAttribute("name", "query") ;
myInput.setAttribute("value",query[1]);
myForm.appendChild(myInput) ;
document.body.appendChild(myForm) ;
myForm.submit();
document.body.removeChild(myForm);
}
</script>
TEXT;
}