Solved

How to prevent form refresh/reload

Posted on 2015-02-17
27
115 Views
Last Modified: 2015-03-14
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
Comment
Question by:prashanth ag
  • 14
  • 6
  • 3
  • +2
27 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 40614317
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40614337
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
 

Author Comment

by:prashanth ag
ID: 40615931
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
 

Author Comment

by:prashanth ag
ID: 40615943
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
 

Author Comment

by:prashanth ag
ID: 40615945
Dear experts

I tried both solutions but problem still exists..
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40621646
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
 

Author Comment

by:prashanth ag
ID: 40632375
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
 
LVL 33

Expert Comment

by:Slick812
ID: 40633824
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
 

Author Comment

by:prashanth ag
ID: 40634768
clms_root path was valid
0
 

Author Comment

by:prashanth ag
ID: 40639361
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
 
LVL 29

Expert Comment

by:fibo
ID: 40639448
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
 
LVL 33

Expert Comment

by:Slick812
ID: 40639913
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
 

Author Comment

by:prashanth ag
ID: 40644195
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:prashanth ag
ID: 40644341
code works fine on localhost, but not on webhost server
0
 
LVL 29

Expert Comment

by:fibo
ID: 40644389
Is this happening also in other browsers?
Also on other computers?
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 167 total points
ID: 40644398
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
 

Author Comment

by:prashanth ag
ID: 40644490
i checked out all browser and different pcs  problem still not resolved
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40644495
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
 

Author Comment

by:prashanth ag
ID: 40644510
not yet
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 333 total points
ID: 40644779
@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
 
LVL 33

Accepted Solution

by:
Slick812 earned 333 total points
ID: 40645030
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
 

Author Comment

by:prashanth ag
ID: 40646729
i tried all the way  problem still not resolved
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40647340
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
 

Author Comment

by:prashanth ag
ID: 40650840
hi all,
is there any php free tool for debug php code
0
 

Assisted Solution

by:prashanth ag
prashanth ag earned 0 total points
ID: 40653349
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
 

Author Closing Comment

by:prashanth ag
ID: 40664881
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now