如果条件为真,如何从当前的PHP页面移动到另一个PHP页面?

时间:2017-10-03 21:45:40

标签: php html web web-applications web-deployment

我正在开展一个网络项目,其中我想从一个PHP页面移动到另一个Php页面,如果条件为真 ......

在下面的登录PHP页面中,我使用$ _POST []获取用户名和密码。如果用户名和密码在当前PHP登录页面的( if statement )中匹配,那么我想跳转到标题函数中指定的另一个PHP页面(choice.php)在if之后。

 <html>
 <body>
 <head>
 </head>
 <form method="post" action="login.php">

<div id="div1">
  <h1>welcome to bizdiary</h1>
  <div id="div2">

  <label >Username</label>
  <input id="name"  type="text" name="username" value="" 
       placeholder="username" />
  <label >Password</label><input type="text" name="password" value="" 
    placeholder="password"/>

  <input type='submit' name="login" value="login" >

     </form>

 <?php
      if(isset($_POST['submit'])){


      $username=$_POST['username'];
      $password=$_POST['password'];

     if($username=='root' && $password=='tiger'){
    header( "Location:http://localhost/bizdiary/choice.php" ); die;
      }
     }
   ?>

2 个答案:

答案 0 :(得分:1)

此代码应该有效:

文件顶部的HTML。 删除表单中的操作。

<?php 

if(isset($_POST['submit'])){
    $username=$_POST['username'];
    $password=$_POST['password'];

    $host = $_SERVER['HTTP_HOST'];  
    // Put in here the conditional that the request need to accomplish to redirect.
    if($username=='root' && $password=='tiger'){            
        header("Location: http://{$host}/choice.php");
    }
}

?>
<html>
    <body>
    <head>
    </head>
    <body>
        <form method="post">

            <div id="div1">
            <h1>welcome to bizdiary</h1>
            <div id="div2">

            <label >Username</label>
            <input id="name" type="text" name="username" value="" placeholder="username" />
            <label >Password</label>
            <input type="text" name="password" value="" placeholder="password"/>

            <input type='submit' name="login" value="login" >

        </form>
    </body>
</html>

答案 1 :(得分:-1)

你应该mysql控制。实施例

if ($_POST){
        $username = htmlspecialchars($_POST['username']);
        $password = htmlspecialchars($_POST['password']);

        if (empty($username) or empty($password)){
          echo 'Don't leave blank.';
        }else {
          $user = mysql_query('SELECT * FROM user WHERE username = "'.$username.'" AND password = "'.$password.'"');

          if (mysql_num_rows($user)){

            header('Location: asd.php');
          }else {
            echo 'Didn't find user.';
          }
        }
      }