Skip to content
Advertisement

Alternative to “header” for re-directs in PHP

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’]

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement