Register and Login problem

Post here is you are having problems with any of the tutorials.
Post Reply
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Register and Login problem

Post by JuiceYum »

I have a problem where when I push submit on my login page it doesn't do anything :/ Here is all my code and a link to my website is here: http://tumabackup.x10.mx/
Login page: http://tumabackup.x10.mx/login.php/

Code for login.php:
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1); 

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

$errors = array();

if (isset($_POST['username'], $_POST['password'])){
	if (empty($_POST['username'])){
		$errors[] = 'The username cannot be empty.';
	}
	mysql_error();
	if (empty($_POST['password'])){
		$errors[] = 'The password cannot be empty.';
	}
	
	if (empty($errors) && valid_credentials($_POST['username'], $_POST['password']) === false){
		$errors[] = 'Username or password are incorrect.';
	}

	if (empty($errors)){
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Location: protected.php');
		die();
	}

}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Login</title>

<link rel="shortcut icon" href="favicon.ico">

<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>

<link href="/ext/style/style-login.css" rel="stylesheet" type="text/css" media="screen" />
</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="index.php">Register here</a>';
		}
		
		?>
	</div>
	<form action="" method="post">
		<p>
			<label for="name">Username:</label>
			<input type="text" id="name" placeholder="Username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
		</p>
		<p>
			<label for="password">Password:</label>
			<input type="password" id="password" placeholder="Password">
		</p>
		<p>
			<input type="submit" value="Submit" />
		</p>
	</form>

</body>
</html>
Please reply with all possible fixes as I am going to bed :P
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login problem

Post by jacek »

<input type="password" id="password" placeholder="Password">
Your form inputs need the name attribute, so this one should be
<input type="password" id="password" name="password" placeholder="Password" />
Image
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login problem

Post by JuiceYum »

Still not working :(
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login problem

Post by JuiceYum »

Heres all my code: I've been looking and searching everywhere for a fix D:
Edit: username and password to login are: test
Url: http://tumabackup.x10.mx/

login.php:
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1); 

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

$errors = array();

echo mysql_error();

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 (empty($errors) && valid_credentials($_POST['username'], $_POST['password']) === false){
		$errors[] = 'Username or password are incorrect.';
	}

	if (empty($errors)){
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Location: protected.php');
		die();
	}
echo mysql_error();
}

echo mysql_error();

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Login</title>

<link rel="shortcut icon" href="favicon.ico">

<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>

<link href="/ext/style/style-login.css" rel="stylesheet" type="text/css" media="screen" />
</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="index.php">Register here</a>';
		}
		
		?>
	</div>
	<form action="" method="post">
		<p>
			<label for="name">Username:</label>
			<input type="text" id="name" placeholder="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="Submit" />
		</p>
	</form>

</body>
</html>
index.php (register page):
<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1); 

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[] = 'Passwords dont match!';
	}
	
	if (user_exists($_POST['username'])){
		$errors[] = 'Username already exists!';
	}
	
	if (empty($errors)){
		add_user($_POST['username'], $_POST['password']);
		
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: protected.php');
		die();
	}	
}

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Sign Up</title>

<link rel="shortcut icon" href="favicon.ico">

<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>

<link href="/ext/style/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="contact">
	<h1>Under Construction.</h1>
	<p><a href="login.php">Login</a></p>
	<form action="/" method="post">
			<input type="text" name="username" id="username" placeholder="Username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />

			<input type="password" name="password" id="password" placeholder="Password" />

			<input type="password" name="repeat_password" id="repeat_password" placeholder="Re-Type Password">

			<input type="submit" value="Submit" />

	</form>
	
			<?php 
		
			if (empty($errors) === false){
				?>
				<ul>
					<?php
						
						
						foreach ($errors as $error){
							echo "<li>{$error}</li>";
						}
						
					?>
				</ul>
				<?php
			}
		
		?>
</div>

</body>
</html>
protected.php:
<?php include('core/init.inc.php'); ?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>NewCo | Welcome</title>
	</head>
	<body>
		<p>
			You are logged in as <?php echo $_SESSION['username']; ?>
		</p>
		<p>
			<a href="logout.php">Logout?</a>
		</p>
	</body>
</html>
init.inc.php:
<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1); 

session_start();

$exceptions = array('index','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('localhost', 'tumaback_user', 'root');
mysql_select_db("tumaback_system_user");

$path = dirname(__FILE__);

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

?>
users.inc.php:
<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1); 

// Checks if the given user name 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}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks 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}')");
}

?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login problem

Post by Temor »

 <input type="text" id="name" placeholder="Username" value="" />
this input also needs the name attribute.
you are checking to see if $_POST['username'] and $_POST['password'] is set.
if (isset($_POST['username'], $_POST['password'])){
You need to name the field username for it to work.

You also might want to change the U in `User_password` in your SQL on line 20 in user.inc.php to a lowercase letter.
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Register and Login problem

Post by JuiceYum »

Thank you very much both times Temor. I remember changing it distinclty on my index for that but then I nedver bothered with login and just didn't realise xD The way you give help is great because so far your help has taught me both things and now they're stuck in my head :P
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login problem

Post by Temor »

JuiceYum wrote:Thank you very much both times Temor. I remember changing it distinclty on my index for that but then I nedver bothered with login and just didn't realise xD The way you give help is great because so far your help has taught me both things and now they're stuck in my head :P
I'm glad I could help :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login problem

Post by jacek »

JuiceYum wrote:The way you give help is great because so far your help has taught me both things and now they're stuck in my head :P
:D
Image
Post Reply