Need help with custom UBB tag (should be simple)

Discussion in 'PHP' started by x0x, Feb 1, 2009.

  1. #1
       $var = str_replace("[player]","<a href=profile.php?t&id=$player", $var);
        $var = str_replace("[/player]","</a>", $var);
    PHP:

    That is what I've got so far, and it is not working.

    The idea is to let players make links to each other profiles, NOT OUTSIDE THE SITE.

    so, if one was to write [player]Jack[/player]

    the outcome would be:
    <a href="profile.php?id=Jack">Jack</a>
    PHP:
    I messed around with this for a few hours, but still couldn't achieve what I wanted. Please help :confused:
     
    x0x, Feb 1, 2009 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Regex would be better than str_replace here. Something like:
    $var = preg_replace('#\[player\](.+?)\[/player\]#', '<a href="profile.php?id=\\1">\\1</a>', $var);
    PHP:
     
    rodney88, Feb 1, 2009 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thank you! It works nicely. Now my question is, can they modify the link to start stealing each other cookies or whatever. In other words, is it safe to use?
     
    x0x, Feb 1, 2009 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's not safe as it is. The regex matches anything so if there's no other processing, it could be used to inject HTML/scripts into the page. You should replace .* with a more specific pattern to only allow certain characters. It depends what characters can legitimately be used in your IDs - if it's only alphanumeric, you'll be fine as it is. Otherwise, you'll need further processing to strip out any nasties.
     
    rodney88, Feb 1, 2009 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    My site is secured and everything is filtered. BUT this won't open up any worm holes? If with those tags they can do everything they can do just by typing in the URL, I'm all good...
     
    x0x, Feb 1, 2009 IP