Jump to content

[SOLVED] Headers error in check login script


graham23s

Recommended Posts

Hi Guys,

 

i normally use this script for my check login:

 

<?php
     include("includes/db_connection.php");     
     // the includes...//////////////////////////////////////////////////////////////////
     include("includes/header.php");
     include("includes/navbar.php");    
     // For register_global on PHP settings///////////////////////////////////////////////
 $username = strtolower($_POST['username']);
 $password = strtolower($_POST['password']);	
 // Check for empty fields
  	 if (empty($username) || empty($password)) {	
 echo "<center><br /><font color=\"red\"><p>Sorry Please Fill In All The Required Fields <a href=\"login.php\">Try Again</a></p></font></center>";
 exit;	
 }	
 // Match the user in the database.../////////////////////////////////////////////////
 $query_1 = "SELECT username,password FROM `membership` WHERE `username`='$username' AND `password`='$password'";
 $result_1 = mysql_query($query_1) or die (mysql_error());	
 $is_greater_than_1 = mysql_num_rows($result_1);	
 // was there a user found?...////////////////////////////////////////////////////////
 if($is_greater_than_1 != 1) {	
     echo "<p>Sorry, You Have Typed In The Wrong Username And Password Combination.</p>";	
 } else {	
 // User is logged in so Let's give him a cookie. ////////////////////////////////////
 setcookie ("member",$username,time()+3600*24*30,"/"); 
 $member = $username;
 // If cookie failed set a session.../////////////////////////////////////////////////
 $_SESSION['member'] = 'member';	
 // Update Login timer.../////////////////////////////////////////////////////////////
 $query_2 = "UPDATE `membership` SET `login`=now() WHERE `username`='$username' AND `password`='$password'";
 $result_2 = mysql_query($query_2);	
     if ($result_2) {	
 header("Location:my_account.php"); // redirects members to a welcome member page/////	
     }}
?>

 

i'm getting "Warning: Cannot modify header information" error, i know it's because i have echoes in there before the header but i was wondering if there was a better way i could do this, would i be better putting the check login scrips on the same page as the login.php or would i still get errors?

 

thank guys

 

Graham

The reason why is because you are outputting html before you are setting cookies... or sending the header("LOCATION:") deal...

 

and it all has to deal with the placement of these 2 lines:

 

include("includes/header.php");

    include("includes/navbar.php"); 

 

change the placement of those, and you'll be set...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.