基于PDO的PHP登录系统

基于PDO的PHP登录系统,php,pdo,login-control,Php,Pdo,Login Control,我有1个页面(index.php)和2个表单,登录n注册。 我想要的只是当我点击登录按钮时,我会停留在这个页面上,表单消失了! 只需index.php和欢迎消息 我有两个文件: 1.php包含2个表单。 2.php(用户类),具有2个函数,登录名为n addNewUser 这是我的索引文件 <?php $user = new User(); if (isset($_POST['login'])) { $username = $_POST['username'

我有1个页面(index.php)和2个表单,登录n注册。 我想要的只是当我点击登录按钮时,我会停留在这个页面上,表单消失了! 只需index.php和欢迎消息

我有两个文件: 1.php包含2个表单。 2.php(用户类),具有2个函数,登录名为n addNewUser

这是我的索引文件

<?php
    $user = new User();
    if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $user->login($username, $password);
    }
    if (isset($_POST['signup'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];

        $user->add($username, $password, $email);
    }
?>
<form action='' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username' autofocus=''>
    <input type='password' name='password' placeholder='Password'>
    <input type='submit' name='login' value='Login' />
</form>

<form action='' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username'></br>
    <input type='email' name='email' placeholder='Email'></br>
    <input type='password' name='password' placeholder='Password'></br></br>
    <input type='submit' name='signup' value='Sign Up' />
</form>

请帮我完成这件事

那么为什么不放一些

<?php if (!$_SESSION['login']) { ?>
  ... show forms ..
<?php } ?>

... 出示表格。。

因此,表单仅在未执行登录时显示?

index.php中的所有内容:

<?php
function login($username, $password){
    session_start();
    if (!empty($username) && !empty($password)) {
        $stmt = $this->db->prepare("SELECT * FROM user WHERE username=? and password=?");
        $stmt->bindParam(1, $username);
        $stmt->bindParam(2, $password);
        $stmt->execute();
        if ($stmt->rowCount() == 1) {
            $_SESSION['login'] = true;
            $_SESSION['username'] = $username;
        } else {
            echo "Wrong username or password";
        }
    } else {
        echo "Please enter username and password";
    }
}

$user = new User();
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $user->login($username, $password);
}
elseif (isset($_POST['signup'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];

    $user->add($username, $password, $email);
}
else
{
?>
<form action='index.php' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username' autofocus=''>
    <input type='password' name='password' placeholder='Password'>
    <input type='submit' name='login' value='Login' />
</form>

<form action='index.php' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username'></br>
    <input type='email' name='email' placeholder='Email'></br>
    <input type='password' name='password' placeholder='Password'></br></br>
    <input type='submit' name='signup' value='Sign Up' />
</form>
<?php
}
?>






散列您的密码。
<?php
function login($username, $password){
    session_start();
    if (!empty($username) && !empty($password)) {
        $stmt = $this->db->prepare("SELECT * FROM user WHERE username=? and password=?");
        $stmt->bindParam(1, $username);
        $stmt->bindParam(2, $password);
        $stmt->execute();
        if ($stmt->rowCount() == 1) {
            $_SESSION['login'] = true;
            $_SESSION['username'] = $username;
        } else {
            echo "Wrong username or password";
        }
    } else {
        echo "Please enter username and password";
    }
}

$user = new User();
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $user->login($username, $password);
}
elseif (isset($_POST['signup'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];

    $user->add($username, $password, $email);
}
else
{
?>
<form action='index.php' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username' autofocus=''>
    <input type='password' name='password' placeholder='Password'>
    <input type='submit' name='login' value='Login' />
</form>

<form action='index.php' method='post' accept-charset='utf-8'>
    <input type='text' name='username' placeholder='Username'></br>
    <input type='email' name='email' placeholder='Email'></br>
    <input type='password' name='password' placeholder='Password'></br></br>
    <input type='submit' name='signup' value='Sign Up' />
</form>
<?php
}
?>