register/login acount -- fetal errors
Posted: Sun Aug 26, 2012 10:11 pm
				
				Dear all;
I worked very hard check more than 10 times and spend around a week already, but i couldn't find and solve the errors.
Can anyone help me ,please?
when i try to register I always got this error:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\tapi\core\inc\user.inc.php on line 6 ".
			I worked very hard check more than 10 times and spend around a week already, but i couldn't find and solve the errors.
Can anyone help me ,please?
when i try to register I always got this error:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\tapi\core\inc\user.inc.php on line 6 ".
user.inc.php
 
<?php
function user_exists($user){ 
	$user = mysql_real_escape_string($user);
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'");
	return (mysql_result($total, 0) == '1') ? true : false; 
}	
function valid_credentials($user, $pass){ 
	$user = mysql_real_escape_string($user); 
	$pass = sha1($pass);
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");
	return (mysql_result($total, 0) == '1') ? true : false; 
}
function add_user($user, $pass){ 
	$user = mysql_real_escape_string(htmlentities($user)); 
	$pass = sha1($pass);
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}' , '{$pass}')");
}
?>
init.inc.php
<?php
session_start();
$exceptions = array('register', 'login');
$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);
if (in_array($page, $exceptions) === false) 
{ 
	if (isset($_SESSION['username']) === false)
	{
		header('Locaton: login.php'); 
		die(); 
	}
}
mysql_connect('127.0.0.1' , 'root' , '485849'); 
mysql_select_db('user_system'); 
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?>
register.php
<?php
include("core/inc/user.inc.php");
$errors = array();
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
	if (empty($_POST['username'])){ //check to see username is not empty.
		$errors[] = 'The username cannot be empty.';
	}
	if (empty($_POST['password']) || empty($_POST['repeat_password'])){
		$errors[] = 'The password and repeat_password cannot be empty.';
	}
	if (user_exists($_POST['username'])){
		$errors[] = 'The username you entered is already taken.';
	}
	if (empty($errors)){
		add_user($_POST['username'], $_POST['password']);
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Location: protected.php');
		die();
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
	</head>
	<body>
		<p>
			<?php
				if (empty($errors) === false)
				{
					?>
					<ul>
						<?php
						foreach ($errors as $errors) 
						{
							echo "<li>{$errors}</li>";
						}
					?>
					</ul>
					<?php
				}
			?>
		</p>
		<form action=""method="post">
			<p>
				<label for="username">username:</label>
				<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
			</p>
			<p>
				<label for="password">password:</label>
				<input type="password" name="password" id="password"/>
			</p>
			<p>
				<label for="repeat_password">repeat password:</label>
				<input type="password" name="repeat_password" id="repeat_password"/>
			</p>
			<p>
				<input type="submit" value="register"/>
			</P>
		</form>
	</body>
	</head>
</html>
login.php
<?php
include('core/init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password']))
{
	if (empty($_POST['username']))
	{
		$errors[] = 'The username cannot be empty.';
	}
	if (empty($_POST['password']))
	{
		$errors[] = 'The username cannot be empty.';
	}
	if (valid_credentials($_POST['username'], $_POST['password']) === false)
	{
		$errors[] = 'Username / Password incorrect.';
	}
	if (empty($errors))
	{
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Locaiton: protected.php');
		die();
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
	</head>
	<body>
		<div>
			<?php 
			if (empty($errors) === false)
			{
			?>
				<ul>
					<?php
					foreach ($errors as $errors)
					{
						echo"<li>{$errors}</li>";
					}
					?>
				</ul>
			<?php	
			}else{
				echo 'Need an account ? <a href="register.php">Register here</a>';
			}
			?>
			
		<form action=""method="post">
			<p>
				<label for="username">username:</label>
				<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
			</p>
			<p>
				<label for="password">password:</label>
				<input type="password" name="password" id="password"/>
			</p>
			<p>
				<input type="submit" value="login"/>
			</P>
		</form>
	</body>
	</head>
</html>
protected.php
<?php include('core/init.inc.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
	</head>
	<body>
		<p>
			you are logged in as <?php echo $_SESSION['username'];  ?>
		</p>
		<p>
			<a> href="logout.php"> Logout ? </a>
		</p>
	</body>
</html	
logout.php
<?php
session_start();
$_SESSION = array();
session_destroy();
header('Location: protected.php');
?>
 )
)