我在
创建了会话登录页面
<?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
在此页面中,会话变量无效。 我使用下面的代码:
<?php
session_start();
echo session_id();
$session = $_SESSION["userid"];
?>
结果如下
ptarbkn67poq1gkch2dh6fvqc3 注意:未定义的索引:userid in C:\ xampp \ htdocs \ feescounter \ header.php 4
我还检查会话保存路径是否可写并且是可写的。 它在C:\ xampp \ tmp
中保存会话答案 0 :(得分:1)
您必须在使用之前启动会话。从你的代码中看起来并不是这样...... 尝试将下面的代码放在每个文件的最顶层,看看它是怎么回事:
<?php
// THIS SHOULD BE THE VERY FIRST LINES OF CODE IN YOUR SCRIPTS
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
答案 1 :(得分:1)
您的header.php文件应为: -
<?php
if (!session_id()) {
session_start();
}
if(!empty($_SESSION['userid'])){
echo $_SESSION['userid'];
}else{
echo 'guest user';
}