表单中的make按钮读取php代码

时间:2018-01-04 08:16:08

标签: php jquery html button

我试图点击按钮 读取第一个php代码(这样他可以检查用户名和密码,如果它们正确移动到mainPage),但它没有& #39; t work

这是我的login.php代码

<?php
    session_start();

    if(isset($_POST['Loginbtn'])){
        $una=$_POST['userName'];
        $pass=$_POST['password1'];
        if($una=='Admin'&&$pass=='Admin'){
            $_SESSION['user']=$una;
            header('Location: MainPage.php');
        } else {
            $error = "Username or Password is invalid";
        }
    }
?>

这是我的signIn.php页码:

<?php
    include('login.php'); // Includes Login Script
    if(isset($_SESSION['login_user'])){
        header("location: home.php");
    }
?>

<!DOCTYPE html>
<html>
    <body>
      <div>
        <div id="rectangle"></div>
            <form id="Form01" name="myForm2" action="" method="post">
                <p id="usern01"> user name</p> <input type="text" name="userName" id="username" > <label id=usernameError></label>
                <p id="password01">password  </p><input type="password" name="password1" id="password"> <label id=passError></label>
                <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="button"  id="Loginbtn">    logIn    </button>
            </form>
        <div>
    </body>
</html>

这是我的Mainpage.php代码(如果有帮助的话)

<?php
    session_start();
    if((!$_SESSION['user'])){
        header("Location: signIn.php?problem=notLoggedIn");
    }

    if(isset($_POST['logoutt'])){
        session_destroy();
        header('Location: signIn.php?problem=goodbye');
    }
?>

3 个答案:

答案 0 :(得分:1)

<?php
session_start();
if(isset($_POST['Loginbtn'])){
    $una=$_POST['userName'];
    $pass=$_POST['password1'];
    if($una=='Admin' && $pass=='Admin'){
        $_SESSION['user']=$una;
        header('Location: Mainpage.php');
    }
    else {
        $error = "Username or Password is invalid";
    }

}
?>

的login.php

<?php
include('login.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
    header("location: home.php");
}
?>
<!DOCTYPE html>
<html>
 <body >
  <div >
 <div id="rectangle"></div>
  <form id="Form01" name="myForm2" action="
  <?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <p id="usern01"> user name</p> <input type="text" name="userName" 
 id="username" > <label id=usernameError></label>
  <p id="password01">password  </p><input type="password" 
 name="password1" id="password"> <label id=passError></label> <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <button type="submit"  name="Loginbtn" id="Loginbtn">    logIn    
  </button>
  </form></body></html> 

此代码在使用您的用户名和密码登录后重定向到mainpage.php。希望这会对你有所帮助

答案 1 :(得分:1)

1。您没有在SESSION数组中设置用户,请将其设置为$_SESSION['login_user']=$una;您正在寻找SESSION数组中的user_login索引< / p>

if(isset($_SESSION['login_user'])){
    header("location: home.php");
}

返回false,因为您还没有设置它。

2。您的表单未提交,因为您的按钮类型未设置为提交。将按钮代码更改为<input type="submit" name="Loginbtn" value="Login">

或到

<button type="submit" id="Loginbtn" name="Loginbtn">Login</button>它会起作用。

答案 2 :(得分:0)

请将此更新的代码用于'signIn.php'文件

<?php
include('login.php'); // Includes Login Script
if (isset($_SESSION['login_user'])) {
    header("location: home.php");
}
?>
<!DOCTYPE html>
<html>
    <body>
        <div>
            <div id="rectangle"></div>
            <form id="Form01" name="myForm2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <p id="usern01"> user name</p> <input type="text" name="userName" id="username" > <label id=usernameError></label>
                <p id="password01">password  </p><input type="password" name="password1" id="password"> <label id=passError></label> <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="submit"  id="Loginbtn">    logIn    </button>
            </form>
    </body>
</html>