10

I am working on a project and I am required to run my program on someone else's webserver. It is a pretty simple login page that I am having the problem with. The program works correctly if I run it through my local host via WAMP. The problem I am having is that the re-direct portion is not working correctly it validates the user and starts a session but when it gets to the redirect nothing happens.

I am either doing something wrong with my syntax which I think is highly unlikely since it does work correctly through my local host. Or alternatively I'm thinking that the server does not have that function (not sure if its possible to pick and choose which modules your server supports although I'm sure it's feasible).

I don't know if it matters but they are using "cpanel" which is where I can access the files and there all in the same directory so if someone could tell me where I am going wrong or suggest an alternative to redirecting via "header" any help would be greatly appreciated. I've looked around but it seems that "header" is the standard work horse.

Heres the code I have:

if( (!empty($_POST['username'])) && (!empty($_POST['password'])) )
{
// username and password sent from Form 
$myusername = $_POST['username']; 
$mypassword = $_POST['password']; 


$sql="SELECT UserName FROM User WHERE UserName='$myusername' and        Password='$mypassword'";

$result=mysql_query($sql);

$row=mysql_fetch_array($result);
//$active=$row['active'];
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
    echo "we made if to the if";
    session_start();
    session_register("myusername");
    $_SESSION['login_user']=$myusername;
    echo "right b4 the re-direct";
    header("location: UI.php"); 
            exit;
}
else
    echo "Your user name/password was not correct pleast TRY AGAIN!!!";

} 

Update: In response to the statements about the echos would the problem possible by that I am processing my form in the same file and using echo $_SERVER['PHP_SELF']

cpowel2
  • 605
  • 1
  • 11
  • 20
  • 1
    Do you have error reporting enabled? Are you 100% sure the condition is met and the header sent? – Pekka Dec 31 '11 at 17:23
  • You can't have the page output anything before the header call. Your debugging statements are preventing the redirection from working. – JJJ Dec 31 '11 at 17:25
  • Yeah I tried removing them but it still doesn't redirect properly – cpowel2 Dec 31 '11 at 17:29
  • See the `` alternatives in [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php). – mario Dec 31 '11 at 17:32

8 Answers8

44

I use this function for redirect...

Which works in all situations..even if headers are already sent..or even javascript is disabled..

function redirect($url)
{
    if (!headers_sent())
    {    
        header('Location: '.$url);
        exit;
        }
    else
        {  
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>'; exit;
    }
}
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
  • Why won't you just use the meta refresh tag? It is way fewer bytes that way. – Sainan Oct 24 '16 at 06:04
  • The back button on the browser doesn't work! After the redirect, the browser skips over the previous page and goes back to what amounts to two pages... – Damian Green Jul 16 '17 at 21:58
4

By using the below code we redirect the page

$page = $_SERVER['REQUEST_URI'];
echo '<script type="text/javascript">';
echo 'window.location.href="'.$page.'";';
echo '</script>';
2
echo "<script>window.location.href='yourPage.php'</script>";
2

From the docs:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Your echo is causing the redirect to fail, as is any other output sent before the header.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Redirecting via headers is illegal if any content has come through. Take out all the echos and it should work. If you do want there to be a message shown to the user before the redirect, you'll likely have to do it with JavaScript via location.href.

ranksrejoined
  • 1,229
  • 9
  • 9
1

You cannot set headers after outputting some text, so either you move the header call before the echo statements (fine in your case) or you use output buffering.

nico
  • 50,859
  • 17
  • 87
  • 112
0

You probably have already sent content to the browser before the php header is sent. This can be just a \n after a closing php tag ( ?> )

To find the place where unwanted output is generated canbe hard on bigger projects or long lines of classes extending each other. To find the problem you can do this:

 error_reporting(E_ALL); 
 ini_set("display_errors", 1);
 header("Location: https://mydomain.com/myLoginAdress/");
 die();

Strict error reporting will throw line and file of the output.

Max
  • 2,561
  • 1
  • 24
  • 29
0
Use This Code 

Syntax :

  echo '<script type="text/javascript"> window.location="<Your_URL>";</script>';


write this line insted of header("location: UI.php");


   echo '<script type="text/javascript"> window.location="UI.php";</script>';
  • 3
    a good answer includes also some explanation of the code you've written... Can you please improve you answer by adding some clarification? – DaFois Jul 27 '18 at 12:27