4

I have this invalid link hard coded in software which I cannot modify.

http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search

I would like to use php header location to redirect it to a valid URL which does not contain the querystring. I'd like to pass just the parameter q=.

I've tried

$q = $_GET['q'];
header ("Location: http://www.newURL.com/results.php?" . $q . ""); 

But it's just passing the invalid querystring to the new location in addition to modifying it in a strange way

This is the destination location I get, which is also invalid

http://www.newURL.com/results.php?#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search

2
  • Seems like the query string having a bunch of stuff before the variable is producing the odd behavior. Try using the $_SERVER array to get the query string and then substring everything after the q= Commented Apr 27, 2015 at 17:59
  • what is the syntex to capture the substring into a session variable using $_SERVER Commented Apr 28, 2015 at 20:46

2 Answers 2

1

That's because # is seen as the start of a fragment identifier and confuses the parser.

You can take the easy-way as Stretch suggested but you should be aware that q is the last query parameter in your URL. Therefore, it might be better to fix the URL and extract the query parameters in a safer way:

<?php
$url = "http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search";

// Replace # with its HTML entity:
$url = str_replace('#', "%23", $url);

// Extract the query part from the URL
$query = parse_url($url, PHP_URL_QUERY);

// From here on you could prepend the new url
$newUrl = "http://www.newURL.com/results.php?" . $query;
var_dump($newUrl);

// Or you can even go further and convert the query part into an array
parse_str($query, $params);
var_dump($params);
?>

Output

string 'http://www.newURL.com/results.php?cof=GALT:%23FFFFFF;GL:1;DIV:%23FFFFFF;FORID:1&q=search' (length=88)

array
  'cof' => string 'GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1' (length=37)
  'q' => string 'search' (length=6)

Update

After your comments, it seems that the URL is not available as a string in your script and you want to get it from the browser.

The bad news is that PHP will not receive the fragment part (everything after the #), because it is not sent to the server. You can verify this if you check the network tab in the Development tools of your browser F12.

In this case, you'll have to host a page at http://www.16start.com/results.php that contains some client-side JavaScript for parsing the fragment and redirecting the user.

Sign up to request clarification or add additional context in comments.

5 Comments

hi wouter. the q= is dynamic. so i must create a session variable from the current URL. and that isint working. str_replace simply does not parse anything after the first "#" it finds in the querystring
I don't think I can follow. But in your question I see that you try to use $_GET so I guess the user is browsing to that incorrect URL (instead of having it as a variable in the PHP script), is this correct?
yes. the user browses to that url. in the landing page of that url i want to redirect him to a correct URL
I'm afraid you can't do that server-side, because the fragment (everything after the #) is not sent to the server. You can see this if you inspect the network using f.e. Chrome Debugger. What you can do is get the hash-fragment from the client-side. So you can embed some JavaScript on that page that parses the fragment and redirects the user.
Hi Wouer. thanks! thats a good idea. and i wont even have to redirect if i can reformat the url beore the page loads. do you have javascript syntex for parsing and removing the invalid part of the querystirng and make it execute before the page loads?
0

one way could be to use strstr() to get everything after (and including q=) in the string.

So:

$q=strstr($_GET['q'],'q=');

Give that a whirl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.