2014-09-20 53 views
0

薩拉姆所有球員的...我已經陷在多個功能在PHP單頁在頁面PHP多功能

例如我創建了一個登錄()和家庭()時,我做我的登錄它顯示家庭但仍然登錄哪有我只能一次查看一個功能..

這裏是腳本

<?php 

function login() { 
    if (isset($_POST['sub'])) { 
     $user = $_POST['user']; 
     $gender = isset($_POST['check']); 

     if ($user == "admin" && $gender == "male") { 
      echo "oh boy you have access to this site"; 
      home(); 
     } else if ($user == "admin" && $gender == "female") { 
      echo " Hey Lady.. You have access to this site"; 
      home(); 
     } else if ($gender == "" || $gender != "male" || $gender != "female") { 
      echo "please select gender"; 
     } else if ($user == "" || $user == " ") { 
      echo "input username"; 
     } else { 
      echo " '" . $user . "' " . " you enter is wrong"; 
     } 
    } 
} 

?> 



<?php 
login(); 
?> 
<form method="POST" > 
<input type="text" name="user" placeholder="username" /><br> 
Male: <input type="radio" value="male" name="check"> female : <input type="radio" 
value="female" name="check" > <br> <input type="submit" name="sub"> 
</form> 

<?php 

function home() { 
?> 

    <p><center>Practice of string a simple test home() </center></p> 
<?php 

    $a = "multi thinker "; 

    $w = ucwords($a); 
    $trim = trim($a, " "); 
    $trim = ltrim($a, " "); 
    $trim = rtrim($a, " "); 

    echo $w . " " . $trim; 

} 
?> 
+0

$ gender = isset($ _ POST ['check']);將把性別設置爲合乎邏輯的。我不認爲這就是你想要的,因爲你後來爲男性,女性或空洞檢查它。 – 2014-09-20 18:29:10

+0

@Len_D這只是一個練習腳本移動到功能點離開這個性別現在:) – 2014-09-20 18:35:21

回答

1
<?php 
    function home(){ 
     header("Location: http://google.com"); 
    } 

    function login() { 
     if (isset($_POST['sub'])) { 
      $user = $_POST['user']; 
      $gender = isset($_POST['check']); 

      if ($user == "admin" && $gender == true) { 
       echo "oh boy you have access to this site"; 
       home(); 
      } else if ($user == "admin" && $gender == false) { 
       echo " Hey Lady.. You have access to this site"; 
       home(); 
      } else if ($gender == "" || $gender != "male" || $gender != "female") { 
       echo "please select gender"; 
      } else if ($user == "" || $user == " ") { 
       echo "input username"; 
      } else if ($user != "admin") { 
       echo " '" . $user . "' " . " you enter is wrong"; 
      } else { 
      } 
     }else{ 
      //$_POST['sub'] does not exist, therefore output login form 
      $out = ' 
       <form method="POST" > 
        <input type="text" name="user" placeholder="username" /><br> 
        Male: <input type="radio" value="male" name="check"> 
        Female : <input type="radio" value="female" name="check" > <br> 
        <input type="submit" name="sub"> 
       </form>'; 
      echo $out; 
     } 
    } 

login(); 

?> 
+0

* *謝謝.. :-)** – 2014-09-21 03:49:43

1

如果你打算把登錄和家庭內容在同一頁面中,你需要一些狀態以指示登錄是否成功。我看到您在登錄功能中設置了$ user。也許會讓一個全局變量,並檢查它的存在,像這樣:

<?php 
$user = null; //nothing yet, will be set on successful login 

function login() { 
    global $user; 
    ... //same as you have 
} 

if($user === null) { //have not logged in yet 
    login(); //try to log in if we have post data 
} 
if($user === null) { //login failed or was not submitted yet 
    ?> 

    <form method="POST"> 
    ... your login form goes here ... 
    </form> 

    <?php 
    return; //do not show the rest of the home page since we showed login 
} //end if $user === null 

function home() { 
    ... your home function here ... 
} 
home(); 
?> 

可以改善的安排,但基本的想法是有一個if-then-else的上分支(a)是登錄表單現在被提交,並且(b)已經成功登錄用戶。

將它分成多個文件會更好。有很多方法可以做到這一點,例如檢查登錄,如果缺少發出標題(「位置:http://mydomanin.com/login」)重定向,則登錄頁面成功後,登錄頁面會重定向。

您還可以將$用戶存儲在會話cookie中而不是全局變量中,以便用戶不必在每次刷新頁面時都登錄。

+0

感謝guidness ..我明白。當登錄發生時,home()不會顯示。登錄發生後,home()出現,但home()登錄()也出現我只想在登錄完成後回家時,只有登錄時沒有登錄完成.. :) – 2014-09-20 18:41:37