dongweiben5229 2017-04-10 08:36
浏览 260

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

I am building a study planner that features just a single welcome area, after logging in. I am currently trying to obtain the user ID of the currently logged in user for use in an SQL update query in this welcome area, as well as the user’s first name for use in the welcome message.

I have tried putting $_SESSION['user_id'] = userid; and $_SESSION['user_firstname'] = firstname; after $_SESSION['login_user'] = $username; ($_SESSION['login_user'] = $username; works fine by the way) but upon logging into the page, I get these errors: Notice: Use of undefined constant userid - assumed 'userid' in C:\wamp64\www\justread\session.php on line 11 and Notice: Use of undefined constant firstname - assumed 'firstname' in C:\wamp64\www\justread\session.php on line 12.

Now, I know the errors want me to do some sort of initialisation for ‘userid’ and ‘firstname’ first before using them to set up a session variable but I am not sure how to go about it, so I am wondering if someone could help me, please.

Thanking you in advance.

I can post more codes if required but the codes I believe are concerned are:

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) {
            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);
    }
}

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();
// Storing Session
$user_check = $_SESSION['login_user'];
$_SESSION['user_id'] = userid;
$_SESSION['user_firstname'] = firstname;
// Test to see the content of the global session variable
print_r($_SESSION);
// SQL Query To Fetch Complete Information Of User
$ses_sql = mysqli_query($mysqli, "SELECT username FROM logins WHERE username='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session = $row['username'];
if (!isset($login_session)) {
    // Closing Connection
    mysqli_close($mysqli);
    // Redirecting To Home Page
    header('Location: index.php');
    // Make sure that codes below do not execut upon redirection.
    exit;
}
  • 写回答

1条回答 默认 最新

  • duanhe0817825 2017-04-10 08:39
    关注

    In PHP, variable name starts with '$' sign. Also, in login.php, you have to fetch the data using mysqli_fetch_row or any similar function. Am assuming you are redirecting to session.php after logging in. In that case, you don't have to assign anything to the session variables. It will be there already. All you have to do is to access it.

    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);
    

    Also, move the connection part to a seperate file and include it in all scripts, so that when your credentials change, you don't have to change it in all the files.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决