帮助从数据库中提取当前记录的用户ID和名字

时间:2017-04-10 08:36:59

标签: php mysql session login

我正在建立一个只有一个欢迎区域的学习规划器,登录后。我目前正在尝试获取当前登录用户的用户ID,以便在此欢迎区域中的SQL更新查询中使用。作为用户在欢迎信息中使用的名字。

我尝试在$_SESSION['user_id'] = userid; $_SESSION['user_firstname'] = firstname;之后正常工作$_SESSION['login_user'] = $username;($_SESSION['login_user'] = $username;但是在登录页面时,我收到了以下错误:{{1 }和Notice: Use of undefined constant userid - assumed 'userid' in C:\wamp64\www\justread\session.php on line 11

现在,我知道错误要求我先使用'userid'和'firstname'进行某种初始化,然后才能使用它们设置会话变量,但我不知道如何去做,所以我想知道如果有人可以帮助我,请。

提前感谢你。

如果需要,我可以发布更多代码,但我相信的代码是:

的login.php:

Notice: Use of undefined constant firstname - assumed 'firstname' in C:\wamp64\www\justread\session.php on line 12

session.php文件

<?php

// Start session
session_start();
// Variable to store error message
$error ="";
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            unset($_SESSION['error']);
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // Set the user ID of the user
            $_SESSION['user_id'] = userid;
            // Set the user first name of the user
            $_SESSION['user_firstname'] = firstname;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // and tell user that the login credentials are invalid.
            $error = "Your username or password is invalid";
            $_SESSION['error'] = $error;
            // redirect user to the home page (index.php)
            header("Location: index.php");
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}

1 个答案:

答案 0 :(得分:0)

在PHP中,变量名以&#39; $&#39;开头。标志。此外,在login.php中,您必须使用mysqli_fetch_row或任何类似的函数来获取数据。假设您在登录后重定向到session.php。在这种情况下,您不必为会话变量分配任何内容。它已经存在了。您所要做的就是访问它。

的login.php

<?php

// Start session
session_start();
// Variable to store error message
$error ="";
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            $row = mysql_fetch_object($query);
            unset($_SESSION['error']);
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // Set the user ID of the user
            $_SESSION['user_id'] = $row->userid;
            // Set the user first name of the user
            $_SESSION['user_firstname'] = $row->firstname;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // and tell user that the login credentials are invalid.
            $error = "Your username or password is invalid";
            $_SESSION['error'] = $error;
            // redirect user to the home page (index.php)
            header("Location: index.php");
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}

session.php文件

<?php

// Establish connection to the server
$mysqli = mysqli_connect("localhost", "root", "");
// Selecting Database
$db = mysqli_select_db($mysqli, "p00702");
// Starting session
session_start();

if (!isset($_SESSION['user_id'])) {
    // Closing Connection
    // Redirecting To Home Page
    header('Location: index.php');
    // Make sure that codes below do not execut upon redirection.
    exit;
}
print_r($_SESSION);

此外,将连接部分移动到单独的文件并将其包含在所有脚本中,这样当您的凭据更改时,您不必在所有文件中更改它。