doukai1226 2017-02-04 00:26
浏览 58
已采纳

我需要帮助在php页面上获取表单以在提交时强制刷新页面

Please know that I did research this, and none of the answers worked, I am working on a PHP project and have run into a strange error. well not really an error but a qirk, that i really dislike...

I have a index.php page, which includes a navbar page which has a login form, when you log in with success it requires you to press the login button twice to get the page to finally refresh and display the fact that your log in worked... it is the same with the log out button. I have had a few almost successes but all of them eventually crash my page or throwing my page into some loop that never ends, or again crashes the page out.

I will add the code I can, mind you that there is a lot so please bear with me...

//From the index page//
          <!-- Log out commands -->
      <?php
        if (isset($_POST["logoffuser"])){
          set_Logout();
          $_POST = array();
          include_once("index.php");
        }
      ?>

At this point the snippet above should run the function to clear the $_SESSION variables set in the Users.php, this works... somewhat, It then is supposed to reload the page by virtue of the form submission. shown next:

<form class='navbar-form navbar-right' method='POST'>
  <div class="form-group">
    <div class='alert alert-success'>
      Logged in as <?php echo $_SESSION['fullname']; ?>! <?php echo $_SESSION['lastTimeLoggedIn'];?>
      <input type='submit' name='logoffuser' value='Log Off' class='btn btn-primary' />
    </div>
  </div>
</form>

The above is included when index is loaded, if $_Session is set and has array values. which will be displayed below... but first is the log out form(the one that shows when a user is logged out of the site...):

<form class="navbar-form navbar-right" method="post">
<?php if (isset($_POST["logIn"])){
  if (!empty($login_out_message)){
    echo "<p class='alert alert-danger'> $login_out_message </p>";
    $_POST = array();
  }else{
    set_Login();

  }
}else{

}?>
<div class="form-group">
  <label for="">Username:</label>
  <input type="text" name="Username" value="" class="form-control">
</div>
<div class="form-group">
  <label for="Password">Password:</label>
  <input type="password" name="Password" value="" class="form-control">
</div>
<button type="submit" name="logIn" class="btn btn-primary">Login</button>
<button type="submit" name="Register" class="btn btn-primary">Register</button>
</form>

and the _Nav.php form loading code which I am including so that you can see how things are being loaded... and I have tried to get the page to reload from here too...

<?php
//dubug purposes
//  var_dump($_SESSION);
//
  if (isset($_SESSION) && sizeof($_SESSION) > 0) {
    include("partial/_loggedin.php");
  } elseif (sizeof($_SESSION) == 0) {
    include("partial/_loggedout.php");
  }
?>

Like i mentioned before I have been all over the place trying code promised to make the index page reload, all I got was a crashed browser, and some bald spots...

Things I have tried so far:: I will identify where i did and what I did ina tree like structure....

index.php: ---- below the set_Logout() function:: ------include("index.php"); //resulted in infinate loop of index pages loading... ------include("_nav.php"); // resulted in simular to the above... ------include_once() of both index.php and _nav.php // result - an extra index and nav with in the current index, but not in an infinate loop. ------something having to do with header(location:index.php); which cause a browser crash, and locked me out of the page when i got it restarted... _nav.php: ------ same as above except that instead of the extra pages loading o the page they loaded with in the navbar... ------ the attempt of the location script caused an error and halted the page load... _loggedout.php and _loggedin.php: ------Tried the same here and momentarly only one thing sort of worked until i had to close my browser and reopen, was the header(location...) it worked and well but when i reloaded the page from a fresh browser load the page halted on load and gave an error about the page being loaded with headers(known issue with this call) ------the requests to include and include_once were the same as the _nav.php 's issues...

I think i had seen something about redirecting to the self of PHP some script, I didn't need it then but that hasn't been tried , i am still looking into it. and I would like to know if you guys can see something that i am missing.

Any help you might suggest would be greatly appreciated

Jesse Fender

  • 写回答

1条回答 默认 最新

  • dtcaw02086 2017-02-04 00:56
    关注

    I figured out what to do ... found it on another stackoverflow site... worke weel even though it fizzles a littlebit when you log in while its deciding what to do... it works and Im ok with that for now...

    SO php page reload using meta tags.

    //_loggedout.php (used to press log in button.)
    <?php if (isset($_POST["logIn"])){
      if (!empty($login_out_message)){
        echo "<p class='alert alert-danger'> $login_out_message </p>";
        $_POST = array();
      }else{
        set_Login();
        //tests
        $page = $_SERVER['PHP_SELF'];
        echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
      }
    }else{
    
    }?>
    
    //from index.php to check if $_SESSION is set... 
    <?php
            if (isset($_POST["logoffuser"])){
              set_Logout();
              $_POST = array();
              //tests
              $page = $_SERVER['PHP_SELF'];
              echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
              //include_once("index.php");
            }
          ?>
    

    The above is what i did to correct my issue... Thanks to the one guy who already posted... Im sorry to have taken up your time...

    Jesse Fender

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染