header redirect and echo a message

Discussion in 'PHP' started by webbusiness23, Oct 3, 2009.

  1. #1
    Hi,

    i have this problem:

    I have a login form. If the user enters the correct username/password he gets redirected to the index.php page.


    If the user enters the incorrect username/password, he gets redirected back to the login.php page.

    The problem is that i can't echo a message when the user is redirected from the index back to the login page. Something like all the login forms on the internet have: "Username/password wrong. Please try again."

    Here is the code from my index.php page:

    <?php
    require_once("includes/functions.php");
    session_start();
    	if ( (isset($_SESSION["username"])) && (isset($_SESSION["password"]))){
    	
    	$username=$_SESSION['username'];
    	
    	if ($_SESSION['password']==get_password($username)){
    		// print the index specific info here
    	
    		echo "Sunteti logat ca userul :".$username." <br/>";
    	}
    	else {
    	echo "Username/password wrong. Please try again.";
    	header( "Location: login.php" );	// cer o noua logare
    	
    	}
    	}
    	
    ?>
    PHP:
    The message "Username/password wrong. Please try again." is never displayed. Even is i type wrong username and password.
    Where should i put that echo message? Or is there something else wrong with the code?
     
    webbusiness23, Oct 3, 2009 IP
  2. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Your problem is that you echo before your header.

    You can never send text to the browser before the header.

    If you want this page to display the error message instead of the login form you have two choices:

    Have this page display the login form again when there is an error.

    or

    Display your error, then do a javascript redirect or meta-refresh on the page.

    Either one of those will fix it for you, but whatever you decide to do you can never have an "echo" or any other output characters before a call to header.
     
    goliath, Oct 3, 2009 IP
  3. renownedmedia

    renownedmedia Well-Known Member

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
  4. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #4
    try this and let me know what happens:

    
    
    <?php
    require_once("includes/functions.php");
    session_start();
        if ( (isset($_SESSION["username"])) && (isset($_SESSION["password"]))){
       
        $username=$_SESSION['username'];
       
        if ($_SESSION['password']==get_password($username)){
            // print the index specific info here
       
            echo "Sunteti logat ca userul :".$username." <br/>";
        }
        else {   
    ?>
    <meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/login.php">
    <?php
        echo "Username/password wrong. Please try again.";
    
       
        }
        }
       
    ?>
    
    
    PHP:
     
    jpinheiro, Oct 3, 2009 IP
  5. webbusiness23

    webbusiness23 Member

    Messages:
    153
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    Your solution does work. BUT it not exactly what i wanted to do.

    Let me explain it more carefully:

    - if the user types the correct username/password combination, he will see the info from the index.php page

    - if the user types the wrong username/password combination, he will be redirected to the login.php page AND he will see an error message above the login form. (echo "Username and password are wrong.Please try again.";)

    It's just like any login form on the internet. If you know your username/password you'll get to see further info from you account. If not, you will have to enter the username/pass again and you'll see an error message. This is what i want to do.


     
    webbusiness23, Oct 4, 2009 IP
  6. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #6
    How about you try this :)

    
    <?php
    require_once("includes/functions.php");
    session_start();
    
        if ( (isset($_SESSION['username'])) && (isset($_SESSION['password']))){
    	
    	$username = $_SESSION['username'];
    	$get_password = get_password($username)
    
    	if ($_SESSION['password'], $get_password == $get_password) {
    		
    		?>
    		<meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/index.php">
    		<?php
    		
    	 echo "Thank you for logging in: $username. <br/>";
        
    	} else { 
    	
    ?>
    <meta http-equiv="REFRESH" content="5;url=http://yourdomain.com/login.php">
    
    <?php
    
        echo "Username/password wrong. Please try again.";
    
    		}
    	}
       
    ?>
    
    
    
    
    PHP:
     
    jpinheiro, Oct 4, 2009 IP
  7. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    use like this

     
    lmao, Oct 4, 2009 IP
  8. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use like this

     
    lmao, Oct 4, 2009 IP
  9. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    oops it is posted twice i dont know how
     
    lmao, Oct 4, 2009 IP
  10. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #10
    you could also try to redirect the user to the login page with a name/value parameter in the URL redirect.
    For example: if the user enters wrong details, you could redirect to login page (which i assume is index.php)

    http://mysite.com/index.php?error_code=1
    ----------
    In index.php (your login page) you could assign the respective error text (ex. see below):

    if ($_GET['error_code'] == 1)
    {
    echo ("OOPS, you entered the wrong username or password. Plz. try again");
    }
    PHP:
     
    krishmk, Oct 4, 2009 IP
  11. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #11
    use session variable to store error before redirect and display it on page where you are redirecting


    Hope that helps

    Regards

    Alex
     
    kmap, Oct 4, 2009 IP
  12. toonboon

    toonboon Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    just have the login form post to the same page where the form is and handle loggin in in that same php file. If correct, header redirect, else show the original page with an error message :)

    easy as stealing vegetables from a child ^^
     
    toonboon, Oct 4, 2009 IP