gpt4 book ai didi

php - $_SESSION 变量不工作,在其他页面不工作

转载 作者:可可西里 更新时间:2023-10-31 23:28:42 25 4
gpt4 key购买 nike

我已经在

创建了 session

登录页面

   <?php
session_start();
include 'dbconfig.php';
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM login WHERE username = '$username' AND password= '$password' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$_SESSION["userid"] = $row['user'];
echo $_SESSION["userid"];
}
} else {
echo "wrong";
}
$conn->close();
?>

获取登录信息

function login() {

var username = $("#username").val();
var password = $("#password").val();
if (username == "" || username == null || username == undefined || password == "" || password == null || password == undefined) {
$('#foremptyvalue').show();
} else {
$('#foremptyvalue').hide();
$('#loader').show();
jQuery.ajax({
url: baseurl + "login.php"
, data: 'username=' + username + '&password=' + password
, type: "POST"
, success: function (response) {
response = $.trim(response);
if (response == "wrong") {
$('#loader').hide();
$('#forwronginput').show();
$("#username").val('');
$("#password").val('');
} else {
$('#loader').hide();
$('#forwronginput').hide();
location.href = "account.php?id=" + response;
}

}
, error: function () {}
});
}

}

HEADER.PHP

在此页面中, session 变量不起作用。我使用了以下代码:

<?php
session_start();
echo session_id();
$session = $_SESSION["userid"];
?>

结果如下

ptarbkn67poq1gkch2dh6fvqc3 Notice: Undefined index: userid in C:\xampp\htdocs\feescounter\header.php on line 4

我也检查了 session 保存路径是否可写,并且是可写的。它将 session 保存在 C:\xampp\tmp

最佳答案

您必须在使用前启动 session 。从你的代码看来不是这样......尝试将下面的代码放在每个文件的最顶部,看看效果如何:

    <?php
// THIS SHOULD BE THE VERY FIRST LINES OF CODE IN YOUR SCRIPTS
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}

关于php - $_SESSION 变量不工作,在其他页面不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37018900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com