2014-09-20 68 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