-1

Hello i make a project and i have the fallowing problem. In my index page i have a dynamic layout:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="index2.css">
</head>
<body class="news">
  <header>
    <div class="nav">
      <ul>
        <li class="home"><a href="index2.php?page=welcome">Home</a></li>
        <li class="logare"><a class="active" a href="index2.php?page=admin_login">Admin</a></li>
        <li class="registration"><a href="index2.php?page=registration">Registration</a></li>
        <li class="produse"><a href="index2.php?page=produse">Produse</a></li>
      </ul>
    </div>
  </header>

  <div id="content">
<?php
 $p=$_GET['page'];

 switch($p){
        case "welcome":
          include('welcome.php');
          break;
        case "admin_login":
          include('Admin_login.php');
          break;
        case "registration":
          include('Registration.php');
          break; 
        case "produse":
          include('produse.php');
          break; 
        default:
           include('welcome.php');
          break;
 }?>
 </div>


</body>
</html>

in default and welcome case it;s redirecting me to my welcome.php page:

<?php
    session_start();

 if(!$_SESSION['email'])
    {

        header("Location: login.php");//redirect to login page to secure the welcome page without login access.
    }

    ?>

<html>
<head>

<title>
Registration
</title>
</head>

<body>
<h1>Welcome</h1><br>
<?php
    echo $_SESSION['email'];
    ?>


<h1><a href="logout.php">Logout here</a> </h1>


</body>

</html>  

Because i have a redirecting in welcome.php too ,my header is not loading from the dynamic layout.How can i repair this,but still redirecting me to login?

Chirica Andrei
  • 77
  • 1
  • 11
  • I think you could use `output buffering` to achieve this – Professor Abronsius Jun 05 '17 at 14:52
  • what does http://php.net/manual/en/function.error-reporting.php throw back; headers sent? you should also add an exit to header. – Funk Forty Niner Jun 05 '17 at 14:52
  • You'll need to re-arrange your logic; you're attempting to set a header (in welcome.php) _after_ you've already sent content to the page (index.php). Besides, you're also defining two `` tags, two `` tags, etc., so it's invalid HTML. Create a navigation file and include that file in your welcome.php and other pages. – Chris Forrence Jun 05 '17 at 14:52
  • `Welcome.php` has a `session_start()` but because the main script has already output to the browser, it cannot work at this point. Simplest solution would be to put the `session_start()` ONCE in the main script and remove it from ALL the included scripts. – RiggsFolly Jun 05 '17 at 14:54
  • I have some requirments .dynamic pages and login redirecting are both required.So what is the solution.What modifications should i do? – Chirica Andrei Jun 05 '17 at 14:57
  • [You didn't answer this comment](https://stackoverflow.com/questions/44371611/php-header-conflict#comment75744486_44371611). Or are you afraid your question will get closed with a duplicate for it? – Funk Forty Niner Jun 05 '17 at 15:03
  • i don't understand the question man – Chirica Andrei Jun 05 '17 at 15:04

1 Answers1

0

To avoid PHP header conflicts, I prefer to use JS redirection instead. See the below code that I have modified for you.

<?php
    session_start();

    if(!$_SESSION['email']) {
        ?>
            <script language="javascript" type="text/javascript">
                alert("User not logged in.");
                window.location = 'login.php';
            </script>
        <?php
    }      
?>

Hope this helps.