-1

i tried to create a script to redirect link of amazon, but when pass the link in a get variable url not return Url after the character &, i tried to echo the variable $_GET['tolink'] but display a cut url, my code:

$url = $_GET['tolink']; header("HTTP/1.1 301 Moved Permanently"); header("Location: $url");

when pass the url in a variable thi code redirect to :

https://www.amazon.com/SAMSUNG-50-Inch-Crystal-AU8000-Built/dp/B08Z1RN7NP/ref=sr_1_2?keywords=samsung%20tv

intestead to

https://www.amazon.com/SAMSUNG-50-Inch-Crystal-AU8000-Built/dp/B08Z1RN7NP/ref=sr_1_2?keywords=samsung+tv&qid=1685363216&sr=8-2

i tried also just to var_dump( $_GET['tolink']) and echo cut url, why ?

UPDATE: found a solution, use $_SERVER['QUERY_STRING'] intestead $_GET['tolink'] it's works but is a safe solution ?

gassiopea
  • 7
  • 2
  • 1
    If your URL contains another URL inside `tolink`, you must URL encode that variable, otherwise it will cut as you describe – Justinas May 29 '23 at 12:49
  • How **exactly** do you pass the URL? As this is tagged with PHP 7 specifically: are you sure this only happens in PHP 7, and not in other versions? Also, when you dump the variable, what happens? – Nico Haase May 29 '23 at 12:49
  • @NicoHaase, example of a link mydomain.com/redirect.php?tolink=https://www.amazon.com/SAMSUNG-50-Inch-Crystal-AU8000-Built/dp/B08Z1RN7NP/ref=sr_1_2?keywords=samsung+tv&qid=1685363216&sr=8-2 , yes i use php7, when dump the variable a isee the cut verison of url where the caracther after & not appears – gassiopea May 29 '23 at 12:56
  • 1
    Please add all clarification to your question by editing it. The URL you've shared does not match the URL from your question, and it doesn't even contain any `&` – Nico Haase May 29 '23 at 12:56
  • @NicoHaase it as 2 & caracther in link below – gassiopea May 29 '23 at 13:00
  • Add **all** clarification to your question by editing it. The comment shows a `tolink` parameter that does not use any ampersands, and it doesn't even start with `https://www` like the URL from your question – Nico Haase May 29 '23 at 13:01
  • Where exactly are you creating that URL? Where are you putting the Amazon URL into a query parameter? You need to properly URL-encode the value at that point. – deceze May 29 '23 at 13:09
  • Hello! It sounds like you'd like to pass the Query String you receive in the 301 redirect. Your problem is that the `&` you were given is not being included when you construct the url. As you've discovered `$_SERVER['QUERY_STRING']` will usually contain the query string given. However, for cases where I want to re-pass a query string via php I like to use [`http_build_query`](https://www.php.net/manual/en/function.http-build-query.php). In this case `http_build_query($_GET)` will take the given query string and re-encode it. – jeremysawesome May 29 '23 at 15:51

2 Answers2

2

A URL may have parameters, like

theurl?param1=val1&param2=val2

so, if you have & inside the value of your URL parameter, it is being mistaken for the separator between URL parameters and this is why it is being cut.

If your URL's tolink parameter was defined in your PHP code, then you can use urlencode, like this


<?php
echo '<a href="someurl?tolink=', urlencode($thelink), '">';
?>

Otherwise, if it was defined at your Javascript code, then you can use encodeURIComponent, like this:

var url = `someurl?tolink=${encodeURIComponent(thelink)}`;

If you hard-coded it, then replace the & with %26.

EDIT

Based on the discussion in the comment-section it looks like there was not much that could be done with the parameter definition, as it was defined somewhere else and, as a result a broken link was received. Hence, $_SERVER['QUERY_STRING'] helped in solving the issue, as that contained the full query string.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • the URL's tolink parameter are not definited in my code and i can't touch it how is composed, this is why i have pass in some way the link like the example – gassiopea May 29 '23 at 13:22
  • @gassiopea Then you're getting a *broken* URL which has been improperly put together. Look into `$_SERVER` to find the original unaltered request URL, and process it yourself from there. – deceze May 29 '23 at 13:38
  • @deceze yes right, i have found some minutes ago the $_SERVER['QUERY_STRING'] string and seems this solve my problem – gassiopea May 29 '23 at 13:40
-1

1. Use urlencode() when passing the URL as a parameter:

$amazonURL= 'your_amazon_url_here';

$encodedURL = urlencode($amazonURL);

// then you can use $encodedURL as a parameter

2. Use urldecode() when using the URL:

$url = urldecode($_GET['tolink']);

header("HTTP/1.1 301 Moved Permanently"); 

header("Location: $url");
Alka Singla
  • 369
  • 2
  • 7
  • You do *not* need to `urldecode` a value obtained from `$_GET`; those values are already parsed and decoded. – deceze May 29 '23 at 13:07
  • i have already tried this not work, the url are cut i dont know why – gassiopea May 29 '23 at 13:08
  • @deceze yes maybe this is the problem, are alredy parsed, how avoid this ? – gassiopea May 29 '23 at 13:09
  • @gassiopea No. The issue is in the code that does `'...?tolink=' . $amazon_url` somewhere. Since you have not shown this code, we can only speculate. – deceze May 29 '23 at 13:11