简体   繁体   English

使用$ _SESSIONS检查用户登录

[英]Checking user login using $_SESSIONS

Below are the following scripts, the first one is checklogin.php. 以下是以下脚本,第一个是checklogin.php。 This matches up the username and password that is stored in MYSQL database. 这与存储在MYSQL数据库中的用户名和密码匹配。 Once this information has been checked they will get sent to their personal page by using a redirect function. 一旦检查了此信息,他们将使用重定向功能发送到其个人页面。

The bottom php script is user1's landing page. 底部的php脚本是user1的登录页面。 I want something on there that will confirm that this person has correctly logged in and is not entitled to view this page. 我想在那里确认该人已正确登录并且无权查看此页面。

At the moment, when i log in as user1 i get shown the page 3.php, ie it's saying that i am not correctly logged in. I know i need to set up a session like: $_SESSION[logged in'] == 'y'; 此刻,当我以user1身份登录时,显示的页面为3.php,也就是说,我没有正确登录。我知道我需要建立一个会话,例如:$ _SESSION [login in'] == 'y'; and i think this should go where the passwords are being compared to what is stored in the database. 而且我认为应该将密码与数据库中存储的密码进行比较。 At the moment I cannot enter my login details and be directed to the correct file at the end. 目前,我无法输入我的登录详细信息,最后无法定向到正确的文件。 Any help will be much appreciated. 任何帮助都感激不尽。

<?php

session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

mysql_connect("localhost", "root", "root") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
$exists = mysql_num_rows($query); //Checks if username exists
$table_users = "";
$table_password = "";

if ($exists > 0) {
    //IF there are no returning rows or no existing username

    //$_SESSION['logged in'] == 'y';

    while ($row = mysql_fetch_assoc($query)) {
        //display all rows from query
        $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
        $table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
        $table_id = $row['id'];
        $page_id = $row['page'];
    }

    if (($username == $table_users) && ($password == $table_password)) {
        // checks if there are any matching fields

        if ($password == $table_password) {
            $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
            $_SESSION['logged_in'] = 'y';
            //echo $table_id;
            //echo $page_id;

            redirect($page_id); //take the user to the page specified in the users table
        } else {
            echo "Login Failed";
        }
    } else {
        print '<script>alert("1. Incorrect Password!");</script>'; //Prompts the user
        print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
    }
} else {
    print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
    print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}

function redirect($page_id)
{
    /* Redirect browser */
    header('Location: '.$page_id);
    /* Make sure that code below does not get executed when we redirect.         */
    exit;
}

?>

And landing page 和目标网页

<?php

session_start();
//user logged in??

if ($_session['logged in'] != 'Y') {
    //No- jump to log in page.
    header("location: 3.php");
    exit();
}
else
{
    echo 'this works';
}

?>

You're defining the session like: 您正在定义会话,例如:

$_SESSION['logged in'] == 'y';

which should be: 应该是:

$_SESSION['logged in'] = 'y';

yet you check like: 但是您检查如下:

if ($_session['logged in'] != 'Y') {

it should be: 它应该是:

if ($_SESSION['logged in'] != 'y') {

You're checking if it's an uppercase Y while it holds a lowercase y . 您正在检查它是否为大写Y,而它是否为小写y So it will never succeed. 因此,它将永远不会成功。

Also $_SESSION is a superglobal which means: $_SESSION也是一个超全局变量 ,这意味着:

Superglobals — Superglobals are built-in variables that are always available in all scopes 超全局变量—超全局变量是内置变量,在所有范围中始终可用

and variables are case sensitive. 并且变量区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM