【问题标题】:session not starting in php会话未在 php 中启动
【发布时间】:2023-03-05 07:14:01
【问题描述】:

这里是我开始会话的地方。

login.php

<?php
    require_once("db.php");
    require_once("functions.php");

    if(isset($_POST['submit'])) {
        global $connection;

        $username = $_POST['username'];
        $safe_username = mysqli_real_escape_string($connection, $username);

        $password = $_POST['password'];

        $query = "SELECT * FROM admin WHERE username = '{$safe_username}'";
        $result = mysqli_query($connection, $query);
        if($row = mysqli_fetch_assoc($result)) {
            $set_password = $row['password'];
            $input_password = crypt($password, $set_password);

            if($input_password == $set_password) {
                session_start();
                $_SESSION['username'] = $safe_username;
                header("location:users-area.php");
            } else {
                die(header("location:index.php?loginFailure=true"));
            }
        } else {
            die(header("location:index.php?loginFailure=true"));
        }
    }
?>

listall.php

这是我检查它的地方。

<?php
session_start();
if(emtpy ($_SESSION['username'])){
    header("location:../login/index.php");
}

由于某种原因这不起作用,如果您未登录,它会带您返回登录页面,如果您正确登录,则允许您通过 listall.php。

希望有人能找到问题。

【问题讨论】:

  • listall.php 应该是if(empty($_SESSION['username']))
  • session_start() 必须首先调用,无论逻辑如何。把它贴在页面顶部,在
  • 你写的是 ETMPY 而不是 EMPTY

标签: php session


【解决方案1】:

session_start() 必须位于 login.php 页面的顶部。

<?php
    session_start();
    require_once("db.php");
    require_once("functions.php");

然后将其从代码的其余部分中删除。

【讨论】:

  • 嗨,John,请注意:OP 的 if(emtpy ($_SESSION['username'])){ 有错字。确信 OP 会带着另一个问题回来。通知emtpy ;-)
【解决方案2】:

将 login.php 更改为:

<?php
    session_start();
    require_once("db.php");
    require_once("functions.php");

    if(isset($_POST['submit'])) {
        global $connection;

        $username = $_POST['username'];
        $safe_username = mysqli_real_escape_string($connection, $username);

        $password = $_POST['password'];

        $query = "SELECT * FROM admin WHERE username = '{$safe_username}'";
        $result = mysqli_query($connection, $query);
        if($row = mysqli_fetch_assoc($result)) {
            $set_password = $row['password'];
            $input_password = crypt($password, $set_password);

            if($input_password == $set_password) {
                $_SESSION['username'] = $safe_username;
                header("location:users-area.php");
            } else {
                die(header("location:index.php?loginFailure=true"));
            }
        } else {
            die(header("location:index.php?loginFailure=true"));
        }
    }
?>

【讨论】:

    【解决方案3】:

    您必须在 Login.php 文件中启动会话。否则您将无法在 login.php 文件中使用会话。

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多