亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我們在一個 PHP 文件中有多個 PHP 腳本 (<?php ... ?>)

如果我們在一個 PHP 文件中有多個 PHP 腳本 (<?php ... ?>)

PHP
忽然笑 2022-10-22 16:28:08
我是網絡開發的新手。我有以下index.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($req_type=='GET' and isset($_GET["bt_register"]) ){        $data_arr = $_GET;    }    elseif ($req_type=='POST' and isset($_POST["bt_register"]) ){        $data_arr = $_POST;    }    else {        die("wrong format response received<br>");        // We didn't handle the first run flaw correctly.        // Thus this message will be shown even if btregister is not pressed    }    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?><?php    // I had to make a new one because old one would die at login press    #---------------- reading our database ------------------------------------------    $req_type= $_SERVER['REQUEST_METHOD']; # Checking req type    if($req_type=='GET' and isset($_GET["bt_getall"]) ){        echo "Get all req received via get";    }    elseif ($req_type=='POST' and isset($_POST["bt_getall"]) ){        echo "Get all req received via post";    }    else {        die("no get all response received, dying now.");    }    echo "<br>";?>在表單中,我有兩個按鈕,bt_register和bt_getall。<?php ...?>對于每個按鈕的任務,我還有兩個 PHP 腳本。該bt_register腳本包含向 MySQL 控制臺添加條目的所有代碼(為簡潔起見已刪除)。bt_getall應該具有從 SQL 服務器獲取條目的所有代碼。按下時bt_register,第一個腳本成功運行,然后運行第二個腳本(這是錯誤的,但可以理解的行為:我希望bt_register只運行第一個腳本,但由于當前的 php 文件有兩個腳本,它會同時運行它們)。
查看完整描述

4 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

die()不應在您的網絡腳本中使用。有時在調試時作為臨時措施很有用,但在實時應用程序中使用它可能會導致災難。一個更好的選擇是使用異常,它可以被您的應用程序或 PHP 本身捕獲。


if(/*this is not allowed*/) {

    throw new \Exception('You are not allowed to do this!');

}

通常,您應該將應用程序設計為具有最少的此類意外情況。如果出現問題不是您的應用程序的錯,那么您的代碼不應該拋出任何錯誤或異常。編寫適當的驗證并以這樣的方式構建項目,以便當用戶提供無效輸入時,他們會得到一個很好的錯誤消息。


查看完整回答
反對 回復 2022-10-22
?
慕森卡

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 文件中。


查看完整回答
反對 回復 2022-10-22
?
子衿沉夜

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.

}


?> 


查看完整回答
反對 回復 2022-10-22
?
慕少森

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();

    }

?>


查看完整回答
反對 回復 2022-10-22
  • 4 回答
  • 0 關注
  • 189 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號