Jump to content

php fatal error


Go to solution Solved by kicken,

Recommended Posts

Hiya I'm going through a php tutorial on building a basic register and login system. I've found I have 3 errors, 2 warnings and 1 fatal error. I think it is relating to the include file, it seems that it cant find it, even though it is in the directory specified. 

 

Warning: include(./inc/user.inc.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\wamp\www\simplyinspiration\core\init.inc.php on line 2

 

Warning: include() [<a href='function.include'>function.include</a>]: Failed opening './inc/user.inc.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\simplyinspiration\core\init.inc.php on line 20

 

Fatal error: Call to undefined function user_exists() in C:\wamp\www\simplyinspiration\register.php on line 16

 

I have 3 pages

 

 

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 alreadt taken.';
	}
	
	if (empty($errors)){
		add_user($_POST['username'], $_POST['password']);
		
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: protection.php');
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title></title>
	</head>
	<body>
		<p>
	<?php 
	
	?>
</p>

<form action="" method="post">
<p>
	<label for="username">Username:</label>
	<input type="text" name="username" id="username" />
</p>
<p>
	<label for="pasword">Password:</label>
	<input type="password" name="password" id="password" />
</p>
<p>

	<label for="repeat_pasword">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 which is located in core directory  

<?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('localhost', 'root', 'worldpeace');
mysql_SELECT_db('simply_inspiration');

$path = dirname("_FILE_");

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

And finally the user.inc.php which is located in core \ inc  directory

<?php 
// check if given username exists in table
function user_exists($user){
	$user = mysql_real_escape_string($user);
	$total = mysql_query("SELECT COUNT ('UserID') FROM 'user' WHERE `Username` = '{$user}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}

// check if 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 ('UserID') FROM 'user' WHERE `Username` = '{$user}' AND `Password` = '{$pass}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}


// add users to the database
function add_user($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);

mysql_query("INSERT INTO `user` (`Username`, `Password`) VALUES ('{$user}', '{$pass}')");
}

?>

I'm guessing the errors are because it seems it cant find the 2 includes and the problem lies with the init.inc.php because that's where the file directory is specified.  But they seem to be in the correct place.

 

According to the tutorial it should be working fine, i've checked and checked again. There's no troubleshooting now so I'm at a total loss. 

 

If you could help me that would be ace!

 

Thank you very much.  

Link to comment
https://forums.phpfreaks.com/topic/279390-php-fatal-error/
Share on other sites

Thank you very much for your reply. Yes there were no  ' ' on it originally but it was spewing all kinds of errors. I now realise that was because I was using underscores _ on either side of file _.

 

That's great its fixed those errors! Its still not functioning though and there is  a new warning: Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\simplyinspiration\core\inc\user.inc.php on line 7

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

Thank you so very much

Link to comment
https://forums.phpfreaks.com/topic/279390-php-fatal-error/#findComment-1437057
Share on other sites

 

	$total = mysql_query("SELECT COUNT ('UserID') FROM 'user' WHERE `Username` = '{$user}'");

 

You do not quote table and column names. Remove your quotes around UserId and user. The same applies to your other query.

 

Also, the reason for the error is because the query is failing, due to the above issues. You should be doing some error checking to ensure the query is successful rather than just blindly passing $total into mysql_result. The PHP manual for mysql_query has plenty of examples of error checking.

Link to comment
https://forums.phpfreaks.com/topic/279390-php-fatal-error/#findComment-1437061
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.