我有一个非常简单的PHP脚本,它接受XML的值并将其保存在$ _SESSION中。但是,当页面更改并且我想要检索此值时,它们是空的。以下是采用值的方法:
session_start();
if (isset($_REQUEST["LocalitatForm"])) {
recollirDades();
}
function recollirDades(){
$localitat = $_REQUEST["localitat"];
$data = $_REQUEST["data"];
$min = 0;
date_format($data, 'Y-m-d');
$xmlDoc = simplexml_load_file("../data/".$localitat.".xml");
foreach ($xmlDoc->prediccion->dia as $dia){
if($dia['fecha'] == $data){
$min = $dia->temperatura->minima;
$max = $dia->temperatura->maxima;
}
}
$_SESSION["localitat"] = $localitat;
$_SESSION["data"] = $data;
$_SESSION["min"] = $min;
$_SESSION["max"] = $max;
header("location: ../views/Temperatura.php");
}
以下是应该显示值的文档(“Temperatura.php”):
<?php
session_start();
echo $_SESSION["localitat"]." ". $_SESSION["data"] . " " .
$_SESSION["min"];
$localitat = $_SESSION["localitat"];
$data = $_SESSION["data"];
$min = $_SESSION["min"];
$max = $_SESSION["max"];
?>
<html>
<head>
<title>Temperatura</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id='data'>
Localitat: <?php echo ucfirst($localitat)?><br>
Data: <?php echo $data ?><br>
Temperatura mínima: <?php echo $min ?>ºC<br>
Temperatura màxima: <?php echo $max ?>ºC<br>
</div>
<a href='../index.php'><input type='button' value='Tornar a buscar'>
</a>
</body>
</html>
在第一个文件中,值是正确的,我已经用回声检查了它。
答案 0 :(得分:0)
好的,我发现了问题。您无法立即在$ _SESSION中存储XML节点的值。在这种情况下,我将其转换为字符串。所以方法的代码是这样的:
session_start();
if (isset($_REQUEST["LocalitatForm"])) {
recollirDades();
}
function recollirDades(){
$localitat = $_REQUEST["localitat"];
$data = $_REQUEST["data"];
$min = 0;
date_format($data, 'Y-m-d');
$xmlDoc = simplexml_load_file("../data/".$localitat.".xml");
foreach ($xmlDoc->prediccion->dia as $dia){
if($dia['fecha'] == $data){
$min = (string)$dia->temperatura->minima;
$max = (string)$dia->temperatura->maxima;
}
}
$_SESSION["localitat"] = $localitat;
$_SESSION["data"] = $data;
$_SESSION["min"] = $min;
$_SESSION["max"] = $max;
header("location: ../views/Temperatura.php");
}