duanliaoyin3171 2017-07-23 12:50
浏览 24
已采纳

在PHP中设置会话

I don't seem to understand how I can setup a session. I wrote this code:

<?php
require_once '../database/Database.php';
session_start();
$database = new Database('localhost', 'root', '');


if(isset($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    if($database->login($username, $password)){
        $loggedIn = $_SESSION['loggedIn'] = true; 
        header("location: ../dashboard/dashboard.php");
    }
    else{
        echo "You've failed to login";
    }
}
?>
<h2><!--<a href="../index.php" role="button" class="btn btn-info btn-circle"><i class="fa fa-home"></i></a>--><button class="btn btn-info btn-circle"><i class="fa fa-info"></i></button></h2>
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <div class="information">
            <h5>Additional Information</h5>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur malesuada, magna sed sagittis posuere, nulla massa vulputate erat, sed laoreet mi nisl id quam. Nunc sodales justo quis orci sagittis tincidunt. Aliquam consectetur, metus vel commodo molestie, dui nisl vulputate nunc, sit amet varius ex nisi a ante. Donec vitae semper enim. Nullam vulputate porttitor interdum. Phasellus vel vehicula ligula, sit amet consectetur nunc. Phasellus eget facilisis arcu, sed fringilla neque. Sed urna purus, dictum eu condimentum eu, egestas vitae lacus. Nam ultricies nec ligula quis consectetur. Suspendisse posuere nulla tortor, vel pulvinar nulla fermentum non. Sed quis nulla sed mi congue sollicitudin. Morbi consectetur mi quis mauris egestas commodo. Vivamus rhoncus vestibulum orci, quis aliquam velit convallis eget.
            </p>
        </div>
        </div>
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <img src="../assets/logo.png">
                </div>
                <div class="panel-body">
                    <form accept-charset="UTF-8" role="form" method="post">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="Username" name="username" type="text">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="password" type="password">
                            </div>
                            <input class="btn btn-info btn-block" type="submit" name ="login" value="Login">
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-4"></div>
    </div>
</div>

Database function:

public function login($username, $password){
        $this->connection = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, 'portal');
        $query = mysqli_query($this->connection, "SELECT * FROM users WHERE password='$password' AND username='$username'");
        if(mysqli_num_rows($query) != 0){
            return true;
        }
        else{
            return false;
        }
    }

I want to start a session when a user logs in and store this one. So when the user reloads the login page he/she's still logged in. Could anyone help me out

  • 写回答

1条回答 默认 最新

  • dongwei1855 2017-07-23 12:58
    关注

    Check session variable outside form validation part of you page

    if(isset($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    if($database->login($username, $password)){
        $loggedIn = $_SESSION['loggedIn'] = true; 
    }
    else{
        echo "You've failed to login";
    }
    }
    If(isset($_SESSION['loggedIn']) &&    $_SESSION['loggedIn']==true){
    header("location: ../dashboard/dashboard.php");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化