2015-05-18 48 views
0

我有後提交表單應該呈現表單字段到PHP文件的回聲語句中(其中還包含HTML元素)的stripslashes

我的問題一個HTML表單的是,雖然結果都是以預期,對特定的情況下,當表單條目有「或」在那裏,他們在results.php頁轉義

我的文件:

form.html

<form action="./results.php" method="post" id="sgemail"> 
    <table align="center" border="1" width="60%" style="border-color: #D2DFF5;"> 
    <tr> 
     <td width="50%" style="padding-left: 8px; text-align: left;"> 
     <div class="form-group"> 
      <strong>Article 1 Title<br /></strong> 
      <input type="text" class="form-control" id="ifn1title" placeholder="" name="ifn1title"><br /> 
      <strong>Article 1 URL<br /></strong> 
      <input type="text" class="form-control" id="ifn1url" placeholder="" name="ifn1url"> 
     </div> 
     </td> 
     <td width="50%" style="padding-left: 8px; text-align: left;"> 
     <div class="form-group"> 
      <strong>Article 1 Description<br /></strong> 
      <textarea rows="5" class="form-control" cols="10" name="ifn1desc" form="sgemail"></textarea> 
     </div> 
     </td> 
    </tr> 
    </table> 
    <table align="center" width="60%"> 
    <tr> 
     <td align="center" width="33%" style="padding-left: 8px; text-align: left;"> 
     <input class="btn btn-primary" type="submit" value="Generate Results HTML Code"> 
     </td> 
    </tr> 
    </table> 
</form> 

results.php

<?php 
    if (isset($_POST['ifn1title'])) 
    if (isset($_POST['ifn1url'])) 
    if (isset($_POST['ifn1desc'])) 
    { 
     $form_ifn1t = $_POST['ifn1title']; 
     $form_ifn1u = $_POST['ifn1url']; 
     $form_ifn1d = $_POST['ifn1desc']; 

echo " 
<table style=\"background-color:#D6E3F0;\" bgcolor=\"#D6E3F0\" align=\"center\" width=\"100%\"> 
    <tr align=\"center\"> 
    <td align=\"center\"><br /> 

<textarea id=\"selectori\" rows=\"50\" cols=\"120\" onclick=\"this.focus();this.select()\" readonly=\"readonly\"> 

    <ul> 
     <li><a href=\"$form_ifn1u\"><strong>$form_ifn1t</strong></a><br />$form_ifn1d</li> 
    </ul> 

</textarea> 
    </td> 
    </tr> 
</table>"; 
} 
?> 

當我提交表單,

結果在相應的位置上來如

<ul> 
    <li><a href="http://www.example.com"><strong>\"Article\"</strong></a><br />\"Test Description\"</li> 
</ul> 

如何解決它,因此結果如下清理乾淨?

<ul> 
    <li><a href="http://www.example.com"><strong>Article</strong></a><br />Test Description</li> 
</ul> 

感謝

編輯:

我說我的PHP文件的頂部以下,這固定我的問題

{ 
    $_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true); 
    $_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true); 
    $_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true); 
    $_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true); 
} 

IT這行與防範代碼注入? 感謝

+1

它看起來像你需要關閉魔術引號,但如果你有這個問題,你真的需要升級你的PHP版本,因爲三年前魔術引號被刪除。 –

+0

我在PHP 5.3.29中如何關閉這個文件中的魔術引號 – valdroni

回答

-2

最好的辦法以呼應HTML和PHP是這樣的:

<?php echo "<div class='my-class'>some HTML here, but i want my ".$php_variable." also in this text</div>";?> 

它順便說一句總是「在PHP代碼時,你有回聲‘使用’,所以我總是用單',所以我不麻煩。不必排除「。

PS:直接回顯$ _POST變量並不是最安全的方法。爲了確保您對代碼注入有點保護一定要使用一些基本的東西,如以下:

$sendVar = stripslashes(htmlentities($_POST['var'])); 

這也應該可以解決含斜線

+0

它不起作用,因爲它再次返回相同的輸出。 編輯: 我加 { $ _GET = json_decode(的stripslashes(json_encode($ _ GET,JSON_HEX_APOS)),True)下,在我的PHP文件的頂部; $ _POST = json_decode(stripslashes(json_encode($ _ POST,JSON_HEX_APOS)),true); $ _COOKIE = json_decode(stripslashes(json_encode($ _ COOKIE,JSON_HEX_APOS)),true); $ _REQUEST = json_decode(stripslashes(json_encode($ _ REQUEST,JSON_HEX_APOS)),true); } 並且問題解決了。如何防止代碼注入專門 – valdroni

+0

爲您的工作歡呼。最簡單的代碼注入方式如上所述。爲了深入瞭解這個問題,我建議你在這裏看看:http://stackoverflow.com/questions/1205889/how-to-prevent-code-injection-attacks-in-php或這裏有關mysql http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Visconti

+0

這是我添加的代碼,以防止代碼注入?我已經在Siguza在$ form_ifn1t = stripslashes($ _ POST ['ifn1title'])中提出建議進行了更改。 – valdroni

0

職位的任何問題我在PHP 29年3月5日如何關閉此單個文件上的魔術引號

您不想這樣做。 你希望他們完全一勞永逸。

請看here他們是什麼,他們爲什麼存在,他們爲什麼壞,以及如何禁用他們。

長話短說,你應該真的升級你的PHP版本。

如果你不能這樣做,你可能應該改變託管人或扔掉任何阻止你這樣做的東西。

第三個最好的辦法是禁用魔術引號:

php_flag magic_quotes_gpc Off 

而最壞的選擇,如果你真的不能做任何的上面是stripslashes$_GET$_REQUEST如果每個條目,$_POST$_COOKIE和你使用它們。
因爲您無法在運行時禁用魔術引號。
1:1複製從here

<?php 
if (get_magic_quotes_gpc()) { 
    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); 
    while (list($key, $val) = each($process)) { 
     foreach ($val as $k => $v) { 
      unset($process[$key][$k]); 
      if (is_array($v)) { 
       $process[$key][stripslashes($k)] = $v; 
       $process[] = &$process[$key][stripslashes($k)]; 
      } else { 
       $process[$key][stripslashes($k)] = stripslashes($v); 
      } 
     } 
    } 
    unset($process); 
} 

在你的情況,這將是剛剛

$form_ifn1t = stripslashes($_POST['ifn1title']); 
$form_ifn1u = stripslashes($_POST['ifn1url']); 
$form_ifn1d = stripslashes($_POST['ifn1desc']); 

升級PHP版本!