2014-02-19 49 views
-3

我想寫一個簡單的PHP窗體,但出來是不同於我想要它是。有沒有人看到我的錯誤是多謝,並感謝。 作業: 編寫一個PHP腳本,用於檢查從表單發送的消息,然後打印發件人和消息的名稱。如果發件人姓名或消息爲空,請打印「您沒有提供所有必需的信息!」刪除用戶名或消息之前或之後的任何空格。也刪除任何HTML標籤,以確保用戶不能改變留言簿。所使用的形式如下:簡單的PHP窗體1

<form action="guestbook.php" method="get"> 
    Sender: <input TYPE="text" name="name"><br> 
    Message: <input type="text" name="message"><br> 
    <input type="submit" value="Send">     
</form> 


Example output 


John: Hello! 

**my script:** 

<?php 

$name = $_GET["name"]; 
if(isset($_GET['submit'])) { 
    echo "$name: Hello!"; 
} else { 
    echo "You didn’t give all required information!"; 
} 

?> 

YOUR PROGRAM DOES NOT OPERATE CORRECTLY 
Your program generated the following output: 

You didn’t give all required information! 


The following output should have been generated: 

John: Hello! 

    The white area indicates correct output from your program. 
    represents a carriage return 
    In the comparison of outputs, the output of your program must be exactly the same as the example output. 

回答

0

您還沒有命名輸入「提交」,唯一一家擁有提交,這是你在找什麼。嘗試在if語句,而不是使用isset($_GET['name']),因爲這是你實際上使用的是什麼:

<?php 
if(isset($_GET['name'])) 
{ 
    echo $_GET['name'].": Hello!"; 
} 
else 
{ 
    echo "You didn’t give all required information!"; 
} 
?> 

你應該知道,雖然像這樣的直接輸出用戶輸入的數據是非常不安全的,創造一個非常大的XSS漏洞。

+0

嗨,感謝您的幫助,當我嘗試您的代碼輸出是不同的我們的程序不能正常運行 您的程序生成以下輸出: John:您好! \t \t 應生成以下輸出: 您沒有提供所有必需的信息! 白色區域表示程序輸出正確。 表示回車 在輸出的比較中,程序的輸出必須與示例輸出完全相同。 – user3314727

+0

您有原始問題未指定的附加要求。這聽起來像你的要求是在顯示消息之前檢查_both_名稱_and_消息是否已設置。嘗試改變這行以說'if(isset($ _ GET ['name'])&& isset($ _ GET ['message'])){'。 – Adam

0
if(isset($_GET['submit'])) 

應該

if(isset($_GET['name'])) 
0

你的PHP看起來應該像下面這樣:

<?php 
$name = $_GET['name']; 
if($name != ''){ 
echo "$name: Hello!"; 
} 
else 
{ 
echo "You didn’t give all required information!"; 
} 
?> 

注:我沒有測試過這一點。

+0

應該指出,這將不兼容[PHP的嚴格水平](http://lampgurus.com/site/b/2012/02/18/strict-mode-in-php.html)兼容 - 如果頁面加載時沒有'name'發送GET參數會觸發警告。爲'$ _GET ['name']'添加'isset'檢查可以防止這種情況發生。 – Adam

0

在你的聲明:

如果發件人名稱或郵件是空白,打印「你沒有給所有 需要的信息!」之前或用戶名 或郵件後刪除任何空格。也刪除任何HTML標籤,以確保用戶不能更改留言簿。

,你應該有這樣的事情裏面你的if語句:

if(isset($_GET['name'])){ 

    $clean_name = trim(strip_tags($_GET['name'])); 

    echo $clean_name; 

} 

裝飾()刪除多餘的空格前後串的後用strip_tags()刪除HTML標籤

0
<form action="guestbook.php" method="get"> 
    Sender: <input type="text" name="name"><br> 
    Message: <input type="text" name="message"><br> 
    <input type="submit" value="Send" name="send">     
</form> 

<?php 
if(isset($_GET['send'])) { 
    $name = trim(strip_tags($_GET['name'])); 
    if($name != ''){ 
     echo "$name: Hello!"; 
    } else { 
     echo "You didn’t give all required information!"; 
    } 
} 
?>