Page 1 of 1

Anchor tags break Query Strings with php?

Posted: Mon Feb 06, 2012 9:30 am
by Eric!
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:

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;
}
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. :?

Re: Anchor tags break Query Strings with php?

Posted: Mon Feb 06, 2012 1:36 pm
by tr0gd0rr
Your URL is incorrect. It should be either `http://mysitesucks.com/test.php?redirect=1#anchor` or `http://mysitesucks.com/test.php#anchor%3Fredirect%3D1`. The former has a query string and an anchor. The latter has an anchor only.

If site 1 has backwards URLs like you are using, then yes, a JavaScript solution to correct the funky URLs would work.

As a side note, you can use parse_str() instead of your block that begins with `if(isset($_POST['query'])) {` but I don't think you want to use that block anyway.

Re: Anchor tags break Query Strings with php?

Posted: Mon Feb 06, 2012 1:50 pm
by tr0gd0rr
BTW, here is a JS function that fixes your jumbled URLs:

Code: Select all

function fixUrl(url) {
    return url.replace(/(#.*?)(\?.+)$/, '$2$1');
}

Re: Anchor tags break Query Strings with php?

Posted: Mon Feb 06, 2012 6:59 pm
by Eric!
Hey thanks! I didn't know anchor tags had to be at the end. The jumbled URL was of my own creation during the redirect. Hopefully that will be the simple fix I'm looking for and I can drop the JS stuff.

Re: Anchor tags break Query Strings with php?

Posted: Tue Feb 07, 2012 6:30 pm
by Eric!
An interesting side note is the browser strips anchor tags from the GET requests so the server never really sees that stuff. I spent a lot of time trying to trick the browser into sending it because I had the query tag and the anchor tag placement backwards which confused the browser but kept it from stripping the tag.