How to remove invalid query string using php header location

I have this incorrect link, hard-coded in software, which I cannot change.

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

I would like to use the php header location to redirect it to a valid url that does not contain the request. I would like to pass only the q = parameter.

I tried

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

But it just passes the invalid query string to a new location in addition to its weird change

This is the destination that I get, which is also incorrect

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

+4
source share
2 answers

, # .

Stretch, , q - URL-. URL- :

<?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);
?>

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)

, URL- string script, .

, PHP ( #), . , F12.

http://www.16start.com/results.php, JavaScript .

+1

strstr(), ( q=) .

:

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

0

Source: https://habr.com/ru/post/1584954/


All Articles