簡體   English   中英

帶引號錯誤的網址

[英]URL with quotes causing errors

我是一個新手試圖使用數據庫為圖庫工作PHP腳本。 我最近更改了腳本和數據庫,以便庫將基於'photo_caption運行。 畫廊有效,但如果數據庫包含引號(例如標題為自然的花園和顏色),劇本只會在引號之前拉出部分(所以這里只是自然而不是整個標題自然的花園和顏色),從而打破鏈接。 現在鏈接結構就像這樣/viewgallery.php?cname=Colorado%20Journies&pcaption=Nature'sGardenAndColors但是因為它顯示的問題顯示為/viewgallery.php?cname=Colorado%20Journies&pcaption=Nature

這個問題有解決方法嗎? 如何獲取$ _GET語句來提取完整​​的photo_caption,包括引號? 謝謝你的幫助...

我現在使用此代碼獲取鏈接的photocaption

$pcaption = isset($_GET['pcaption']) ? ($_GET['pcaption']) : 0;

if($ pcaption){

    $result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 

    list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 

    $nr = mysql_num_rows( $result ); 
     mysql_free_result( $result );     

    $p_caption = $photo_caption;
    $p_description = $photo_description;
    $p_keywords = $photo_keywords;

    //fill pid_array with sorted pids in current category 

    $result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' ORDER BY photo_caption" ); 

    $ct = mysql_num_rows( $result );     

    while ($row = mysql_fetch_array($result)) { 

          $pid_array[] = $row[0]; 
    } 
    mysql_free_result( $result ); 

    if( empty($nr ) ) 
    { 
        print "%%%%NR is $nr";
        $result_final = "\t<tr><td>***No Photo found</td></tr>\n"; 
    } 
    else 
    { 
        $result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_name='".addslashes($cname)."'" ); 
        list($category_name) = mysql_fetch_array( $result ); 
        mysql_free_result( $result );     
        $result_final = "
        <div class=limagePage>
        <div class=llink><a href='viewgallery.php'>ALBUMS</a><span class=arrow>&gt;&gt</span><a href='viewgallery.php?cname=$cname>$category_name'</a></div>
         ";
        // display previous and next links if more than one photo 

        if ($ct > 1) 
        { 

            $key = array_search($pcaption , $pid_array); 
            $prev = $key - 1; 

            if ($prev < 0) $prev = $ct - 1; 
            $next = $key + 1; 

            if ($next == $ct) $next = 0; 

            //$cname = str_replace(" ","_",$cname);
            //$pcaption=str_replace(" ","_",$pcaption);

            $result_final .= "<div class='prevnext'>"; 
            $result_final .= "<span class='prev'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='photos/assets/left.png'  border='0' ></a></span>"; 
            $result_final .= "<span class='next'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$prev]."'><img src='photos/assets/right.png'  border='0' ></a></span>"; 
            $result_final .= "</div>";

        }            
    }
    //$cname = str_replace(" ","_",$cname);
    //$pcaption=str_replace(" ","_",$pcaption);
   $result_final .= "<div class=limage><table><tr><td><table class=image><tr>\n\t<td><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /></a>
  <div class=caption>".$photo_caption."</div> 
  <div class='excerpt'>".$photo_description."</div> 
  </td>                    
  </tr></table></td></tr></table><div class=underline></div></div>
  <!-- .limagePage --></div>    ";

嘗試編碼值的url

urlencode($pid_array[$next])

http://php.net/manual/en/function.urlencode.php

不幸的是urlencode不會影響'嘗試str_replace("'", "%27", $pid_array[$next])也許它會str_replace("'", "%27", $pid_array[$next])幫助。 例如,它適用於地址欄中的Google。

UPD rawurldecode (upd。正確: rawurlencode當然)可以做到這一點,無論如何鏈接看起來有點難看,充滿了%53,%27等等。但是別忘了urldecode($_GET['pcaption'])使用之前

您需要在鏈接之前對URL進行編碼,如下所示:

<a href='viewgallery.php?cname=$cname&pcaption=".urlencode($pid_array[$next])."'>

否則認為鏈接會提前截止,因為鏈接將如下所示:

<a href='viewgallery.php?cname=$cname&pcaption=Nature's photo'>
                                                     ^ ends the link

使用http_build_query()。 使用此函數,您可以將數組轉換為http查詢,例如:
$ data = array('name'=>'my name','city'=>'my city');

$ url =“ http://your_url.com/ ?” http_build_query($數據);



輸出將類似於:http: //your_url.com/?name = my%20name &city = my%20city

rawurlencode($pid_array[$next])

也應該做的伎倆。

請注意,使用變量解析引號會使用更多處理。 因為你已經中途做了..

$result_final .= "<span class='prev'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='photos/assets/left.png'  border='0' ></a></span>"; 

可以換成:

$result_final .= '<span class="prev"><a href="viewgallery.php?cname='.urlencode($cname).'&amp;pcaption='.urlencode($pid_array[$next]).'"><img src="photos/assets/left.png" border="0" ></a></span>'; 

也&不是HTML有效,&amp; 現在html有效,引用不重要,cpuwise效率更高。

每個試圖幫助我的人......我要感謝大家的幫助。 我發現除了數據庫中的引號“空格”,其中photo_caption也會引起問題。 所以我試着修剪代碼中的空格(它工作)並決定現在避免引用。 只是想留下一個跟進記錄......謝謝大家。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM