dongxiangchan0743 2015-09-25 16:26
浏览 43
已采纳

在访问页面之前创建会话

So when the user hits log in this code is executed: LoggedIn.php

<?php
include 'connect.php';
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    die ('Username and/or password does not exist!');
}
// Prepare our SQL 
if ($stmt = $mysqli->prepare('SELECT password FROM users WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    if(!$stmt->execute()){
    trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);
    } 
    $stmt->store_result(); 
    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($password);
        $stmt->fetch();      
        // Account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            header('location: userPage.php');
                    //**should I create the session here?**

        } else {
            echo 'Incorrect username and/or password!';
        }
    } else {
        echo 'Incorrect username blar password!';
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
?> 

OR should the session be created when they are on the userPage.php. This is the page that they get access to when they log on

<?php
ob_start();
include 'connect.php';
if(!isset($_SESSION['username']) || !isset($_SESSION['password']))
{
    header("location:http://www.fortunefilly.com/loginTemplate.php");
}
else
{
    session_start();
    $username =$_SESSION['username']  ;
}
?>

But I don't think its actually creating a session because I try to echo out $username but It doesn't work. Just a few pointers on the scenario would be helpful

Thank you in advance

  • 写回答

2条回答 默认 最新

  • douwo3665 2015-09-25 16:36
    关注

    If you plan to use/create/unset (whatsoever) the sessions, you must write session_start(); in the very beginning of your code:

    LoggedIn.php

    <?php
    include 'connect.php';
    session_start();
    if ( !isset($_POST['username'], $_POST['password']) ) {
        // Could not get the data that should have been sent.
        die ('Username and/or password does not exist!');
    

    Or in your userPage.php:

    <?php
    session_start();
    ob_start();
    include 'connect.php';
    if(!isset($_SESSION['username']) || !isset($_SESSION['password']))
    {
        header("location:http://www.fortunefilly.com/loginTemplate.php");
    }
    

    EDIT:

    Coming back to the problem now, you need to set the sessions, a good palce would be:

     if (password_verify($_POST['password'], $password)) {
                // Verification success! User has loggedin!
    
                header('location: userPage.php');
                        //**should I create the session here?**
    

    Taking it right out like a sore tooth:

    if(!isset($_SESSION['username'])){ //should do it
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)