PHP注册后登录用户

时间:2015-08-05 03:06:32

标签: php mysql

我正在创建一个简单的登录和注册表单。我尝试做的是当用户注册时,它应该登录。为了登录,需要将用户注册的ID发送到主页,以便用户名可以显示。我不确定我的代码有什么问题。

寄存器:

<!DOCTYPE html>
<?php

    session_start();
    if(isset($_SESSION['userID']) AND !empty($_SESSION['userID'])) {
        header("Location: home.php");
    }

    if(isset($_POST['register'])) {
        $firstName = mysqli_real_escape_string($dbConnect, $_POST['firstName']);
        $lastName = mysqli_real_escape_string($dbConnect, $_POST['lastName']);
        $username = mysqli_real_escape_string($dbConnect, $_POST['username']);
        $email = mysqli_real_escape_string($dbConnect, $_POST['email']);
        $password = mysqli_real_escape_string($dbConnect, $_POST['password']);

{           // Check if data exists already in the database
            $exists = mysqli_query($dbConnect, "SELECT user_id, username, email FROM users WHERE username = '$username' AND email = '$email'");
            $row = mysqli_fetch_array($exists);

        $dbusername = $row['username'];
        $dbemail = $row['email'];

        if ($username == $dbusername) {
            die("Username already taken.");
        }   else if ($email == $dbemail) {
            die("Email already registered.");
        }
}

        $registerUser = "INSERT INTO users (first_name, last_name, username, email, password) VALUES('$firstName', '$lastName', '$username', '$email', '$password')";

{           // Select ID from registered user

        $selectID = "SELECT user_id FROM users WHERE username = '$username'";
        $selectID_Query = mysqli_query($dbConnect, $selectID);

        $fetch = mysqli_fetch_array($selectID_Query);
        $userID = $fetch['user_id'];

        $_SESSION['userID'] = $userID;
}

        if(mysqli_query($dbConnect, $registerUser)) {
            header("Location: home.php");
        }   else {
            echo "<script>alert('error while registering you...');</script>";
        }
    }

    include "includes/head.php";
    include "includes/nav.php";
?>
    <div id="main-content">
        <div class="welcome-msg">
            <h1 class="huge">Registration form</h1>
            <h3 class="medium">Please fill in all the inputs</h3>

            <form id="login-form" method="post">
                <label for="firstName">First Name</label>
                <input type="text" name="firstName" id="firstName" required>

                <label for="lastName">Last Name</label>
                <input type="text" name="lastName" id="lastName" required>

                <label for="username">Username</label>
                <input type="text" name="username" id="username" required>

                <label for="email">Email</label>
                <input type="email" name="email" id="email" required>

                <label for="password">Password</label>
                <input type="password" name="password" id="password" required>

                <button type="submit" name="register">Register</button>
            </form>

        </div>
    </div>

</body>

主页:

<!DOCTYPE html>
<html>
<?php

session_start();

if(!isset($_SESSION['userID'])) {
    header("Location: index.php");
}

$tUsers_Select_Query = mysqli_query($dbConnect, "SELECT * FROM users WHERE user_id=".$_SESSION['userID']);
$row = mysqli_fetch_array($tUsers_Select_Query);

include "includes/head.php";
include "includes/nav.php";

?>

    <div id="main-content">
        <h1 class="huge">Welcome back, <?php echo $row['username'] ?>!</h1>
        <a href="/lr/logout.php?logout">Logout</a>
    </div>


</body>

如果您需要更多详情,请发表评论。

2 个答案:

答案 0 :(得分:0)

尝试首先检查会话$_SESSION['userID']的值。你试过在你的DOM中检查它吗?会话值必须显示在那里。

答案 1 :(得分:0)

我必须使用的是函数mysqli_insert_id。在检查SQL命令是否正确的if语句中,我只需要添加

$_SESSION['userID'] = mysqli_insert_id($dbConnect);

如果最后一个SQL查询是UPDATE或INSERT查询,它将返回ID。例如,如果查询是SELECT,则它将返回0。

$registerUser = "INSERT INTO users (first_name, last_name, username, email, password) VALUES('$firstName', '$lastName', '$username', '$email', '$password')";

    if(mysqli_query($dbConnect, $registerUser)) {
        $_SESSION['userID'] = mysqli_insert_id($dbConnect);
        header("Location: home.php");
    }   else {
            echo "<script>alert('error while registering you...');</script>";
    }

http://php.net/manual/en/mysqli.insert-id.php