4 回答

TA貢獻1828條經驗 獲得超6個贊
die()不應在您的網絡腳本中使用。有時在調試時作為臨時措施很有用,但在實時應用程序中使用它可能會導致災難。一個更好的選擇是使用異常,它可以被您的應用程序或 PHP 本身捕獲。
if(/*this is not allowed*/) {
throw new \Exception('You are not allowed to do this!');
}
通常,您應該將應用程序設計為具有最少的此類意外情況。如果出現問題不是您的應用程序的錯,那么您的代碼不應該拋出任何錯誤或異常。編寫適當的驗證并以這樣的方式構建項目,以便當用戶提供無效輸入時,他們會得到一個很好的錯誤消息。

TA貢獻1806條經驗 獲得超8個贊
牢記您在問題中施加的條件,
我確實希望我的所有 PHP 腳本只保留在一個文件中,并且表單按鈕可以運行
我會建議以下解決您的問題的方法
<H1>Form validation with my SQL integration </H1>
<form action="index.php" method="post">
emailid = <input type="text" name="tv_name" size="30" maxlength="32"><br>
password = <input type="password" name="tv_pass" size="30" maxlength="11"><br>
Press to create a new account: <input type="submit" colspan="12" name="bt_register" value="Register!"><br>
Press to get all: <input type="submit" colspan="12" name="bt_getall" value="GetAll entries"><br>
</form>
<?php
$req_type= $_SERVER['REQUEST_METHOD'];
$data_arr =[];
if(isset($_REQUEST["bt_register"]) ){ // Executes for bt_register only
$data_arr = $_REQUEST;
if($req_type == "GET"){
// Write operations that you want to perform for Register (when the method is GET)"
}elseif($req_type == "POST"){
// Write operations that you want to perform for Register (when the method is POST)"
}else{
throw new \Exception('Invalid method used');
}
// Write operations that you want to perform for all register requests (GET or POST)
echo strip_tags(""); # Converting key-string pair to key-array pair
# Finding k-v pairs for each data
foreach($data_arr as $i=> $value){
echo "---- $i : $value <br>";
}
#... insertion/deletion/updation/creation
}elseif(isset($_REQUEST["bt_getall"])){ // Executes for bt_getall only
if($req_type=='GET'){
echo "Get all req recieved via get";
}
elseif ($req_type=='POST'){
echo "Get all req recieved via post";
}
else {
throw new \Exception('Invalid method used');
}
// Write operations that you want to perform for all getall requests (GET or POST)
}elseif(isset($_REQUEST["bt_login"])){ // executes for bt_login only
// Write code that should be executed on login button press
}
else { // executes for default page load or any other button press
echo "<h2>First time load</h2>";
// Write code (if any) that you want to run on first landing only.
}
?>
代碼更改的說明
die()
不應按照上一個答案中的說明使用參考下面引用的您的評論,
<?php ?>
在這種情況下再次編寫標簽是沒有用的。寫新<?php ?>
標簽不能調用已經死掉的請求// 必須創建一個新的,因為舊的會在登錄按下時死掉
isset($_REQUEST["bt_register"]
無論他們的方法如何(GET 或 POST),都可以為您提供數據使用 if-else 處理請求類型(注冊、獲取所有、登錄、首次加載等),如上述代碼的注釋中所述
我建議您對每種類型的請求使用不同的 PHP 文件,而不是將所有代碼都寫在一個 index.php 文件中。

TA貢獻1828條經驗 獲得超3個贊
是的,從字面上看,除了 die() 之外的任何東西。.... 什么都沒有。
例如:
$req_type= $_SERVER['REQUEST_METHOD'];
$data_arr =[];
if($req_type=='GET' and isset($_GET["bt_register"]) ){
$data_arr = $_GET;
}
elseif ($req_type=='POST' and isset($_POST["bt_register"]) ){
$data_arr = $_POST;
}
else{
//do whatever you want here, or nothing at all.
}
但是,更多有用的輸入......
為什么要檢查請求方法?您已將表單設置為 POST。其他形式會調用這個相同的腳本嗎?我想你想要更像這樣的東西:
<?php
$req_type= $_SERVER['REQUEST_METHOD']; # checking req type
if($req_type=='POST' && isset($_POST["tv_name"]) && isset($_POST["tv_pass"]) ){
echo "Everything is as it should be";
}
else {
//log the error
error_log("Error with POST tv_name or tv-pass", 3, '/var/www/html/myfancyloggingfolder/didsomeonetrytoGET.log');
//display an error message
echo 'This message could be more robust, but either POST was not used or tv_name / tv_pass was not included.';
// if you do not want the script to stop then do not use die or exit here.
}
?>

TA貢獻2019條經驗 獲得超9個贊
我目前正在使用以下使用函數來區分不同按鈕調用的方法。這樣,只有需要在單擊按鈕時運行的代碼才會運行,因為isset (_REQTYPE[button_id])僅在button_id每次單擊按鈕時為特定設置設置,然后將其清除。(請注意,這些只是我的猜測,因為我的代碼對我有用。請隨時添加更好的、更有事實支持的推理):
<?php
$req_type= $_SERVER['REQUEST_METHOD']; # Checking req type
$data_arr =[];
if($req_type=='GET' and isset($_GET["bt_reg"])) { # Pressing bt_reg would make a $_GET=[k1:v1, k2:v2,...] one of the pair will be "bt_reg":"Register!" . so checking if it's set
$data_arr = $_GET;
runRegisterCommands($data_arr);
}
elseif ($req_type=='POST' and isset($_POST["bt_reg"])) { # Pressing bt_reg would make a $_POST=[k1:v1, k2:v2,...] one of the pair will be "bt_reg":"Register!" . so checking if it's set
$data_arr = $_POST;
runRegisterCommands($data_arr);
}
// -----------------------------------------------------
if($req_type=='GET' and isset($_GET["bt_getall"]) ){ # Same as above, but for bt_getall, since bt_getall won't be set up on bt_register click
echo "Get all req received via get";
runGetAllCommands();
}
elseif ($req_type=='POST' and isset($_POST["bt_getall"]) ){
echo "Get all req received via post";
runGetAllCommands();
}
?>
- 4 回答
- 0 關注
- 189 瀏覽
添加回答
舉報