2014-09-29 89 views
1

我其實有很多問題。但他們是相關的。PHP - self form post and exit

首先,使用自己的帖子提交表單是不好的做法?它有什麼不好的安全問題嗎?之所以我決定使用自己的帖子是因爲我不想透露我使用的文件/文件夾結構和服務器端編程語言。

例如,這清楚地表明我使用的是PHP,並且logout.php文件位於兄弟級別的註銷文件夾中。

<form action="../logout/logout.php" method="post"> 
    //form elements goes here 
</form> 

有了這個例子,我沒有表現我在用服務器端語言,但程序員清楚地知道,這是自交和邏輯必須先通過自身:

<?php 
    if (isset($_POST['log_out'])) 
    { 
    // do some stuff 
    } 
?> 
<form method="post"> 
    //form elements goes here 
</form> 

我意識到自己有一篇自我發表的結論是,我必須在頁面的每個自我發佈開始時都有一個複雜的邏輯。這些邏輯將不得不選擇正確的執行代碼,有時會重定向我並停止執行其餘的代碼。

例如,在這裏我有一個頁面可以處理多個事件併發布到自身。爲了停止執行剩餘的代碼或HTML波紋管,我需要使用PHP函數「exit」。

<?php 
    if (isset($_POST['add_two_numbers'])) 
    { 
    // code for adding two numbers 
    exit(); 
    } 
    else if (isset($_POST['add_three_numbers'])) 
    { 
    // code for adding three numbers 
    exit(); 
    } 
    else if (isset($_POST['log_out'])) 
    { 
    // destroy session and data before logging out 
    header("location: ./logout/logout.php"); 
    exit(); 
    } 

    // php code that should happen if there is no post logic and 
    // will be unapropiately executed if exit is not used 
?> 

<html> 
    <head> 
    <script> 
    function init() 
    { 
     // will do stuff if php did not exit properly 
    } 

    // more js code 

    window.onload = init; 

所以,我必須使用PHP命令退出,我讀了很多關於它如何導致的問題,它是壞的,一些人建議使用return代替。但是,返回的問題是,它並不總是停止繼續執行特別是html部分的代碼。那麼有沒有更好的方法來做到這一點?

謝謝。

+3

我不知道你一直在讀什麼,但自發布並退出就好了。 – 2014-09-29 19:09:35

+3

_「我不想透露我正在使用的文件/文件夾結構和服務器端編程語言」_--這完全是無稽之談。通過默默無聞的安全行不通。 – CBroe 2014-09-29 19:10:36

+0

如果您試圖隱藏您的服務器端語言爲php的事實,那麼文件擴展名只是您需要隱藏的許多事情之一。無論如何,@CBroe說遮擋不安全 – andrew 2014-09-29 19:13:38

回答

1

如果您希望每次都嘗試保持頁面不會一次加載所有代碼,只需將每個主邏輯放在一個單獨的文件中,如果請求每個元素,就可以將其包含並運行。

即使保持包含所有這些其他包含的單個文件包含選項,也可能更清晰並更易於實現。

你將不得不代碼的網頁(但你他們的名字):

add.two.php 
add.three.php 
logout.php 
default.php 

的index.php

<?php 
    if(isset($_POST['add_two_numbers'])) { 
      require('/directory/hierarchy/add.two.php'); 
      exit(); 
     } 
    elseif(isset($_POST['add_three_numbers'])) { 
      require('/directory/hierarchy/add.three.php'); 
      // code for adding three numbers 
      exit(); 
     } 
    elseif(isset($_POST['log_out'])) { 
      // destroy session and data before logging out 
      header("location: ./logout/logout.php"); 
      exit(); 
     } 
    else 
     require('/directory/hierarchy/default.php'); ?> 

<html> 
    <head> 
    <script> 
    function init() 
    { 
     // will do stuff if php did not exit properly 
    } 

    // more js code 

    window.onload = init; 
1

如果我記得,PHP中的自我形式發佈被稱爲「粘滯形式」,這可能在任何方面都很好,沒有任何錯誤(以我的經驗)。我個人使用粘性表單進行表單字段驗證和/或輸出附加信息(狀態,錯誤等)。 爲了避免退出()你可能應該改變你的網頁邏輯,但很難在這一刻給你一個建議:)。