0

i have a serious problem over here. I created a user system with sessions. The problem is that there is content you only see when you are logged in. For example in the navigation bar the sign in button is replaced with a account button.

Now to my problem: Every page php-includes the navbar.php. For example in the index.php is written:

<body>
<?php include("navbar.php")?>
</body>

The login.php redirects to the index.php:

header("Location: index.php");

But the index.php does not refresh. After a hard refresh with "F5" every thing is fine.

I also tried meta tags to prevent loading the page in the cache.

Any Ideas?

index.php:

<html lang="en">
<head>
    ...
</head>
<body >
<?php include("navbar.php")?>
    <div id="wrap">
    ...
    </div>
</body> 
</html>

navbar.php

<div class="navbar">
    <?php
    session_start();
    if (!isset($_SESSION['logged']) || !$_SESSION['logged']) 
    {?>
      ...Sign in etc...
    <?php
    }
    else
    {?>
        ...Accounting...
    <?php
    }?>
</div>

login.php:

<html>
<head>
</head>
<body>
<?php include("navbar.php"); ?>
<div class="container">
  <form class="form-signin" action="logon.php" method="post" >
        ...
    <button type="submit">Login</button>
  </form>
</div>
</body>
</html>

logon.php:

<?php
if login successfull //pseudo code
    header("Location: http://www.***.com/index.php");
}
else 
{
    header("Location: http://www.google.de");
}
exit;
?>
Swagger
  • 1
  • 1
  • 2
  • what is the cache limit on your php files ? talking about .htaccess – Jay Harris Jun 27 '13 at 14:14
  • since its a dynamic file, generally practice is to force no caching – Jay Harris Jun 27 '13 at 14:14
  • Where are you including the login.php file? are you `echo`-ing anything prior to calling `header`? is there HTML in the file before you're setting the headers? if so: move the `header` to the top, or use `ob_start()` – Elias Van Ootegem Jun 27 '13 at 14:24
  • the cache limit is set to nocache... – Swagger Jun 27 '13 at 14:24
  • @Swagger: all your files start with an `` tag, so the headers are being sent _before_ you call `header`. Adress that issue by output buffering, or making sure the headers are set _before_ any output is being generated. – Elias Van Ootegem Jun 27 '13 at 19:22

2 Answers2

2

add this to your .htaccess file. this will disable browser caching on these file extensions

 <FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
 Header unset Cache-Control
 </FilesMatch>
Jay Harris
  • 4,201
  • 17
  • 21
0

According to the specs, you have to pass an absolute uri.
It's also best to add this, when redirecting:

header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.google.com');

For SEO purpouses, and browser cache (google this to find out more).
I hope you know this already, but you'll also have to make sure no output has been sent to the client, because in that case, the headers have already been sent, and logic dictates that it's too late to change them, then.

Check this question for more details on how to deal with headers and output buffering.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Changed the login.php to: header("Location: http://www.***.com/index.php"); still not refreshing the index.php, after pressing F5 every thing is fine and the login button is replaced with account – Swagger Jun 27 '13 at 14:19
  • @Swagger: and the result was...? Also: the snippet you posted I get the impression you've already sent the headers (implicitly by sending output). I've added a link to an older question that explains how to buffer output or how to ensure you can set the headers if needs must – Elias Van Ootegem Jun 27 '13 at 14:21