Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    68
    Thanks
    29
    Thanked 0 Times in 0 Posts

    problems with header() function

    I'm building a simple login form that references itself. I'm trying to redirect after a successful login, but my header function isn't working.

    The username password query is working (verified by the echo statements) but I can't get the header function to work. Any ideas as to what is wrong?
    Code:
    <?php
    require_once 'scripts/app_config.php';
    require_once 'scripts/database_connection.php';
    session_start();
    
    ?>
    
    <!doctype html>
    
    <head>
    
    	<title>Login</title>
    	<!-- CSS -->	
    	<link rel="stylesheet" href="css/reset.css">
    	<link rel="stylesheet" href="css/animate.css">
    	<link rel="stylesheet" href="css/login.css">
    	
    </head>	
    <body>
    		
    		<?php
    			
    			$username = $_POST['username'];
    			$password = $_POST['password'];
    						
    			//Check if the username or password boxes were not filled in
    			if(!$username || !$password){
    				//if not display an error message
    				echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    			}else{
    				// find user by username and password - working
    				$userQuery = 'SELECT * FROM users 
                                       WHERE user_name ='.'"'. $username.'" AND password='.'"'. $password.'"' ; 
    				$users = mysql_query($userQuery);
    
            		$user = mysql_fetch_array($users);
            		// echo for testing - working - all values are printing
            		echo 'username:'.$user['user_id'].'<br />';
            		echo 'password:'.$user['password'].'<br />';
    				echo "<center>You have successfully logged in!</center>";				
    				
    				//set the login session storing there id - we use this to see if they are logged in or not
    				$_SESSION['user_id'] = $user['user_id'];
    				echo "session user_id: ".$_SESSION['user_id'].'<br />';
    				header("Location: http://aaronhaas.com/pitchshark6/index.php");
    				//exit;
    				}
    		?>
    
    		<div id="container">
    		<form action="" method="POST">
    		
    			<label for="username">Username:</label>
    			<input type="name" name="username" id="username">
    			
    			<label for="password">Password:</label>
    			<p><a href="#">Forgot your password?</a>
    			<input type="password" name="password" id="password">
    			
    			<div id="lower">
    				<input type="checkbox"><label class="check" for="checkbox">Keep me logged in</label>
    				<input type="submit" value="Login">
    			</div> <!-- close lower -->
    		
    		</form>
    		
    	</div>
    	
    	
    	<!-- End Page Content -->
    	
    </body>
    
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Unless you are using output buffering, headers must be called before any output. This includes breaks in PHP code with HTML or even just whitespace, and any print/echos.


  •  

    Posting Permissions

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