Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to "header" for re-directs in PHP

Tags:

redirect

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

like image 965
cpowel2 Avatar asked Dec 31 '11 17:12

cpowel2


1 Answers

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.

like image 95
ranksrejoined Avatar answered Nov 15 '22 13:11

ranksrejoined