Page 1 of 1

Register and Login (User Account System)

Posted: Sat May 26, 2012 5:20 pm
by gokgulu
I have a problem with my register page.
If I want to register then there is nothing in the database.

I checked my code 10 times, but i see nothing wrong HELP :(

This is my database
Image

init.inc.php
<?php

session_start();

$exceptions = array('register', 'login');

$script_name_array = explode('/', $_SERVER['SCRIPT_NAME']);
$last_bit = end($script_name_array);
$page = substr($last_bit, 0, -4);

if (in_array($page, $exceptions) === false){
	if(isset($_SESSION['gebruikersnaam']) === false){
		header('Location: login.php');
		die();
	}
}

mysql_connect('127.0.0.1', 'root', 'toor');
mysql_select_db('db_gebruikers1');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

?>
user.inc.php
<?php

// nakijken of de gebruikersnaam bestaat in de database
function user_exists($gebruiker){
	$gebruiker = mysql_real_escape_string($gebruiker);
	
	$totaal = mysql_query("SELECT COUNT(`gebruiker_id`) FROM `gebruikers` WHERE `gebruiker_naam` = '{$gebruiker}'");
	
	return (mysql_result($totaal, 0) == '1') ? true : false;	
}

// nakijken of de gebruikersnaam en wachtwoord combinatie klopt
function valid_credentials($gebruiker, $wachtwoord){
	$gebruiker = mysql_real_escape_string(htmlentities($gebruiker));
	$wachtwoord = sha1($wachtwoord);
	
	$totaal = mysql_query("SELECT COUNT('gebruiker_id') FROM `gebruikers` WHERE `gebruiker_naam` = '{$gebruiker}' AND 'gebruiker_wachtwoord' = '{$wachtwoord}'");

	return (mysql_result($totaal, 0) == '1') ? true : false;
}

// een gebruiker toevoegen aan de databese
function add_user($gebruiker, $wachtwoord){
	$gebruiker = mysql_real_escape_string(htmlentities($gebruiker));
	$wachtwoord = sha1($wachtwoord);
	
	mysql_query("INSERT INTO `gebruikers` (`gebruiker_naam`, `gebruiker_wachtwoord`) VALUES ('{$gebruiker}, '{$wachtwoord}')");
}

?>
register.php
<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['gebruikersnaam'], $_POST['wachtwoord'], $_POST['hhwachtwoord'])){
	if (empty($_POST['gebruikersnaam'])){
		$errors[] = 'Voer een gebruikersnaam in.';
	}
	if (empty($_POST['wachtwoord']) || empty($_POST['hhwachtwoord'])){
		$errors[] = 'Voer een wachtwoord in.';
	}
	if ($_POST['wachtwoord'] !== $_POST['hhwachtwoord']){
		$errors[] = 'Wachtwoord komt niet overeen.';
	}
	if (user_exists($_POST['gebruikersnaam'])){
		$errors[] = 'Gebruikersnaam is al in gebruik.';
	}
	if (empty($errors)){
		add_user($_POST['gebruikersnaam'], $_POST['wachtwoord']);
		
		$SESSION['gebruikersnaam'] = htmlentities($_POST['gebruikersnaam']);
		
		header('Location: protected.php');
		die();
	}
	
}

?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<link rel="stylesheet" type="text/css" href="ext/css/style.css" />
		<title></title>
	</head>
	<body>
		<div>
			<?php
			
			if (empty($errors) === false){
			?>
			<ul>
				<?php
				
				foreach ($errors as $error){
					echo "<li>{$error}</li>";
				}				
				?>
			</ul>
			<?php
			}		
			?>
		</div>
		<form action="" method="post">
			<table>
				<tr>
					<td>Gebruikersnaam:</td>
					<td><input type="text" name="gebruikersnaam" id="gebruikersnaam" value="<?php if (isset($_POST['gebruikersnaam'])) echo htmlentities($_POST['gebruikersnaam']); ?>" /></td>
				</tr>
				<tr>
					<td>Wachtwoord:</td>
					<td><input type="password" name="wachtwoord" id="wachtwoord" /></td>
				</tr>
				<tr>
					<td>Herhaal wachtwoord:</td>
					<td><input type="password" name="hhwachtwoord" id="hhwachtwoord" /></td>
				</tr>
					<td></td>
					<td><input type="submit" value="Register" /></td>
				</tr>				
			</table>
		</form>	
	</body>
</html>

Re: Register and Login (User Account System)

Posted: Sat May 26, 2012 11:23 pm
by jacek
mysql_query("INSERT INTO `gebruikers` (`gebruiker_naam`, `gebruiker_wachtwoord`) VALUES ('{$gebruiker}, '{$wachtwoord}')");
You have a missing ' on this line in the add_user() function. after the first variable in the VALUES part.

Something else I noticed is that in the valid_credentials() function you have this line
$totaal = mysql_query("SELECT COUNT('gebruiker_id') FROM `gebruikers` WHERE `gebruiker_naam` = '{$gebruiker}' AND 'gebruiker_wachtwoord' = '{$wachtwoord}'");
which has the wrong quotes in the COUNT() function, they should be ` not '

:)

Re: Register and Login (User Account System)

Posted: Sun May 27, 2012 12:42 am
by gokgulu
Ohh thank you it's working, but how can you see those little mistakes and I can not see them?
Do you have a trick or something?

First of all I am newbie in programming and I study computer science. This is my first year.
What is the secret to be a good programmer or what can you advise me?

THX for your help :)

Re: Register and Login (User Account System)

Posted: Sun May 27, 2012 11:33 am
by jacek
gokgulu wrote:but how can you see those little mistakes and I can not see them?
Do you have a trick or something?
That is a pretty common mistake so I get used to checking for it :)
gokgulu wrote:What is the secret to be a good programmer or what can you advise me?
Practice ! Just write lots of code, try doing things in different ways.

Re: Register and Login (User Account System)

Posted: Sat Aug 11, 2012 12:22 pm
by pluggy
Is there anyplace on here where I can download the files used in the the tutorual?

Thanks

Re: Register and Login (User Account System)

Posted: Sun Aug 12, 2012 12:45 am
by jacek
Nope sorry :(