Having problem with detection %20 in url

Discussion in 'PHP' started by loop, Mar 28, 2014.

  1. #1
    I want to this type of url:

    site.com/search-digital%20point.html

    redirect with php to:

    site.com/search-digital+point.html

    using this code,and it work if characters "-" or "_" is found, but having problem to detect "%20" in url:

    $pos1=strpos($_GET['search'],"_");
    $pos2=strpos($_GET['search'],"-");
    $pos3=strpos($_GET['search'],"%20");
    if($pos1 || $pos2 || $pos3){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: search-".str_replace(array("_","-"," ","%20"),"+",$_GET['search']).".html");    
    }
    Code (markup):
     
    loop, Mar 28, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    You've got a logic disconnect there -- any %20 should already be decoded as spaces by the time it gets to $_GET. That's what they DO. If you had:

    test.php?search=dumb%20ditties%20by+ktel

    $_GET['search'] would == 'dumb ditties by ktel';

    They would already be spaces. They're already decoded... same for +, which is why + is encoded as %2B

    I'd also suggest using urlencode to pass it back out since you might have other characters that are invalid and need escaping with %. (it's NOT just about spaces!). You're brute forcing something there's a function to already do PROPERLY.

    Though really what you are trying to do is going to get WAY too pointlessly convoluted; much less trying to use redirects to turn dynamic search results into static files?!? That's just asking for it to /fail/ as there's too many possible combinations.

    That said, the first thing I'd do is change the _ and - to spaces, then test if there are any spaces in $_SEARCH. If so, redirect using urlencode.

    $search = str_replace(['_', '-'], ' ', $_GET['search']);
    if (strpos($search, ' ') !== false) {
    	header('HTTP/1.1 301 Moved Permanently');
    	header('Location: search-' . urlencode($search) . '.html');
    }
    Code (markup):
    Hope this helps. (P.S. I'm using PHP 5.4 style array there, so if you're on 5.3/lower change that to Array())

    -- edit -- doh, also you want to test !== false, since 0 could still mean there's a space in there. Really though if it's a url, + or %20 shouldn't even matter as they're 100% interchangeable.
     
    deathshadow, Mar 28, 2014 IP
    digitalpoint likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Oh, when you get stuck processing input like this, it often helps to run a test output to show you whats going on. If you run this as test.php:
    <?php
    if (isset($_GET['test'])) echo '
    <pre>Test result : ', $_GET['test'], '
      Should be : this is a+test</pre>';
    ?>
    <br />	
    <a href="test.php?test=this+is%20a%2btest">Test</a>
    Code (markup):
    .. and click on the 'test' anchor, the output will be:
    Test result : this is a+test
      Should be : this is a+test
    Code (markup):
    As you can see the %20 and + are turned into spaces, while the %2B is turned into a plus when it's plugged into $_GET automatically for you. When it doubt, echo it out.

    This in the URL:
    this+is%20a%2btest

    Is this in $_GET
    this is a+test

    Oh, and don't feel bad if you don't get it right away, character escaping usually feels like a convoluted mess for the first... eh, decade or so you deal with it.
     
    Last edited: Mar 28, 2014
    deathshadow, Mar 28, 2014 IP
  4. akshat113

    akshat113 Member

    Messages:
    17
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #4
    I can code it for you... foor more info mail me at
     
    akshat113, May 1, 2014 IP