passing & in url variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    passing & in url variable

    hi,
    I need to pass a variable like alias='F&B' in url. but when i try to get the value in next page using $_GET ,its breaking.

    url like

    Code:
    http://example.com?alias=F&B
    how do i pass it as entire data and get it in next page?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The & character has special meaning in the query string of an URL. You need to urlencode() it before adding it.

    If you can't do it via PHP, the URL encoded replacement for "&" is "%26".

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      i have done that.i use ralurlencode
      and the url looks like

      Code:
      http://example.com/Restaurants?aliasF%26B
      but when i try to print like

      Code:
      $alias =  rawurldecode($_GET['alias']);
      echo $alias;
      its prints only F. how to solve it?
      Last edited by Atli; Mar 17 '10, 06:41 PM. Reason: Please use "example.com" for example urls. Random domain names tend to exist.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        You forgot the = in the URL, between the key and the value.

        And you don't need the rawurldecode() call. The query-string is decoded automatically.

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          hey the = i forgot while typing . but still its not working.

          Comment

          • pradeepjain
            Contributor
            • Jul 2007
            • 563

            #6
            hey i have few things to explain over here . basically i have .htaccess entry like

            Code:
             RewriteRule ^Restaurants/(.*)$ /restaurant_rating.php?alias=$1 [L]

            so i pass like
            Code:
            http://abc.com/Restaurants/F%26B
            which gets converted to

            Code:
            http://abc.com/restaurant_rating.php?alias=F%26B
            internally .

            so when i tried directly to use
            Code:
            http://abc.com/restaurant_rating.php?alias=F%26B
            and print alias . it prints properly but when i use

            Code:
            http://abc.com/Restaurants/F%26B
            and print alias it prints only F .how to solve this problem ?

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Ok, I see.

              The problem here seems to be that by using a mod_rewrite re-route, you are essentially making two requests. - mod_rewrite internally re-directs you from the original URL to the re-written version.

              So when you start out like this:
              - http://example.com/Restaurants/F%26B
              Apache takes the original URL and translates %26 to & (as per usual), and then hands it over to mod_rewrite, which re-writes the URL and redirects you, using & instead of %26.
              - http://example.com/restaurant_rating.php?alias=F&B
              Which again, makes the URL invalid (the & being read as a variable seperator, rather than a part of the value.)

              What you need to do to overcome this, is to manipulate the original URL so that when it is re-written by mod_rewrite, the outcome of the redirect is a valid URL. - To that end, you can encode the % in the %26 value, so when it is rewritten it becomes just a normal %.
              - http://example.com/Restaurants/F%2526B
              Which is translated to
              - http://example.com/restaurant_rating.php?alias=F%26B
              From which PHP can extract "F&B".


              P.S.
              Also take into account, that if mod_rewrite does multiple re-writes (chain rewrites), then each of them would need to be accounted for.

              ... You may well be better of either using an ID rather than a name, or restrict names to non-special characters.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                It seems that a workaround for this problem exists in Apache 2.2. (See mod_rewrite - Apache HTTP server.)

                If you add the "B" flag to the rewrite, mod_rewrite will escape special chars for you.
                [code=text]RewriteRule ^(.*)$ index.php?show= $1 [L,B][/code]

                Comment

                • pradeepjain
                  Contributor
                  • Jul 2007
                  • 563

                  #9
                  i tused the option B like u said .


                  Code:
                   RewriteRule ^(.*)$ index.php?show=$1 [L,B]
                  but apache is throwing internal server error

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Which version of Apache are you using? Like I said, this only works with Apache 2.2. If you are using an earlier version, you can't use that. You will need to double-escape instead. (Or just upgrade your server.)

                    In any case, check the Apache error logs and see what the problem is, exactly.
                    The Error log is usually located under: "logs\error.log " in your Apache install directory, or under: "/var/log/apache/" on Linux. (Can vary based on the type of install you used.)

                    Comment

                    • pradeepjain
                      Contributor
                      • Jul 2007
                      • 563

                      #11
                      apache version on hosted server is 1.3.33 .. how do i work around this problem!

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        There is no workaround. The workaround was added in 2.2, so if you are using an older version you can not use it.

                        Unless you can upgrade, you would just have to resort to double-escaping, like I explained in post #7.

                        P.S.
                        You may want to consider upgrading Apache, regardless. It's been twelve years since 1.3 was released, and the newer versions have been improved quite a lot.

                        Comment

                        Working...