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

我需要帮助在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

I figured out what to do ... found it on another * 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