我尝试将某些值保存到$ _SESSION ['']特征,但当我尝试在其他页面中访问它们时,不会打印这些值。
我还尝试在$ GLOBALS数组中保存相同的值但得到了相同的结果。
这里是我获取值的代码,并尝试登录
<?php
session_start();
include '../php/func.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Inicio de sesión</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../css/styles.css">
</head>
<body>
<div class="todo">
<div class="image">
<img class="img" src="../images/logo.png" title="Equipos" alt="Equipos">
</div>
<div class="wrapper">
<div class="container form-style-6">
<article>
<center>
<div class="col-lg-12">
<h2 id="h2h">Iniciar Sesión</h2>
<form action="../php/func.php" method="POST">
<p class="campos1">Matricula:</p> <input type="text" id="tfCorreo1 " value="" name = "userName"></br>
<p class="campos1">Contraseña:</p> <input type="password" id=" tfPassword1" value="" name = "userPass"><br></br>
<input type="Submit" name="bLogin" value="Iniciar Sesion" "><br></br>
<input type="button" name="bCrear" value="Crear Cuenta" ;">
</form>
<div id="result2"></div>
</div>
</center>
</article>
</div>
</div>
</div>
</body>
</html>
这里是后端部分,我在那里进行数据库连接并尝试使用从上面发送的值进行登录,我还添加了函数来检查存储在globals和session中的值
<?php
$enlace;
$SessionUser;
$SessionName;
$SessionAdmin;
if($_REQUEST['bLogin']){
$Username = $_REQUEST['userName'];
$Password = $_REQUEST['userPass'];
login($Username, $Password);
}
function SetGlobals($IdUser, $UserName, $AdminStatus){
$GLOBALS['SessionUser'] = $IdUser;
$GLOBALS['SessionName'] = $UserName;
$GLOBALS['SessionAdmin'] = "1";
}
function CheckSessionValues(){
echo "<br> Session Values:";
echo "<br> Session User: ". $_SESSION["SessionUser"];
echo "<br> Session Name: ". $_SESSION['SessionName'];
echo "<br> Session Admin: ". $_SESSION['SessionAdmin'];
}
function CheckGlobals(){
echo "<br> Globals Values: ";
echo "<br> Session User: ". $GLOBALS['SessionUser'];
echo "<br> Session Name". $GLOBALS['SessionName'] ;
echo "<br> SessionAdmin ". $GLOBALS['SessionAdmin'];
}
function IniciarConexion(){
$GLOBALS['enlace'] = mysqli_connect('localhost' , 'root', 'mysql');
mysqli_set_charset($GLOBALS['enlace'],'utf8');
if(!$GLOBALS['enlace']){
die('No se pudo conectar a la db' . mysql_error());
}
if (!mysqli_select_db($GLOBALS['enlace'],'novus')) {
die('No se pudo seleccionar la base de datos: ' . mysql_error());
}
}
function CerrarConexion(){
mysqli_close($GLOBALS['enlace']);
}
function login($loginUser, $loginPass){
if((strcmp($loginUser,'') == 0) || (strcmp($loginPass,'') == 0)){
$response = False;
}else{
IniciarConexion();
$QuerySQL = "SELECT * FROM user WHERE user_id = '$loginUser';";
//echo "$QuerySQL";
$transaccionSQL = mysqli_query($GLOBALS['enlace'], $QuerySQL);
if(!$transaccionSQL){
die("Hubo un problema al iniciar sesion, Intente mas tarde");
$response = False;
}else{
//echo sha1($loginPass);
$row = mysqli_fetch_object($transaccionSQL);
//$response = "$row->user_id $row->password $row->activo";
$Pass = $row->password;
//echo "Response: $response";
$Activo = $row->activo;
$Admin = $row->admin;
$User = $row->user_id;
$UserName = $row->user_name;
echo "$Pass <br>".sha1($loginPass,False);
$response = False;
if($Activo == 1){
//if($Pass == sha1($loginPass)){
if(strcmp($Pass, sha1($loginPass)) == 0){
$response = True;
//session_start();
SetGlobals($User, $UserName,$Admin);
$_SESSION["SessionUser"] = $User;
$_SESSION['SessionName'] = $UserName;
$_SESSION['SessionAdmin'] = $Admin;
}
}
}
if($response){
echo "El inicio de sesión sera: exitoso";
header("location: home.php");
}else{
$message = "Usuario o contraseña incorrecta, favor de intentar nuevamente";
echo "<script type='text/javascript'>alert('$message');</script>";
header("location: login.php");
}
CerrarConexion();
}
return $response;
}
function logout(){
session_destroy();
}
?>
最后这里是我想要获取数据的页面,我使用这两种方法打印值,以防其中任何一个工作
<?php
session_start();
include '../php/func.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Inicio</title>
<?php
CheckGlobals();
CheckSessionValues();
?>
</head>
<body>
<h1> Recursos Educativos </h1>
</body>
</html>
所以我想知道我在做什么,是否应该让session_start()调用其他地方或者通过使用位置方法更改页面而错过某些内容但是我只是在打印值时获取空白
答案 0 :(得分:0)
你必须使用session_start();在所有页面中。