login/register tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
Insight223
Posts: 5
Joined: Tue Mar 13, 2012 5:31 am

login/register tutorial

Post by Insight223 »

So the error I am having is that I keep getting an error like

Warning: include(/public_html/core/inc/inc/user.inc.php) [function.include]: failed to open stream: No such file or directory in /public_html/core/inc/init.inc.php on line 23

Warning: include() [function.include]: Failed opening '/public_html/core/inc/inc/user.inc.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /public_html/core/inc/init.inc.php on line 23

Here is my files.
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();
        }
}
 
include('core/dbconnect.inc.php');
 
$path = dirname(__FILE__);
 
include("{path}/inc/user.inc.php");
 
?>
include('core/dbconnect.inc.php');

All it is, is an include that allows me to connect easier than writing it out every time. Everything I have tried has NOT worked.


protected.hp
<?php
include('core/init.inc.php');
?>
user.inc.php
<?php

	//Checks if 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;
	}

	//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 `username` = `{$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}'')");

	}
?>
register.php
<?php
include('core/init.inc.php');

	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>
	<head>
		<title>Test</title>
	</head>
	<body>
		<h1>Register</h1>
		<form action="" method="POST">
			<p>
				<label for="username">Username:</label>
				<input type="text" name ="username" id="username" />
			</p>
			<p>
				<label for="password">Password:</label>
				<input type="password" name ="password" id="password" />
			</p>
			<p>
				<label for="repeat_password">Password:</label>
				<input type="password" name ="repeat_password" id="repeat_password" />
			</p>
			<p>
				<input type="submit" value="Register" />
			</p>
		</form>
	</body>
</html>
Any help would be VERY much appreciated!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: login/register tutorial

Post by Temor »

include("{path}/inc/user.inc.php");
you forgot the dollarsign ($) before path.
Insight223
Posts: 5
Joined: Tue Mar 13, 2012 5:31 am

Re: login/register tutorial

Post by Insight223 »

fixed the error.

Although now it is just letting me log in as anyone

if i type the username as aseiogjah and password adgih;sdn or anything it just logs in and says you are logged in as aseiogjah

my checker function
	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;
	}
Last edited by jacek on Wed Mar 14, 2012 5:06 pm, edited 1 time in total.
Reason: code tags...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: login/register tutorial

Post by Temor »

What does your login page look like?
Post Reply