PHP:开始SESSION无法正常工作

时间:2017-05-12 07:41:35

标签: php html session

我在用户登录时开始会话,但是当我尝试从会话中拉出时,它就像它不在那里一样。任何想法为什么?

用于登录页面的PHP

session_start();
if (isset($_POST) & !empty($_POST)) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM `web_accouts` WHERE username='$username' AND password='$password'";
    $result = mysqli_query($conn, $sql);
    $count = mysqli_num_rows($result);
    if ($count == 1) {
        $_SESSION['username'] = $username;
        header('location: /main-page.php');
        die();
    } else {
        $fmsg = "Invalid Username/Password";
    }
}

PHP拉取信息

<?php
    echo "Username is " . $_SESSION["username"] . ".<br>";
?>

我已经尝试了所有我能想到的东西,但我是PHP的新手

1 个答案:

答案 0 :(得分:0)

另外为了防止某些php的通知(例如会话已经开始在线... ),请执行此操作

<?php

if(!isset($_SESSION) && session_id() == ''){//only start session if it hasnt been started
session_start(); 
}
?>