简体   繁体   中英

php GET variable gets interrupted with ampersand (&)

SOLUTION:

it seems htaccess requires 'B' for it to retain the ampersand so using

RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L,B]

works

SOLUTION

I am trying to read the get value from the url which is:

view.php?album=something

This is one of my rules in my .htaccess file:

#this rewrite 'view.php?album=something' to 'album/something'
RewriteRule ^album/([^/\.]+)/?$ ^view.php?album=$1 [L] 

The problem is that sometimes the album name ie 'something' may contain and & in the name and this causes $_GET to trip up.

**For example** 
album name is 'football&basketball'

The view.php page will only read up to football and throw an error.

Without the rewrite rule it works perfectly fine, but with the rewrite rule it messes up for that one case.

I am using rawurlencode at the moment

If there is no & in the name then it also works fine - its only with the & when it messes up

Does anyone have any suggestions on how to overcome this? I have tried using htmlspecialchars and htmlentities etc.

EDIT: Added Code

htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On

#rewrite view.php?album=whatever to album/whatever
RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

RewriteBase /ShyamJoshi/dist/ 

#remove .php and .html extension
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]


RewriteOptions Inherit
ReWriteCond %{HTTP:accept-encoding} (gzip.*) 
ReWriteCond %{REQUEST_FILENAME} !.+\.gz$ 
RewriteCond %{REQUEST_FILENAME}.gz -f 
RewriteRule (.+) $1.gz [QSA,L] 

</IfModule>

PHP:

This function is being used twice. First it shows all directories and then it shows all images in directories (which is the 'else' part)

function getImagesOrImageDirs($dirOnly) {
    $dirLoc = getcwd() . '/images/events/';

    $allDirs = preg_grep('/^([^.])/', scandir($dirLoc));

    echo '<div class="row">';
    if ($dirOnly == true) {
      foreach($allDirs as $dir) {
        $innerDirImages = preg_grep('/^([^.])/', scandir($dirLoc.'/'.$dir));
        $firstImageOfDir = reset($innerDirImages);

        /* view.php?album=something -> rewritten by htaccess to albums/something*/
        $href = 'album/'.$dir;

        // $href = 'album/'.$dir;
        $imageSrc = 'images/events/'.$dir.'/'.$firstImageOfDir;

        echo albumAndPhotoTemplate($href, $imageSrc, true, $dir, 'col-xs-12 col-sm-3');
      }
    } else {
      if (empty ($_GET['album']) ) {
        return ;
      }
      $albumName = $_GET['album'];
      print_r($albumName);
      $dirLoc = $dirLoc .'/'.$albumName;
      $allImages = preg_grep('/^([^.])/', scandir($dirLoc));

      foreach($allImages as $image) {
        $imgSrcAndHref = 'images/events/'.$albumName.'/'.$image;
        echo albumAndPhotoTemplate($imgSrcAndHref, $imgSrcAndHref, false, '',  'col-xs-12, col-sm-3');
      }
    }
    echo '</div>';
  }

Try like this..

RewriteEngine On

RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L]

Get your variable using

$_GET['album'];

If you want to both variable and name:

print_r($_GET);

To escape & on url use %26 like this..

view.php?album=something%26basketball

See more here escaping ampersand in url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM