Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

How to prevent form refresh/reload

Redirect the user to a different page after submission takes the original submission destination page out of the browser history so the user cannot refresh it or reload to it.

consider an example
 Admin / Manager page
Suppose particalur manager login and view his employee details
manager 1      emp1 emp2
manager 2     emp3 emp4


code:
<?php
                    if($_SESSION['type'] == "Admin"){
                              ?>
                              <li><a href="<?php echo CLMS_ROOT.'/admin/index.php?load=view'; ?> " >Admin Panel</a></li>
                        <?php
                              }
                              else if($_SESSION['type'] == "Manager"){
                              ?>
                              <li><a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view' ; ?> " >Manager Panel</a></li>
                                <?php
                    }
                  ?>



  <li><a href="<?php echo CLMS_ROOT.'/panel/index.php?load=view'; ?>">Employee Panel</a></li>

my problem is  after manager 1  login  & logout then manager 2 login  he get  manager 1 employee details
how to achieve without refresh or reload  manger 2 employee details
0
prashanth ag
Asked:
prashanth ag
  • 14
  • 6
  • 3
  • +2
4 Solutions
 
KimputerCommented:
Your logout code is not complete, use session_unset()

Or to be totally paranoid, use this:

<?php
    session_start();
    session_unset();
    session_destroy();
    session_write_close();
    setcookie(session_name(),'',0,'/');
    session_regenerate_id(true);
?>

Optional, use this code in the login page (meaning if the user forgot to log out, and just close the tab, the new user will go to the log in page, and still start as new)
0
 
Ray PaseurCommented:
The correct way to effect a logout is shown in this article.  See the part about "the Logout Page" about half way down.  You may want to read the entire article - it seems to cover the topic you're working on now.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

There is a similar "gotcha" that always bothers developers, but is never a problem in real life.  See the part about "the fine print" in this article.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
0
 
prashanth agAuthor Commented:
please find the  Logout code

