Anchor tags break Query Strings with php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Anchor tags break Query Strings with php?

Post 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. :?
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Anchor tags break Query Strings with php?

Post 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.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Anchor tags break Query Strings with php?

Post by tr0gd0rr »

BTW, here is a JS function that fixes your jumbled URLs:

Code: Select all

function fixUrl(url) {
    return url.replace(/(#.*?)(\?.+)$/, '$2$1');
}
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Anchor tags break Query Strings with php?

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Anchor tags break Query Strings with php?

Post 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.
Post Reply