php会话变量值不要在同一页面更新

时间:2016-04-16 19:14:27

标签: php session variables

我有一个名为' logerr'的会话变量。如果变量等于0,则用户未记录或日志正常...但如果未找到user / pswd ..变量更改为1且主页面显示"登录错误 像这样 的index.php

<?php 
    session_start(); 
    if(isset($_SESSION['openclose']) && $_SESSION['openclose']=='1') {
        header("location:index2.php");
        exit();     
    }
?>
<?php 
   if(isset($_SESSION['logerr'])) {
    $errlog=$_SESSION['logerr'];
   } else {
    $errlog=0;   
   }
?>
bla
bla
 <?php
 if($errlog==1) {
  ?>
    <h5 class="redfont">Error de Login!!</h5>
       <?php
  }
    ?>                 
<h2>write user & pswd</h2>
 <form id="form1" name="form1" method="post" action="f1.php">

BLA BLA

f1.php

ob_start();
bla
bla

if($count==1){  <-- means if the user or paswd was found
session_start();
    $_SESSION['logerr']=0;
    $_SESSION['openclose'] = '1';
bla
bla
    header("location:index2.php");
    exit();

} else {
    $_SESSION['logerr']=1;
    header("location:index.php");
    exit();
}
?>

事情是登录错误短语永远不会出现,错误变量总是为零..

1 个答案:

答案 0 :(得分:0)

尝试将会话开始移动到if语句之前的行。在您的情况下,只有在您没有错误时才会启动会话,因此您无法设置错误变量。

    ob_start();
// bla bla
session_start(); // moved //

if($count==1){  <-- means if the user or paswd was found
$_SESSION['logerr']=0;
$_SESSION['openclose'] = '1';
bla
bla
header("location:index2.php");
exit();

} else {
$_SESSION['logerr']=1;
header("location:index.php");
exit();
}
?>