0
<?php
$username=$_POST["uname"];
$password=$_POST["pass"];
if($username=="shashwata"&&$password==12345){
header('location:home.php');            
}
else{
echo ("bye");   
}
?>

I am new in php, and this code is just for testing purpose..The main problem is the above code is running nicely inside localhost and gives the correct output but when i am trying to run this inside the hosting site it gives the warning.. i have checked there are no white space, no line space before and after

error_reporting( E_ERROR | E_WARNING | E_PARSE | E_NOTICE); 

even after that it works fine in localhost but all problem arises at the hosting site. please help me.. thanks in advance.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160

5 Answers5

1

Try this code

<?php
$username=$_POST["uname"];
$password=$_POST["pass"];
if($username=="shashwata"&&$password==12345){

  echo ("<SCRIPT LANGUAGE='JavaScript'>
       window.location.href='home.php';
    </SCRIPT>");             
  }
else{
echo ("bye");   
 }
 ?>
saru
  • 238
  • 1
  • 3
  • 14
  • That's gonna make the user wait slightly longer at best and requires the user to have javascript –  Feb 13 '15 at 07:34
  • thanks a lot this code is working fine.. but what's the problem with header() . why it 's not working. even i already made all possible changes which are mentioned here bt still getting the error. – Shashwata Mondal Feb 13 '15 at 09:44
0

Are these lines of code the first few lines in your file or are there any other lines above these ones?

You can only call a header() redirect BEFORE any output, print, echo or stand alone HTML.

Dan Smith
  • 29
  • 1
  • 7
  • no there are no other lines. i have checked there are no space and line space. and there are no output or print code before header() call but still i am getting the warning what i have to change? and i have tried error_reporting( E_ERROR | E_WARNING | E_PARSE | E_NOTICE); to show the error inside localhost but still is is fine the problem occurs only inside hosting site.. – Shashwata Mondal Feb 13 '15 at 06:53
  • I've uploaded this code to one of my hosting packages and it runs as I expect it to. The 2 URLs below are links to your code in a PHP file acting under both User auth=true and auth=false. AS you can see, it is running. http://geewizz.it/getP.php http://geewizz.it/getP.php?auth=1 can you post the full Header warning? – Dan Smith Feb 13 '15 at 07:02
  • Warning: Cannot modify header information - headers already sent by (output started at /usr/share/php/header.php:2) in /home/shash777/public_html/test.php on line 5 – Shashwata Mondal Feb 13 '15 at 07:06
  • Are you sure this is your whole file? your not including anything from anywhere or calling a header from a template file or something? What PHP version is your web hosting running – Dan Smith Feb 13 '15 at 07:12
  • yes this is all what my page shows ..web hosting running in php version 5.3.25. – Shashwata Mondal Feb 13 '15 at 10:02
0

try this code

        <?php
        $username = isset($_POST["uname"]) ? $_POST["uname"] : 0;
        $password = isset($_POST["pass"]) ? $_POST["pass"] : 0;
        if($username=="shashwata"&&$password==12345){
        header('location:home.php');            
        }
        else{
        echo ("bye");   
        }
        ?>
Pavan Jiwnani
  • 274
  • 1
  • 5
  • this code also gives the same warning inside the hosting site .. – Shashwata Mondal Feb 13 '15 at 07:22
  • The problem is that your hosting site is throwing some warning message apart from header warning due to that header command can't be processed. You can either try to disable the error reporting and code should work fine or you can post the screenshot of the exact warnings messages that you are getting – Pavan Jiwnani Feb 13 '15 at 07:29
  • i have already tried it (disabling error report) but nothing changed.. it shows the blank page rather than warning.. – Shashwata Mondal Feb 13 '15 at 10:07
0

Here's an easy way out:

<?php
ob_start();
//insert custom code here including headers
ob_end_flush();
?>

And make sure no characters at all precede the php tag (1st line). this includes spaces.

This method works because output is held in memory and not displayed until ob_end_flush call is reached.

0

I have a couple answers. The first code has a line that removes most errors, You'll only get Fatal errors, warnings, and parsing errors, not notices about undefined variables.

The second code is an easier rendition from what the poster above posted. Basically variables are supposed to be initialized or the compiler complains.

<?php
error_reporting(7); // only show the really important errors
ob_start();
$username=$_POST["uname"];
$password=$_POST["pass"];
if($username=="shashwata"&&$password==12345){
header('HTTP/1.1 301 Redirect',true);
header('location: home.php',true);
}else{
echo ("bye");
}
ob_end_flush();
?>

<?php
ob_start();
if isset($_POST["uname"]){$username=$_POST["uname"];}else{$username="";}
if isset($_POST["pass"]){$password=$_POST["pass"];}else{$password="";}
if($username=="shashwata"&&$password==12345){
header('HTTP/1.1 301 Redirect',true);
header('location: home.php',true);
}else{
echo ("bye");
}
ob_end_flush();
?>