SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    login form question

    I made a test login form in php, but it has a problem:
    When i enter the correct username and pass, i get the following message:

    Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\home.php:10) in c:\program files\apache group\apache\htdocs\home.php on line 32

    Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\home.php:10) in c:\program files\apache group\apache\htdocs\home.php on line 33

    Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\home.php:10) in c:\program files\apache group\apache\htdocs\home.php on line 34






    (i am hosting the script locally on an apache server)

    Here is the script:

    <?php
    // valid login credentials
    $username = 'sean';
    $password = 'password';
    // grab current time
    $time=time();

    // handle the logout event
    if ($logout == true) {
    setcookie ("user", md5($_POST[user]), $time-3200);
    setcookie ("pass", md5($_POST[pass]), $time-3200);
    header("Location: home.php");
    }

    // handle validation event
    if ($_POST[user] && $_POST[pass]) {
    if ($_POST[user]==$username && $_POST[pass]==$password) {
    setcookie ("user", md5($_POST[user]), $time+3200);
    setcookie ("pass", md5($_POST[pass]), $time+3200);
    header("Location: home.php");
    } else { $login_error= true; }
    }

    // handle login event, both successful and erroneous, or show login screen
    if ($login_error == true) { ?>
    <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
    <tr><td align=center bgcolor=#123dd4>LOGIN ERROR</td></tr>
    <tr><td align=center><b>Invalid Username and/or Password</b><br><br><a href=home.php>Back</a></td></tr>
    </table>
    <?
    } elseif ($_COOKIE[user] == md5($username) && $_COOKIE[pass] == md5($password)) { ?>
    <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
    <tr><td align=center bgcolor=#123dd4>SECURE AREA</td></tr>
    <tr><td align=right><a href=home.php?logout=true>Logout</a></td></tr>
    <tr><td>You have successfully logged in.<br><br>
    Encrypted Username: <b><?= $_COOKIE[user] ?></b><br>
    Encrypted Password: <b><?= $_COOKIE[pass] ?></b><br>
    </td></tr>
    </table>
    <?
    } else {
    ?>
    <form action=home.php method=post>
    <table align=center class="table1">
    <tr><td colspan=2 align=center bgcolor=#123dd4>LOGIN</td></tr>
    <tr><td align=right>Username: </td><td><input type=text name=user size=15></td></tr>
    <tr><td align=right>Password: </td><td><input type=password name=pass size=15></td></tr>
    <tr><td align=center colspan=2><input type=submit value=Login></td></tr>
    </table>
    </form>
    <?
    }

    someone please help

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ensure that you havent sent any html or white space to the browser before you redirect.

  3. #3
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im not sure what u mean

  4. #4
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok n/m i got the login form to work but now when i try to logout it just refreshes the page and keeps me logged in
    wuts up with that¿

  5. #5
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    help me

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a few different ways to do this but try adding the following to the logout action.

    $_COOKIE['user']='';
    $_COOKIE['pass']='';

  7. #7
    SitePoint Zealot amrknt's Avatar
    Join Date
    Nov 2003
    Location
    india
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is b'coz of line
    PHP Code:
    header("Location: home.php"); 
    I'm also facing these type of problems on my WNIDOWS based machine. However when I run my codes from web server hosted on LINUX, all these codes run smoothly without having any problem.

    What's the reason behind it I don't know, but willing to know if anyone tell me.

    Thanks

  8. #8
    SitePoint Member
    Join Date
    Mar 2004
    Location
    Belgium
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very simple - you need to add an 'exit' statement after each header statement.

  9. #9
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im very new to php and im not sure what u mean by an exit statement- could you chow me an example?

  10. #10
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exit Statement..

    This should do it. You could also use an output buffer...


    PHP Code:
    // handle validation event 
    if ($_POST[user] && $_POST[pass]) { 
    if (
    $_POST[user]==$username && $_POST[pass]==$password) { 
    setcookie ("user"md5($_POST[user]), $time+3200); 
    setcookie ("pass"md5($_POST[pass]), $time+3200); 
    header("Location: home.php"); 
    exit();
    } else { 
    $login_errortrue; } 

    http://us2.php.net/manual/en/function.exit.php
    mcahill
    Reel-Time.com - Saltwater Fly Fishing
    The Vario Blog
    VarioCreative.com 1 2 3 4 5 6 7

  11. #11
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now i get this error message when i try to login:
    Parse error: parse error, unexpected T_STRING in c:\program files\apache group\apache\htdocs\login\index.php on line 20

  12. #12
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please post the first 25 lines or so of the index.php page

  13. #13
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the entire inde.php code:
    Code:
    <?php
    // valid login credentials 
    $username = 'sean'; 
    $password = 'password'; 
    // grab current time 
    $time=time(); 
    
    // handle the logout event 
    if ($logout == true) { 
    setcookie ("user", md5($_POST[user]), $time-3200); 
    setcookie ("pass", md5($_POST[pass]), $time-3200); 
    header("Location: index.php"); 
    } 
    
    // handle validation event 
    if ($_POST[user] && $_POST[pass]) { 
    if ($_POST[user]==$username && $_POST[pass]==$password) { 
    setcookie ("user", md5($_POST[user]), $time+3200); 
    setcookie ("pass", md5($_POST[pass]), $time+3200); x
    header("Location: index.php"); 
    exit();
    } else { $login_error= true; } 
    } 
    
    // handle login event, both successful and erroneous, or show login screen 
    if ($login_error == true) { ?> 
    <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;"> 
    <tr><td align=center bgcolor=#123dd4>LOGIN ERROR</td></tr> 
    <tr><td align=center><b>Invalid Username and/or Password</b><br><br><a href=index.php>Back</a></td></tr> 
    </table> 
    <? 
    } elseif ($_COOKIE[user] == md5($username) && $_COOKIE[pass] == md5($password)) { ?> 
    <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;"> 
    <tr><td align=center bgcolor=#123dd4>SECURE AREA</td></tr> 
    <tr><td align=right><a href=index.php?logout=true>Logout</a></td></tr> 
    <tr><td>You have successfully logged in.<br><br> 
    Encrypted Username: <b><?= $_COOKIE[user] ?></b><br> 
    Encrypted Password: <b><?= $_COOKIE[pass] ?></b><br> 
    </td></tr> 
    </table> 
    <? 
    } else { 
    ?> 
    <form action=index.php method=post> 
    <table align=center class="table1"> 
    <tr><td colspan=2 align=center bgcolor=#123dd4>LOGIN</td></tr> 
    <tr><td align=right>Username: </td><td><input type=text name=user size=15></td></tr> 
    <tr><td align=right>Password: </td><td><input type=password name=pass size=15></td></tr> 
    <tr><td align=center colspan=2><input type=submit value=Login></td></tr> 
    </table> 
    </form> 
    <? 
    }
    ?>

  14. #14
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whats with the trailing x in the following line

    setcookie ("pass", md5($_POST[pass]), $time+3200); x

    remove that x

  15. #15
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the tip i know longer get the error message but now ive run into the same problem i had before - once i login, i try to puch logout and it doesnt do anything
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

    -Albert Einstein

  16. #16
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    help?¿
    anyone?¿
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

    -Albert Einstein

  17. #17
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure if it will help but change the logout function to this...

    PHP Code:
    // handle the logout event 
    if ($_GET['logout'] == true) { 
    setcookie ("user"""$time-3200); 
    setcookie ("pass"""$time-3200); 
    header("Location: index.php"); exit();


  18. #18
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no man that didnt help but thanx neway
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

    -Albert Einstein

  19. #19
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    alrite i finally got but thanx for your post that really put me in the right direction
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

    -Albert Einstein

  20. #20
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Care to post what the problem was... for others who might be following this thread?

  21. #21
    SitePoint Enthusiast luckyirish's Avatar
    Join Date
    Apr 2004
    Location
    New Jersey
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well to tell you the truth i dont know all i did was move the file to a new location and it worked??? but i think your code was completely right
    thanks again man
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

    -Albert Einstein

  22. #22
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alrighty Thanks... glad to hear all is well!

  23. #23
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting the same header(); error you were getting. You dont know how you fixed it? I'm stuck and staring at this little script with my eyes buggin out trying to figure out whats wrong with it... Any help?

    PHP Code:
    <? 
    /* Check User Script */ 
    session_start();  // Start Session 

    include ('./connect/mysql_connect.php');
    include (
    './header.inc');

    // Conver to simple variables 
    $username $_POST['username']; 
    $password $_POST['password']; 

    if((!
    $username) || (!$password)){ 
        echo 
    "Please enter ALL of the information. <br />"
        include 
    'login_form.html'
        include (
    './footer.inc');
        exit(); 

    else{

    // Convert password to md5 hash 


    // check if the user info validates the db 
    $sql mysql_query("SELECT * FROM players where username='$username' AND password='$password'") or die(mysql_error());  
    $login_check mysql_num_rows($sql); 

    if(
    $login_check 0){ 
        while(
    $row mysql_fetch_array($sql)){ 
        foreach( 
    $row AS $key => $val ){ 
            $
    $key stripslashes$val ); 
        } 
            
    // Register some session variables! 
            
    session_register('firstName'); 
            
    $_SESSION['firstName'] = $firstName
            
    session_register('lastName'); 
            
    $_SESSION['lastName'] = $lastName
            
    session_register('emailAddress'); 
            
    $_SESSION['emailAddress'] = $emailAddress
                    
             
                     
            
    header("Location: roster.php"); 
            exit();
        } 
    } else { 
        echo 
    "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> 
        Please try again!<br />"

        include 
    'login_form.html'

    }
    include (
    './footer.inc');
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •