Pass complex URL as parameter in PHP

后端 未结 2 1915
感情败类
感情败类 2021-02-14 08:18

I\'m trying to pass a complex URL as a url parameter but the problem occurs if the url contains & for example I want to pass the following link as a parameter

http:

相关标签:
2条回答
  • 2021-02-14 08:39

    From what I understand, you'll need to replace & into &.

    $url = str_replace('&', '&', $url);
    

    & is reserved, used for separating GET parameters, & is for writing a literal ampersand in a URL.

    0 讨论(0)
  • 2021-02-14 08:48

    Use urlencode() or rawurlencode().


    You wrote:

    I'm trying to get a URL as a parameter from a user and redirect user to this URL.

    But you didn't answer the question - how do you get that URL? How does user provide you with it? Is it written in <input type='text'/>? Or does user click some link that contains URL as one of parameters? Or is passed as ID of some URL that is stored in DB?

    One case that comes into my mind - replacing URLs in plain text and sending user to some "redirecting page" before opening real page, so final page does not see HTTP referrer which might contain some secure data (e.g., session ID). In this case, you would write

    <a href='redirect.php?link=<?php echo rawurlencode($url); ?>'>
        <?php echo htmlspecialchars($url); ?>
    </a>
    

    instead of

    <a href='redirect.php?link=<?php echo $url; ?>'>
        <?php echo htmlspecialchars($url); ?>
    </a>
    
    0 讨论(0)
提交回复
热议问题