Explode + url faking problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frizzle

    Explode + url faking problem

    Hi there,

    i have a site with fake folders & files.
    htaccess rewrites everything to index.php?vars
    now in index.php i decide what file to include with a switch/case
    statement.

    to define where i am, i explode the query string, and check $array[0]
    where i am for the main section.
    Only now if there are two slashes behind each other
    (folder//subfolder), the value inbetween ( '' )
    gets ignored, though in 'real' it would give an error. How can i have
    explode telling
    me that value is '' ?

    I hope you understand (sorry for my english)

    Frizzle.

  • Toby Inkster

    #2
    Re: Explode + url faking problem

    frizzle wrote:
    [color=blue]
    > Only now if there are two slashes behind each other
    > (folder//subfolder)[/color]

    Best thing is to make sure explode() never even sees that.

    <?php
    $url = 'folder//subfolder/////somefile';

    while (strstr($url, '//'))
    $url = str_replace('//', '/', $url);

    $parts = explode('/', $url);

    print $parts[0]; /* prints 'folder' */
    print $parts[1]; /* prints 'subfolder' */
    print $parts[2]; /* prints 'somefile' */
    ?>

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact

    Comment

    • frizzle

      #3
      Re: Explode + url faking problem


      Toby Inkster wrote:[color=blue]
      > frizzle wrote:
      >[color=green]
      > > Only now if there are two slashes behind each other
      > > (folder//subfolder)[/color]
      >
      > Best thing is to make sure explode() never even sees that.
      >
      > <?php
      > $url = 'folder//subfolder/////somefile';
      >
      > while (strstr($url, '//'))
      > $url = str_replace('//', '/', $url);
      >
      > $parts = explode('/', $url);
      >
      > print $parts[0]; /* prints 'folder' */
      > print $parts[1]; /* prints 'subfolder' */
      > print $parts[2]; /* prints 'somefile' */
      > ?>
      >
      > --
      > Toby A Inkster BSc (Hons) ARCS
      > Contact Me ~ http://tobyinkster.co.uk/contact[/color]

      Well, i figured right now, explode ignores it, and goes past it.
      The way you present it, is axactly what it does right now w/o any
      str_repl().
      What i'd like is to have $array[1] to be empry or null, etc.

      I hope you get it.

      Frizzle.

      Comment

      • Bent Stigsen

        #4
        Re: Explode + url faking problem

        frizzle wrote:[color=blue]
        > Toby Inkster wrote:[color=green]
        >> frizzle wrote:
        >>[color=darkred]
        >>> Only now if there are two slashes behind each other
        >>> (folder//subfolder)[/color]
        >> Best thing is to make sure explode() never even sees that.
        >>
        >> <?php
        >> $url = 'folder//subfolder/////somefile';
        >>
        >> while (strstr($url, '//'))
        >> $url = str_replace('//', '/', $url);
        >>
        >> $parts = explode('/', $url);[/color][/color]
        [snip][color=blue]
        > Well, i figured right now, explode ignores it, and goes past it.
        > The way you present it, is axactly what it does right now w/o any
        > str_repl().[/color]

        Can you post an example of code that will do that. You must be doing
        something else that removes the empty values.
        [color=blue]
        > What i'd like is to have $array[1] to be empry or null, etc.[/color]

        That is what explode should give you, if you got nothing in between
        two separators. If you take Toby's code without the while/str_replace,
        then you should get:
        Array
        (
        [0] => folder
        [1] =>
        [2] => subfolder
        [3] =>
        [4] =>
        [5] =>
        [6] =>
        [7] => somefile
        )


        /Bent

        Comment

        • frizzle

          #5
          Re: Explode + url faking problem


          Bent Stigsen wrote:[color=blue]
          > frizzle wrote:[color=green]
          > > Toby Inkster wrote:[color=darkred]
          > >> frizzle wrote:
          > >>
          > >>> Only now if there are two slashes behind each other
          > >>> (folder//subfolder)
          > >> Best thing is to make sure explode() never even sees that.
          > >>
          > >> <?php
          > >> $url = 'folder//subfolder/////somefile';
          > >>
          > >> while (strstr($url, '//'))
          > >> $url = str_replace('//', '/', $url);
          > >>
          > >> $parts = explode('/', $url);[/color][/color]
          > [snip][color=green]
          > > Well, i figured right now, explode ignores it, and goes past it.
          > > The way you present it, is axactly what it does right now w/o any
          > > str_repl().[/color]
          >
          > Can you post an example of code that will do that. You must be doing
          > something else that removes the empty values.
          >[color=green]
          > > What i'd like is to have $array[1] to be empry or null, etc.[/color]
          >
          > That is what explode should give you, if you got nothing in between
          > two separators. If you take Toby's code without the while/str_replace,
          > then you should get:
          > Array
          > (
          > [0] => folder
          > [1] =>
          > [2] => subfolder
          > [3] =>
          > [4] =>
          > [5] =>
          > [6] =>
          > [7] => somefile
          > )
          >
          >
          > /Bent[/color]

          function DefineLoc(){
          $loc = explode('/', $_SERVER['QUERY_STRING'], ) );
          return $loc;
          };

          The function worked that way for me. Also, when i had trailing slashes,
          the current location would differ from w/o any trailing slashes.

          Now i have this:
          function DefineLoc(){
          $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
          return $loc;
          };

          But that's also not as it should be but fixes the trailing problem.

          Frizzle.

          Comment

          • Bent Stigsen

            #6
            Re: Explode + url faking problem

            frizzle wrote:
            [snip][color=blue]
            > Now i have this:
            > function DefineLoc(){
            > $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
            > return $loc;
            > };
            >
            > But that's also not as it should be but fixes the trailing problem.[/color]

            I don't think I quite understand what you want.

            Can you give an example value of $_SERVER['QUERY_STRING'], and how the
            array returned from DefineLoc should look like.


            /Bent

            Comment

            • frizzle

              #7
              Re: Explode + url faking problem


              Bent Stigsen wrote:[color=blue]
              > frizzle wrote:
              > [snip][color=green]
              > > Now i have this:
              > > function DefineLoc(){
              > > $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
              > > return $loc;
              > > };
              > >
              > > But that's also not as it should be but fixes the trailing problem.[/color]
              >
              > I don't think I quite understand what you want.
              >
              > Can you give an example value of $_SERVER['QUERY_STRING'], and how the
              > array returned from DefineLoc should look like.
              >
              >
              > /Bent[/color]

              $_SERVER['QUERY_STRING'] =
              '/artists/nirvana/mtv_unplugged/about_a_girl.ht ml'
              $array[0] => artists
              $array[1] => nirvana
              $array[2] => mtv_unplugged
              $array[3] => about_a_girl.ht ml

              $_SERVER['QUERY_STRING'] = '/artists/nirvana/mtv_unplugged/'
              $array[0] => artists
              $array[1] => nirvana
              $array[2] => mtv_unplugged
              $array[3] => NULL

              $_SERVER['QUERY_STRING'] = '/artists/nirvana/mtv_unplugged'
              $array[0] => artists
              $array[1] => nirvana
              $array[2] => mtv_unplugged
              $array[3] => NULL

              $_SERVER['QUERY_STRING'] = '/artists//mtv_unplugged/'
              $array[0] => artists
              $array[1] => NULL
              $array[2] => mtv_unplugged
              $array[3] => NULL

              I hope you understand what i mean/want.

              Frizzle.

              Comment

              • Bent Stigsen

                #8
                Re: Explode + url faking problem

                frizzle wrote:
                [snip][color=blue]
                > $_SERVER['QUERY_STRING'] = '/artists/nirvana/mtv_unplugged'
                > $array[0] => artists
                > $array[1] => nirvana
                > $array[2] => mtv_unplugged
                > $array[3] => NULL[/color]
                [snip]

                This one above is your real problem. You would need some mechanism to
                determine what kind last element is, file or part of path. A lazy way
                could be to set the criteria or just make the assumption that files
                always has an attached extension (i.e. '.' in the name).

                For instance:
                function DefineLoc(){
                $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
                if (strrpos(end($l oc), '.')===false) $loc[] = null;
                reset($loc);
                return $loc;
                };

                If you have pathnames containing '.', then you would have to write
                some code that can make the distinction.

                /Bent

                Comment

                • frizzle

                  #9
                  Re: Explode + url faking problem

                  Bent Stigsen wrote:[color=blue]
                  > frizzle wrote:
                  > [snip][color=green]
                  > > $_SERVER['QUERY_STRING'] = '/artists/nirvana/mtv_unplugged'
                  > > $array[0] => artists
                  > > $array[1] => nirvana
                  > > $array[2] => mtv_unplugged
                  > > $array[3] => NULL[/color]
                  > [snip]
                  >
                  > This one above is your real problem. You would need some mechanism to
                  > determine what kind last element is, file or part of path. A lazy way
                  > could be to set the criteria or just make the assumption that files
                  > always has an attached extension (i.e. '.' in the name).
                  >
                  > For instance:
                  > function DefineLoc(){
                  > $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
                  > if (strrpos(end($l oc), '.')===false) $loc[] = null;
                  > reset($loc);
                  > return $loc;
                  > };
                  >
                  > If you have pathnames containing '.', then you would have to write
                  > some code that can make the distinction.
                  >
                  > /Bent[/color]

                  Actually what i have is quite similar to what you suggest. But it still
                  doesn't solve my double slash problem, where the value should be NULL.

                  The thingy i have to check if a filename is specified is the following:

                  if( preg_match( '/(\.html)$/', $loc[3] ) ){
                  $song_url = preg_replace( '/(\.html)$/', '', $loc[3] );
                  $song_extension = TRUE;
                  }else{
                  $song_extension = FALSE;
                  };

                  It checks all values in the array are UrlSafe(), wich means that they
                  are A-z, 0-9, -, _ That's also why i delete the last '.html', because
                  else validation would return false.

                  Frizzle.

                  Comment

                  • Toby Inkster

                    #10
                    Re: Explode + url faking problem

                    frizzle wrote:
                    [color=blue]
                    > $_SERVER['QUERY_STRING'] = '/artists//mtv_unplugged/'[/color]

                    $array = explode('/', $_SERVER['QUERY_STRING']);
                    array_shift($ar ray);
                    for ($i=0;$i<4;$i++ )
                    if (!strlen($array[$i]))
                    $array[$i] = 'NULL';
                    [color=blue]
                    > $array[0] => artists
                    > $array[1] => NULL
                    > $array[2] => mtv_unplugged
                    > $array[3] => NULL[/color]

                    --
                    Toby A Inkster BSc (Hons) ARCS
                    Contact Me ~ http://tobyinkster.co.uk/contact

                    Comment

                    • Bent Stigsen

                      #11
                      Re: Explode + url faking problem

                      frizzle wrote:[color=blue]
                      > Bent Stigsen wrote:[color=green]
                      >> frizzle wrote:
                      >> [snip][color=darkred]
                      >>> $_SERVER['QUERY_STRING'] = '/artists/nirvana/mtv_unplugged'
                      >>> $array[0] => artists
                      >>> $array[1] => nirvana
                      >>> $array[2] => mtv_unplugged
                      >>> $array[3] => NULL[/color]
                      >> [snip]
                      >>
                      >> This one above is your real problem. You would need some mechanism to
                      >> determine what kind last element is, file or part of path. A lazy way
                      >> could be to set the criteria or just make the assumption that files
                      >> always has an attached extension (i.e. '.' in the name).
                      >>
                      >> For instance:
                      >> function DefineLoc(){
                      >> $loc = explode('/', trim( $_SERVER['QUERY_STRING'], '/' ) );
                      >> if (strrpos(end($l oc), '.')===false) $loc[] = null;
                      >> reset($loc);
                      >> return $loc;
                      >> };
                      >>
                      >> If you have pathnames containing '.', then you would have to write
                      >> some code that can make the distinction.
                      >>
                      >> /Bent[/color]
                      >
                      > Actually what i have is quite similar to what you suggest. But it still
                      > doesn't solve my double slash problem, where the value should be NULL.[/color]

                      If I run this:
                      $url = '/artists//mtv_unplugged/';
                      $loc = explode('/', trim( $url, '/' ) );
                      if (strrpos(end($l oc), '.')===false) $loc[] = null;
                      print_r($loc);

                      I get:
                      Array
                      (
                      [0] => artists
                      [1] =>
                      [2] => mtv_unplugged
                      [3] =>
                      )

                      Isn't that what you want?


                      /Bent

                      [snip]

                      Comment

                      • the DtTvB

                        #12
                        Re: Explode + url faking problem

                        Let me guess it, you did mod_rewite
                        RewriteRule ^/(.+?)$ index.php?$1

                        Is that what you want?
                        <?php
                        $requested_page = $_SERVER['QUERY_STRING'];
                        $requested_page = preg_replace('~/+~', '/', $requested_page );
                        $requested_tree = explode('/', $requested_page );
                        ?>

                        I tested with
                        http://localhost/test5.php?folder///...der///yo///lol and it said
                        that
                        $requested_tree is Array (
                        [0] => folder
                        [1] => subfolder
                        [2] => yo
                        [3] => lol
                        )

                        Comment

                        • frizzle

                          #13
                          Re: Explode + url faking problem


                          the DtTvB wrote:[color=blue]
                          > Let me guess it, you did mod_rewite
                          > RewriteRule ^/(.+?)$ index.php?$1
                          >
                          > Is that what you want?
                          > <?php
                          > $requested_page = $_SERVER['QUERY_STRING'];
                          > $requested_page = preg_replace('~/+~', '/', $requested_page );
                          > $requested_tree = explode('/', $requested_page );
                          > ?>
                          >
                          > I tested with
                          > http://localhost/test5.php?folder///...der///yo///lol and it said
                          > that
                          > $requested_tree is Array (
                          > [0] => folder
                          > [1] => subfolder
                          > [2] => yo
                          > [3] => lol
                          > )[/color]

                          I have mod-rewrite:

                          RewriteBase /testfolder/
                          RewriteRule ^(index\.php)$ - [L]
                          RewriteRule ^(img|swf|js|cs s)/.*$ - [L]
                          RewriteRule ^(.*)$ index.php?$1 [L]

                          But this also still gives me problems as stated on


                          Frizzle.

                          Comment

                          Working...