亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.htacces 文件和 php 中的 get 方法有問題

.htacces 文件和 php 中的 get 方法有問題

PHP
肥皂起泡泡 2021-11-13 17:21:48
我有 company.php?name=Son's.&.clothes 的 URL當我使用.htaccess文件時,它應該像 company/Son's.&.clothes$url = $pro->vendor_company; ///$url = str_replace('&', '%26', $url);// Here i replaced & with %26http://localhost/worldsindia/name/Jai%20Industries%20Ltd. // Then it looks like that輸出是我在 PHP 中使用 GET 方法從 URL 獲取名稱: if(isset($_GET['name'])){        $name = $_GET['name'];        }    var_dump($name);   Jai Industries Ltd.php 這是我的.htaccess文件RewriteEngine On<ifModule mod_headers.c>Header set Connection keep-alive </ifModule>## EXPIRES CACHING ##<IfModule mod_expires.c>ExpiresActive OnExpiresByType image/jpg "access 1 year"ExpiresByType image/jpeg "access 1 year"ExpiresByType image/gif "access 1 year"ExpiresByType image/png "access 1 year"ExpiresByType text/css "access 1 month"ExpiresByType text/html "access 1 month"ExpiresByType application/pdf "access 1 month"ExpiresByType text/x-javascript "access 1 month"ExpiresByType application/x-shockwave-flash "access 1 month"ExpiresByType image/x-icon "access 1 year"ExpiresDefault "access 1 month"</IfModule>## EXPIRES CACHING ###AddOutputFilterByType DEFLATE text/plain#AddOutputFilterByType DEFLATE text/html#AddOutputFilterByType DEFLATE text/xml#AddOutputFilterByType DEFLATE text/css#AddOutputFilterByType DEFLATE application/xml#AddOutputFilterByType DEFLATE application/xhtml+xml#AddOutputFilterByType DEFLATE application/rss+xml#AddOutputFilterByType DEFLATE application/javascript#AddOutputFilterByType DEFLATE application/x-javascriptRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^([^\.]+)/?$ $0.php [NC,L]RewriteRule ^company/([A-Za-z0-9-\s&()+/.']+)  company.php?name=$1 [NE,B,L]$name 的輸出就像 Son's
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

請求的 url 不是 urlencoded。


& 字符表示獲取請求中的新參數。


url [questionmark] parametername [equals sign] value [ampersand] parametername [equals sign] value


url?foo=bar&baz=bal

會變成:


$_GET = [

    'foo' => 'bar',

    'baz' => 'bal'

];

所以同樣的邏輯適用于你的 url company.php?name=Son's.&.clothes,然后在你的情況下你得到


$_GET = [

    'name' => 'Son\'s.',

    '.clothes' => null,

];

或者如果您的 webserver/php 設置不同,空的 get 變量可能會被刪除。


urlencode()在使用 php 或 javascript 渲染它們之前使用encodeURIComponent()或通過手動編碼/替換&符號%26


company.php?name=Son's.%26.clothes

基于編輯的問題


$url = str_replace('&', '%26', $url);// Here i replaced & with %26

$url = urlencode($url);

刪除第一行。urlencode 會處理這個問題?,F在您正在對 %26 的轉義符號進行 urlencoding。這兩行應該變成:


 $url = urlencode($url);

哪個會給


 http://localhost/worldsindia/name/Durgesh+Jaiswal+%26+Co

現在你的重寫規則是荒謬的


 RewriteRule ^company/([A-Za-z0-9-\s()+/']+)  company.php?name=$1 [NC,L]

這將檢查 url 是否以公司一詞開頭。

您的 url 以 worldsinda 開頭

即使它通過將 company 換成 worldsindia 來匹配,那么您的 url 也會變成:


 company.php?name=name/Durgesh Jaiswal & Co

這是一個奇怪的獲取請求。但是因為它是重寫規則而不是重定向規則,它再次從頂部輸入到 .htaccessfile 中。


所以你現在擁有的是網址:


 company.php?name=name/Durgesh Jaiswal & Co

這會將 & 解釋為參數分隔符,它將刪除第二個參數,因為它是空的。所以你最終得到


array(1) {

  ["name"]=>

  string(21) "name/Durgesh Jaiswal "

}

現在要修復它,您需要修復您的重寫規則


RewriteRule ^company/([A-Za-z0-9-\s()+']+)/([A-Za-z0-9-\s()+']+)  company.php?company=$1&name=$2 [NC,L]

在這里,我們設置了基本 url,/company因此 company 之后的任何內容都將被提供給 company.php 文件。任何不是公司的東西都會被喂給別的東西。


現在他得到請求的參數是


array(2) {

  ["company"]=>

  string(11) "worldsindia"

  ["name"]=>

  string(16) "Durgesh Jaiswal "

}

我們仍然沒有 & 號之后的部分,這是因為 Apache 做了一些事情,但主要是因為您的重定向規則中沒有 & 號。


RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)  test.php?company=$1&name=$2 [NE,L]

現在我們結束了:


array(3) {

  ["company"]=>

  string(13) "worldwideinda"

  ["name"]=>

  string(16) "Durgesh Jaiswal "

  ["Co"]=>

  string(0) ""

}

我們可以通過在重寫規則中添加 B 修改器來解決這個問題,閱讀更多信息https://www.gerd-riesselmann.net/development/using-and-path-apache-modrewrite-and-php/


 RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)  test.php?company=$1&name=$2 [NE,B,L] 

這將導致


array(2) {

  ["company"]=>

  string(13) "worldwideinda"

  ["name"]=>

  string(20) "Durgesh+Jaiswal+&+Co"

}

由于優點,這不是很好看。最好讓 url 編碼用正確的轉義碼 %20 替換它們。你可以用rawurlencode做到這一點

http://img1.sycdn.imooc.com//618f83d00001c27e06900218.jpg

查看完整回答
反對 回復 2021-11-13
  • 1 回答
  • 0 關注
  • 216 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號