2010-02-08 106 views
1

我希望有人能夠幫助我解決這個問題。我是一個ASP程序員,不知道這是如何工作的PHPPHP申請表 - 我不知道如何使用它

echo '</textarea> 
     <input type="hidden" name="g_word" id="g_word" value="$_POST[g_word]" /> 
     <input type="hidden" name="article_no" id="article_no" value="$_POST[article_no]" /> 
     </form>'; 

如何在上面的例子中使用$_POST[article_no]?在asp中我會使用它像這樣"+Request.Form("article_no")+"。我將如何在PHP中做到這一點?

感謝

回答

2
echo '</textarea><input type="hidden" 
name="g_word" id="g_word" 
value="'.$_POST[g_word].'" /> <input 
type="hidden" name="article_no" 
id="article_no" 
value="'.$_POST[article_no].'" /></form>'; 

關閉單引號,並用一個點來concatonate

$value = "cool"; 
echo 'My String is ' . $value . '!!!!!'; 

在這種情況下,該點是一樣的加連接符。

+0

謝謝我欣賞幫助:-) – 2010-02-09 23:43:51

1

變量不在單引號內解釋。但是,它們在雙引號字符串或heredoc中。就個人而言,我會切換出PHP模式完全,像這樣:

<?php 
//... 
?> 
</textarea><input type="hidden" 
name="g_word" id="g_word" 
value="<?php echo htmlentities($_POST['g_word']); ?>" /> <input 
type="hidden" name="article_no" 
id="article_no" 
value="<?php echo htmlentities($_POST['article_no']); ?>" /></form> 
<?php 
//... 

,如果你做了一些格式和使用短標記,這更可讀 - 雖然,它需要一個非默認的配置選項,還有其他的缺點,主要是如果你有由PHP中間件解析的XML文檔,或者你的應用程序將被安裝在你不能控制的服務器上。

想看看像這樣的:

<form> 
    <textarea> 
    <? 
    //... 
    ?> 
    </textarea> 
    <input type="hidden" name="g_word" id="g_word" value="<?= htmlentities($_POST['g_word']); ?>" /> 
    <input type="hidden" name="article_no" id="article_no value="<?= htmlentities($_POST['article_no']); ?>"/> 
</form> 
<? 
//... 
5

,如果你使用上面張貼的解決方案,請添加對XSS注入一些基本的保護 - 例如ヶ輛($ _ POST [ 'article_no'])

+1

試圖在個案基礎上處理XSS在我看來,乞求漏洞。我贊成使用帶有default_modifiers的Smarty(使用這個補丁:http://www.smarty.net/forums/viewtopic.php?t = 4992)或使用Django。 – 2010-02-08 06:30:43

+0

@ bluej100:+1。這絕對是我的經驗 - 你不能指望每個在你的項目上工作的人都按照個案的原則插入他們的XSS漏洞,所以儘可能自動化它是一個巨大的勝利。 – 2010-02-08 06:37:50

2
echo '</textarea><input type="hidden" 
name="g_word" id="g_word" 
value="'.$_POST['g_word'].'" /> <input 
type="hidden" name="article_no" 
id="article_no" 
value="'.$_POST['article_no'].'" /></form>'; 

你必須把article_no放在'-s之間。

1

認爲我明白你的問題;如果沒有,請隨時告訴我。

在PHP(以及許多其他語言)中,字符串周圍的引號數決定了字符串的解析方式。如果使用單引號,則不會解析字符串中的任何內容(除了另一個單引號外 - 如果您希望它是字符串的一部分而不是密引碼,則需要使用反斜線進行轉義)。如果使用雙引號,則會分析更多內容,但您必須做更多的轉義。

處理在字符串中插入變量有多種方法。

使用雙引號:

echo "</textarea><input type=\"hidden\" 
name=\"g_word\" id=\"g_word\" 
value=\"$_POST['g_word']\" /> <input 
type=\"hidden\" name=\"article_no\" 
id=\"article_no\" 
value=\"$_POST['article_no']\" /></form>'; 

使用單引號:

echo '</textarea><input type="hidden" 
name="g_word" id="g_word" 
value="' . $_POST['g_word'] . '" /> <input 
type="hidden" name="article_no" 
id="article_no" 
value="' . $_POST['article_no'] . " /></form>'; 

或者說,在我看來,最優雅的方式,使用(s)printf返回一個格式化字符串:

printf('</textarea><input type="hidden" 
name="g_word" id="g_word" 
value="%s" /> <input 
type="hidden" name="article_no" 
id="article_no" 
value="%d" /></form>', $_POST['g_word'], $_POST['article_no']);