1

the header("location:") is not working online but it works on localhost. I have tried also to put the full URI in the header, still not working. Just jump over the header("location:") but it write out the echo. Why is it jumping over the header(location); but printing the echo"hellooooo", Can someone please help me? :)

<?php
  include('header.php');

  session_start();
    if(!isset($_SESSION["login_user"])) {
      header("location:login-form.php");
      echo "hellooooo";
      exit();
    }

?>


<?php
if (isset($_GET['page'])) {
   $p = $_GET['page'];


   switch ($p) {

    case 'job-application':
        include ('job-application.php');
        break;

    case 'company-application':
        include ('company-application.php');
        break;

    case 'edit-text':
        include ('edit-text.php');
        break;

    case 'school-time':
        include ('school-time.php');
        break;

    case 'care-time':
        include ('care-time.php');
        break;

    case 'cleaning-time':
        include ('cleaning-time.php');
        break;

    case 'economy-time':
        include ('economy-time.php');
        break;

    case 'restaurant-time':
        include ('restaurant-time.php');
        break;

    case 'construction-time':
        include ('construction-time.php');
        break;

    case 'retail-time':
        include ('retail-time.php');
        break;

    default:
        include ('job-application.php');
        break;
   }

   } else {

      include ('job-application.php');
}
?>

<?php include('footer.php'); ?>
  • 1
    Turn your error reporting up. – Sammitch May 28 '18 at 19:13
  • Headers usually prefer an absolute URL so include the http/https. Also your include 'header.php' might be considered output, and headers sent after output may not work. Try putting your login check about your header include. – Stephen May 28 '18 at 19:21
  • Like @Sammitch said, turn the error reporting on by entering this code at the top of PHP script: `error_reporting(0);` This will help you to figure out where it went wrong. – Anbuselvan Rocky May 28 '18 at 19:26
  • That `include('header.php');` I'm guessing will start writing some output to stdout. After you enable your `display_errors` settings you will most likely see the error message that I flag as duplicate. Follow the instruction of that post. – Federkun May 28 '18 at 19:32

1 Answers1

1

Try this:

<?php
session_start();
    if(!isset($_SESSION["login_user"])) {
      header("location: http/https://your_site/path_to/login-form.php");
      echo "hellooooo";
      exit();
    }
  include('header.php');

if (isset($_GET['page'])) {
   $p = $_GET['page'];


   switch ($p) {

case 'job-application':
    include ('job-application.php');
    break;

case 'company-application':
    include ('company-application.php');
    break;

case 'edit-text':
    include ('edit-text.php');
    break;

case 'school-time':
    include ('school-time.php');
    break;

case 'care-time':
    include ('care-time.php');
    break;

case 'cleaning-time':
    include ('cleaning-time.php');
    break;

case 'economy-time':
    include ('economy-time.php');
    break;

case 'restaurant-time':
    include ('restaurant-time.php');
    break;

case 'construction-time':
    include ('construction-time.php');
    break;

case 'retail-time':
    include ('retail-time.php');
    break;

default:
    include ('job-application.php');
    break;
   }

   } else {

  include ('job-application.php');
}
?>

<?php include('footer.php'); ?>

I wrote the notes in the comment. Location: prefers absolute URLS so add the http/https, and run your login check before your output anything to the browser. IE. include your header.php after the login check.

Stephen
  • 395
  • 1
  • 15