static public function logout()
{  
 session_start();
    //$_SESSION = array();
 


 unset($_SESSION['username']);
 unset($_SESSION['emp_code']);
 unset($_SESSION['full_name']);
 unset($_SESSION['type']);
 //session_unset();
 session_destroy();
 
 header('Location:'.CLMS_ROOT);
 //header('Location: http://test.com/');
 exit(0);
 
 ?>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
prashanth agAuthor Commented:
Html code
<head>
<link href="../css/bootstrap.min.css" rel="stylesheet">
   </head>
<body>
<div class="navbar-collapse collapse welcome">
               <ul class="nav navbar-nav navbar-right">
             <li><a href="<?php echo CLMS_ROOT.'/logout.php'; ?>">Logout</a></li>
         </ul>
            </div>
</body>
0
 
prashanth agAuthor Commented:
Dear experts

I tried both solutions but problem still exists..
0
 
Slick812Commented:
greetings prashanth ag , , it is my opinion that the PHP code you gave as -
<?php
if($_SESSION['type'] == "Admin"){
?>
   <li><a href="<?php echo CLMS_ROOT.'/admin/index.php?load=view'; ?> " >Admin Panel</a></li>
<?php
   } else if($_SESSION['type'] == "Manager"){
?>
   <li><a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view' ; ?> " >Manager Panel</a></li>
<?php
}
?>
<li><a href="<?php echo CLMS_ROOT.'/panel/index.php?load=view'; ?>">Employee Panel</a></li>

Open in new window


is NOT your problem, as no matter Which "Manager" may be signed in (using the Session array), it always produces the same exact Link URL as -
    <a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view' ; ?> " >Manager Panel</a>

so it could be Manager 1, Manager 2, or Manager 3, they ALL get the same web address in that Link.

I have no Idea about your session use in the manager panel page at -
    <a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view' ; ?> " >Manager Panel</a>
but could that be your problem? ?   OR maybe you need to have a manager ID in the web address?

<a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view&id='.$_SESSION['id'] ; ?> " >Manager Panel</a>
0
 
prashanth agAuthor Commented:
I updated  following code but problem still exists

<li><a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view&manager_code='.$_SESSION['Manager_code'].' ' ; ?> " >Manager Panel</a></li>
0
 
Slick812Commented:
you will probably need to check you File Access paths, such as -
   CLMS_ROOT.'/Manager/index.php
is this  CLMS_ROOT  path part valid on the server you use ?
0
 
prashanth agAuthor Commented:
clms_root path was valid
0
 
prashanth agAuthor Commented:
plz find User login authentication("check_permission.php") code and i added every page  

 
require_once 'check_permission.php';


<?php

   if(!isset($_SESSION['username']) && !isset($_SESSION['type']) && !isset($_SESSION['Password']) )
   {        
         header('Location: '.CLMS_ROOT);
         exit;
   }
  if(!(($_SESSION['type'] == 'Admin') || ($_SESSION['type'] == 'Manager') || ($_SESSION['type'] == 'Employee'))){
      header('Location: '.CLMS_ROOT);
         exit;
   }

?>

is there any thing  i am missing in code  ?
0
 
fiboCommented:
Not sure how exactly you reach the page.
Is it by a direct link typed in the browser, or as a redirect from some other place?

The code you give is the code that will be used by any user logged in as an admin... but in this page the login of the admin appears nowhere.... so everybody will probably get the same result.

You need to give us more code:
- either more of the code of the same php program
- OR the code of the page which links to this one
0
 
Slick812Commented:
you show us this code -
<?php
   if(!isset($_SESSION['username']) && !isset($_SESSION['type']) && !isset($_SESSION['Password']) )
   {        
         header('Location: '.CLMS_ROOT);
         exit;
   }
  if(!(($_SESSION['type'] == 'Admin') || ($_SESSION['type'] == 'Manager') || ($_SESSION['type'] == 'Employee'))){
      header('Location: '.CLMS_ROOT);
         exit;
   }
?>

Open in new window


I do not see that it is necessary to check for all of these  $_SESSION values on every page, to insure That someone has properly signed into your sight?
But as fibo  has said already, I would need to see more code work, AND have some Idea what you are trying to do, your goal in this code work.

You say in your question, problem -
    "my problem is  after manager 1  login  & logout then manager 2 login  he get  manager 1 employee details
how to achieve without refresh or reload  manger 2 employee details "
so I guess that you now are trying to set   $_SESSION values  so when you sign in as  "manger 2", , to get to see the employees for "manger 2" and NOT the employees for "manger 1" ?
0
 
prashanth agAuthor Commented:
my problem is

Browser just loads the Page that was in BEFORE and it does NOT updating

how to achieve without refresh or reload (F5)
0
 
prashanth agAuthor Commented:
code works fine on localhost, but not on webhost server
0
 
fiboCommented:
Is this happening also in other browsers?
Also on other computers?
0
 
KimputerCommented:
If possible, you should check on that PC, if the page (the one you don't expect, the older one) is being sent from the server, or it's being loaded from the cache.
If it's loaded from the cache (i.e. page code is sent only once), then try this in your pages, so it won't cache anything:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Open in new window

0
 
prashanth agAuthor Commented:
i checked out all browser and different pcs  problem still not resolved
0
 
KimputerCommented:
So what you are saying is, it's loading that page from the server, not from cache? Or you didn't implement the solution yet?
0
 
prashanth agAuthor Commented:
not yet
0
 
Slick812Commented:
@prashanth ag, , ,  You must have code pages that are working correctly, because you say -
   "code works fine on localhost, but not on webhost server"

And you say that with the "webhost server" page, it loads incorrectly at first, but you can get the correct employee list, if you press F5, which DOES NOTHING to change the page code, but it does force the browser to get a NEW transfer of a page (not use the cache). .

you say - "i checked out all browser and different pcs", so if it is NOT your Page code that is the problem, since it works on a page reload, and on localhost, and you say that you have tried the several header additions like -
     header("Expires
     header("Cache-Control:

Then a logical conclusion is that it is some Apache (maybe PHP)  "webhost server" Setting, that may cause it to use old page loads, , I have not ever had this problem, but I have heard that some Server "Memory Caching" add ons can be set to send a page from the server "memory cache" instead of running the server code again, suppose to be faster that way? ? But I do NOT KNOW if that is your problem, or how to fix that sort of thing?

I have done AJAX years ago, when IE would stupidly cache ajax request pages, so you had to add a changing GET parameter to the address, so each and every call would be an Un-Cached page addy.

first page request -
      www.web.com/ab.php?nc=1

second page request -
      www.web.com/ab.php?nc=2

where you just increase the nc parameter by one for every request, and the page will never show a cached page.

You can store an Integer in the $_SESSION like maybe
      $_SESSION["nc"] = 0;

then on the page that have your EMPLOYEE page link write something like -

<a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view&nc='.$_SESSION['nc']++ ; ?> " >Manager Panel</a>
0
 
Slick812Commented:
I remembered that you do a Change of users and so my $_SESSION["nc"] will not work for this, since the session changes, you will have to try a random for the parameter -

<a href="<?php echo CLMS_ROOT.'/Manager/index.php?load=view&nc='.mt_rand() ; ?> " >Manager Panel</a>
0
 
prashanth agAuthor Commented:
i tried all the way  problem still not resolved
0
 
Slick812Commented:
you say - "works fine on localhost", , Can you determine what In the set-up or http request handling , is the difference between your  localhost and your server.
I am only saying this as I have NO OTHER ideas or thinking of what, could be your problem?

When I have problems I can not understand, or see to fix, it is necessary for me to do another development cycle , I start with "Test" pages that only have the very minimum Basic code (server and browser) to get a correct functioning for that one employee stats, you can't fix, then add the elements like, more PHP and HTML to see what causes the problem. But since you have it working on localhost, it may be something in the SERVER computer, and NOT in the PHP or HTML code?
0
 
prashanth agAuthor Commented:
hi all,
is there any php free tool for debug php code
0
 
prashanth agAuthor Commented:
Hopefully it will help prevent Cache, if any!

1. All used headers have first letters uppercase, so you MUST follow this. For example:

Location, not location
Content-Type, not content-type, nor CONTENT-TYPE

2. Then there MUST be colon and space, like

good: header("Content-Type: text/plain");
wrong: header("Content-Type:text/plain");

3. Location header MUST be absolute uri with scheme, domain, port, path, etc.

good: header("Location: http://www.example.com/something.php?a=1");

4. Relative URIs are NOT allowed

wrong:  Location: /something.php?a=1
wrong:  Location: ?a=1
0
 
prashanth agAuthor Commented:
elseif (empty($ajax) ) {

  header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
  header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
  header( "Cache-Control: no-cache, must-revalidate" );
  header( "Pragma: no-cache" );

  ?>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 14
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now