Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7

Thread: PHP redirect

  1. #1
    Junior Member
    Join Date
    Aug 2006
    Posts
    11
    Member #
    13768
    Hello,

    I am fairly new to PHP and having some trouble redirecting a page once a user has been authenticated via a mySQL DB query.

    I have the following code at the very top of the authentication page:

    Code:
    <?php
    $username="trandru_admin";
    $pass="password";
    $hostname="mysql4.opentransfer.com";
    @mysql_connect($hostname, $username, $pass) or die("Cannot connect to DB!");
    @mysql_select_db("trandru_main") or die("Cannot select DB!");
    $sql="SELECT loginid FROM login WHERE loginid='.$loginid.' and pass='.$password.'";
    $r=mysql_query($sql);
    if(!$r) {
    $err=mysql_error();
    print $err;
    exit();
    }
    if(mysql_affected_rows()>0){    
        header( 'Location: http://www.rossandrus.com/' );    
    }
    ?>
    If login is unsuccessful, there is a script within the page to display:

    Code:
    <?php
                if(mysql_affected_rows()==0){
                    print "No such login in the system. Please try again.";
                }             
    ?>
    I get the following error message when I try to login successfully:

    Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/trandrus/esuns-class.studioorpheus.com/includes/super_global.php:4) in /hsphere/local/home/trandrus/esuns-class.studioorpheus.com/includes/authenticate.php on line 17

    Why do I get this when the code is at the very top of the page? Anyways, it looks like this is just confirming what I want to happen. But no redirect, just an error message. Any help would be appreciated.

    Thanks!

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    You can never echo anything before you send an HTTP header, because headers always come before text that is actually rendered in the browser. Remember, if you have whitespace before or after your open/close PHP tags, that will do it also.

    A safe technique is to immediately exit after sending a header. It's not clean, but it works.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Junior Member
    Join Date
    Aug 2006
    Posts
    11
    Member #
    13768
    I removed all whitespace (there was none) and tried exiting immediately after writing the header. I get the same error message only this time the page wont load (because the authentication code is required).

    It seems as if there is something else after this code that is trying to modify header info bc the error message points back to this code. But if the page doesn't load, that means the parser is exiting before the authentication code is processed since what follows is the document.

    Do I have to overwrite or disable some default header declaration? Or could this be a compatibility problem regarding the PHP version or document type declaration?

    Any input would be appreciated. Thanks.

  5. #4
    Junior Member _shady's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    20
    Member #
    14934
    You should probably switch to javascript to redirect the browser. Filburt1 is right though, that message is recieved if something is echo'd prior to using a header() statement.

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Javascript is rarely a good way of redirecting as there's such a huge amount of overhead compared to just using the very protocol (HTTP) the document is send over in the first place.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Junior Member
    Join Date
    Aug 2006
    Posts
    11
    Member #
    13768
    I ended up just using a meta refresh. What are the pros and cons of this method as opposed to php or java script?

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    In order of preference and cleanliness:
    1. Avoid the redirect entirely
    2. Redirect via HTTP (PHP's [phpfunction]header[/phpfunction])
    3. <meta />
    4. Javascript
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:00 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com