$_GET不显示像&这样的字符。

huangapple go评论88阅读模式
英文:

$_GET not display character like &

问题

I tried to create a script to redirect a link on Amazon, but when I pass the link as a GET variable, the URL doesn't return everything after the character "&." I tried to echo the variable $_GET['tolink'], but it displays a truncated URL. My code is as follows:

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

When I pass the URL as a variable, this code redirects to:

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

Instead of:

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 also tried to use var_dump($_GET['tolink']) and it still echoed a truncated URL. Why?

UPDATE: I found a solution. I used $_SERVER['QUERY_STRING'] instead of $_GET['tolink'], and it works. But is this a safe solution?

英文:

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 ?

答案1

得分: 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

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 &amp; 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.

英文:

A URL may have parameters, like

theurl?param1=val1&param2=val2

so, if you have &amp; 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


&lt;?php
echo &#39;&lt;a href=&quot;someurl?tolink=&#39;, urlencode($thelink), &#39;&quot;&gt;&#39;;
?&gt;

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 &amp; 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[&#39;QUERY_STRING&#39;] helped in solving the issue, as that contained the full query string.

答案2

得分: -1

  1. 在将URL作为参数传递时使用urlencode()函数:
$amazonURL = 'your_amazon_url_here';
$encodedURL = urlencode($amazonURL);
// 然后您可以将$encodedURL作为参数使用
  1. 在使用URL时,请使用urldecode()函数:
$url = urldecode($_GET['tolink']);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
英文:

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

$amazonURL= &#39;your_amazon_url_here&#39;;

$encodedURL = urlencode($amazonURL);

// then you can use $encodedURL as a parameter

2. Use urldecode() when using the URL:

$url = urldecode($_GET[&#39;tolink&#39;]);

header(&quot;HTTP/1.1 301 Moved Permanently&quot;); 

header(&quot;Location: $url&quot;);

huangapple
  • 本文由 发表于 2023年5月29日 20:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定