doouzlrvb01417498 2016-04-08 06:08
浏览 18
已采纳

获取Url参数

I am having a simple problem. I need to get the parameter value from url.

URL is like this:

http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

I need to get the value of someUrl in php.

What i tried:

$_GET['someUrl'];

Output I am getting is http://www.example.com/folder/index.htm

Output need to get :

http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

Note: I can't separate the URL after # into new parameter.

I hope i am able to clear the question.

EDIT: As per the valuable feedback, i have encoded the string before sending into as a URL parameter.

I did encoding by javascript function encodeURIComponent Now URL become like below:

http://www.example.com/index.php?someUrl=http%3A%2F%2Fexample.com%folder%2Ftallyhelp.htm%23%253Cid%3D18025%253E%253Enewwnd%3Dfalse

After decoding in php by urldecode function, i am getting the result http://www.example.com/folder/index.htm#>newwnd=false 'id' remove from the result.

  • 写回答

1条回答 默认 最新

  • dongtao9158 2016-04-08 06:37
    关注

    Your problem is that if your request to your page is this:

    http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false
    

    then whatever is after the # is an anchor tag, that means whatever follows is not a parameter that you can $_GET, its just an anchor tag. so you need to encode that # (and some other strange characters that can cause more problems)

    so if you manage to make your request to your page like this:

    http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse
    

    then you will be able to use $_GET['someUrl']; to get the value you want because the # will be encoded (among other characters too)

    so how to make your request like this? simply use urlencode to encode your someUrl parameter.

    see how it works

    echo urlencode('http://www.example.com/folder/index.htm#<id=18025>>newwnd=false');
    

    so after encoding your links that pointing to that page will be like this

    <a href="http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse">my link</a>
    

    as you can see the # will be encoded and that will stop it from messing with the request's url and you will be able to get this string via $_GET['someUrl'];

    UPDATE for OP's new edit:

    if you encoded with js's encodeURIComponent then use rawurldecode() in PHP. see How to decode the url in php where url is encoded with encodeURIComponent() which has another answer also that may help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题