SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help 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. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The main problem is your echoing out content before the header function has a chance to run, for this function to work correctly nothing can be echoed out before it runs so all your HTML code before your PHP code and echo statements will need to be moved so it loads after the PHP code.


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
  •