Page 1 of 1

Register and Login (User Account System) [part 06]

Posted: Fri Jan 20, 2012 11:47 pm
by hys
Hi there.

I have seen the part 6,

and when i refresh site to the login it well not show anything on the site at all its totaly white. And one last thing is it possible to get ur css code i love the set up u have on the login and register :)

The code is bellow here.

Regards
Hys

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 password cannot be empty.':
	}
	
	if (valid_credentials($_POST['username'], $_POST['password']) === false) {
		$errors[] = 'Username / Password incorrect.';
	}
	
	if (empty($errors)) {
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: protected.php');
		die();
	}
}

?>
<!DOCTYPE html>

<html lang="da">
<head>
        <meta charset="utf-8">
        <title></title>
	<link rel="stylesheet" type="text/css" href="ext/css/style.css" />

</head>
	<body>
		<div>
			<?php
			
			if (empty($errors) === false) {
				?>
				<ul>
					<?php
					
					foreach ($errors as $error) {
						echo "<li>{$error}</li>";
					}
					
					?>
				</ul>
				<?php
			} else {
				echo 'Need an account ? <a href="register.php">Register here</a>';
			}
			
			?>
		</div>
		<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>
</html>
logout.php
<?php

session_start();

$_SESSION = array();

session_destroy();

header('Location: protected.php');

?>
protected.php
<?php include('core/init.inc.php'); ?>
<!DOCTYPE html>

<html lang="da">
<head>
        <meta charset="utf-8">
        <title></title>

</head>
	<body>
		<p>
			You are logged in as <?php echo $_SESSION['username']; ?>
		</p>
		<p>
			<a href="logout.php">Logout ?</a>
		</p>
	</body>
</html>
register.php
<?php 

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

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])) {
	if (empty($_POST['username'])) {
		$errors[] = 'The username cannot be empty.';
	}
	
	if (empty($_POST['password']) || empty($_POST['repeat_password'])) {
		$errors[] = 'The password cannot be empty.';
	}
	
	if ($_POST['password'] !== $_POST['repeat_password']) {
		$errors[] = 'Password verification failed.';
	}
	
	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>

<html lang="da">
<head>
        <meta charset="utf-8">
        <title></title>
	<link rel="stylesheet" type="text/css" href="ext/css/style.css" />

</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">
			<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>
</html>
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('Location: login.php');
		die();
	}
}

mysql_connect('cant tell u that', 'cant tell u that', 'cant tell u that');
mysql_select_db('cant tell u that');

$path = dirname(__FILE__);

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

?>
user.inc.php
<?php

// check if the given username exists in the database.
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;
}

// check if the given username and password combination is valid.
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;
}

// adds a user to the database
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}')");
}

?>

Re: Register and Login (User Account System) [part 06]

Posted: Sun Jan 22, 2012 2:33 pm
by jacek
A totally blank page usually indicates a syntax error that is being hidden.

Make sure you have error_reporting set to E_ALL and display_error set to On in your php.ini file (google can tell you how to do that)

And I don't have the css any more :(

Re: Register and Login (User Account System) [part 06]

Posted: Sun Jan 22, 2012 2:47 pm
by hys
jacek wrote:A totally blank page usually indicates a syntax error that is being hidden.

Make sure you have error_reporting set to E_ALL and display_error set to On in your php.ini file (google can tell you how to do that)

And I don't have the css any more :(

I found the error :)

But is it possible for you to help mé make the css from your vids :) really like the style.

Regards
Mike aka hys

Re: Register and Login (User Account System) [part 06]

Posted: Sun Jan 22, 2012 2:49 pm
by jacek
I don't really remember what it looked like, I doubt it was anything fancy ;)

Have a go yourself, if you get stuck we have a css section on the forum :)