My membership script works fine, except I can't login, because this check.php script doesn't work. Login form action is check.php. Once I login with username and password, check.php gives me an error message "Please enter ALL of the information". The only information on login form is username and password. The password is a random md5 password. I copied and paste this script from a php tutortial, because I don't know php. Everything works except this file. How do I direct it to my member page once its working? Please help.

<?
/* Check Username Script */
session_start();  // Start Session

include 'db.php';
// 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';
	exit();
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$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('first_name');
		$_SESSION['first_name'] = $first_name;
		session_register('last_name');
		$_SESSION['last_name'] = $last_name;
		session_register('email_address');
		$_SESSION['email_address'] = $email_address;
		session_register('special_user');
		$_SESSION['user_level'] = $user_level;
		
		mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
		
		header("Location: members.php");
	}
} 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';
}
?>

where is your log-in form? if you run this directly of course you will encounter an error to enter the field, coz you username and password are empty

here is th login script.

<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="check.php" method="post" name="" id="">
  <table width="50%" border="0" align="center" cellpadding="4" cellspacing="0">
    <tr> 
      <td width="22%">Username</td>
      <td width="78%"><input name="username" type="text" id="username"></td>
    </tr>
    <tr> 
      <td>Password</td>
      <td><input name="password" type="password" id="password"></td>
    </tr>
    <tr> 
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>
</body>
</html>

put your code inside $_POST

like

<?
/* Check Username Script */
session_start(); // Start Session

include 'db.php';
// Conver to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if($_POST['Submit']){ // start of $_POST['Submit']

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

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$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('first_name');
$_SESSION['first_name'] = $first_name;
session_register('last_name');
$_SESSION['last_name'] = $last_name;
session_register('email_address');
$_SESSION['email_address'] = $email_address;
session_register('special_user');
$_SESSION['user_level'] = $user_level;

mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");

header("Location: members.php");
}
} 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';
}

} // end of $_POST['Submit']

?>
Member Avatar for rajarajan2017
if(empty($username) || empty($password)){
echo "Please enter ALL of the information! <br />";
include 'login_form.html';
exit();
}

I uploaded the code above. After I type username and password on login form I got these error messages.

1. Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by output started at check.php

2. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent output started at check.php

Member Avatar for rajarajan2017

session_start(): should be in the first line, Some unwanted characters are added before or after <?php and session_start. Try to identify in some of the editors and remove it. it will work.

haeders are send when ou output something.
so put the sesion stuf before anything else.

also make sure that there are no empty lines before your

no empty line here
<?php 

no echo here

sesion_stuff()
?>
Member Avatar for rajarajan2017

Use ob_start(); before session_start();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.