0

I’ve hit a snag with the code below. I have no idea why the second one with header/location keeps giving me a blank page.

index.php and user.php and login.php are all in the main folder.

The following outputs false:

<?php 

    include('functions/check.php');


    if (test())
        {
            echo "True";
        }
    else 
        {
            echo "false";
        }

    ?>

But when I try this, I get a blank page:

<?php 

    include('functions/check.php');


    if (test())
        {
            header("Location: user.php");
        }
    else 
        {
            header("Location: login.php");
        }

    ?>

I tried all types of different variations:

header("Location: /login.php");
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
John_Nil
  • 165
  • 2
  • 4
  • 17
  • A blank page usually means that error reporting is turned off. Enable it and update your question with the error reported. As for `Warning: Cannot modify header information - headers already sent by` see http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – user555 Feb 11 '14 at 00:54

4 Answers4

3

Found a very useful fix for that :

// at document start

 <?php ob_start(); ?>

// at document end

<?php ob_end_flush(); ?>
John_Nil
  • 165
  • 2
  • 4
  • 17
0

Use the full URL instead of the relative URL.

http://us3.php.net/manual/en/function.header.php

Sirago
  • 101
  • 3
  • @John_Nil Also be sure you aren't outputting ANY data before you send the headers. You don't have ANYTHING displaying on the page? View source and check that. The headers need to be the first thing that's sent. – Sirago Feb 11 '14 at 00:07
  • I allowed all errors to be displayed and i got this : Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosoraweb101/b5/ipg.gfgdgfgcom/folder/functions/connect.php:29) in /hermes/bosoraweb101/b5/ipg.gfgdgfgcom/folder/index.php on line 14 – John_Nil Feb 11 '14 at 00:11
  • line 14 in connect.php is just this "?>" , the end tag for the php file – John_Nil Feb 11 '14 at 00:12
  • I'd look for a rogue echo somewhere in all the files involved in making that page. Looks like connect.php or check.php may be the culprit. Basically, somewhere you're outputting data. I can help if you want to post the files. – Sirago Feb 11 '14 at 00:14
  • the check.php is returning true/false , could that be the problem ? – John_Nil Feb 11 '14 at 00:18
  • You can try "if (test() == TRUE)" – Sirago Feb 11 '14 at 00:22
0

You have to make sure that not any text has been printed before call a header("Location: xxx");

Check for the code that you're including for an echo or print_r.

dnetix
  • 320
  • 3
  • 10
0

There's some kind of output to the page before sending the new header. Make sure there's absolutely nothing being output to the page before you use the command. It might even be a strange symbol, for example Notepad adds a strange symbol to the beginning of the file, it is not visible, but it's there, so it gets printed out to the page as HTML.

Janis Vepris
  • 587
  • 3
  • 13