在PHP中设置会话

时间:2017-07-23 12:50:08

标签: php

我似乎不明白如何设置会话。我写了这段代码:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Data");
  var range = sheet.getDataRange();
  var values = range.getValues();
  var today = new Date(Date.now());
   today.setHours(0,0,0,0);
    Logger.log(today);

var setTotal;
var getToday = values[4].map(Number).indexOf(+today);
Logger.log(getToday);
for (i = 0; i < values.length; i++) {
 var getTotal = values[i][getToday];
       Logger.log(getTotal.slice(-1)[0]); 
 }
}

数据库功能:

<?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>

我想在用户登录并存储此会话时启动会话。因此,当用户重新加载他/她仍然登录的登录页面时。任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

检查页面

的表单验证部分之外的会话变量
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");
}