Click here to Skip to main content
15,076,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have built a login application in PHP(no frameworks) with MySQL as Database.
I am getting warnings and errors when I attempt to run index.php on localhost (WAMP). The error says:
"Warning: require_once(Cl_User.php): failed to open stream: No such file or directory in C:\wamp\www\login\config.php on line 18"
"Error: Fatal error: require_once(): Failed opening required 'Cl_User.php' (include_path='.;C:\php\pear') in C:\wamp\www\login\config.php on line 18"
What should I do to remove errors?
Here is the php file codes:

a. index.php:
ob_start();
session_start();
require_once 'config.php';

// Initialize User Class
$user_obj = new Cl_User();
?>

if( !empty($_POST)) {
try {
$data = $user_obj->login( $_POST );
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
header('Location: home.php');
}
} catch (Exception $e) {
$error = $e->getMessage();
}
}

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
header('Location: home.php');
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE-Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP Login Page Demo</title>
</head>
</html>

b. config.php:


require_once 'messages.php';

// site specific config declaration
define( 'BASE_PATH', 'http://localhost/login/ (http://localhost/login/)');
define( 'DB_HOST', 'localhost');
define( 'DB_USERNAME', 'root');
define( 'DB_PASSWORD', '');
define( 'DB_NAME', 'login');

function __autoload($class)
{
$parts = explode('_', $class);
$path = implode(DIRECTORY_SEPARATOR, $parts);
require_once $path . '.php';
}
?>

c. messages.php:
define('FIELDS_MISSING', 'Some Fields Are Missing');
define('PASSWORD_NOT_MATCH', 'Passwords do not match');
define('USER_REGISTRATION_FAIL', 'User registration failed');
define('USER_REGISTRATION_SUCCESS', 'User registration was successful, You may login now');

define('LOGIN_FIELDS_MISSING', 'Email and Password missing');
define('LOGIN_FAIL', 'Email and Password Mismatch');

define('PASSWORD_CHANGE_SUCCESS', 'Password Changed Successfully.');

?>

d. User.php:
class Cl_User
{
protected $_con;

public function __construct()
{
$db = new Cl_DBclass();
$this->_con = $db->con;
}

public function registration(array $data)
{
if(!empty($data)){
// Trim all incoming data
$trimmed_data = array_map('trim', $data);

$name = mysqli_real_escape_string( $this->_con, $trimmed_data['name'] );
$password = mysqli_real_escape_string( $this->_con, $trimmed_data['password'] );
$cpassword = mysqli_real_escape_string( $this->_con, $trimmed_data['confirm_password'] );

// Check for an email address:
if(filter_var( $trimmed_data['email'], FILTER_VALIDATE_EMAIL)) {
$email = mysqli_real_escape_string( $this->_con, $trimmed_data['email']);
} else {
throw new Exception("Please enter a valid email address!");
}

if((!$name) || (!$email) || (!$password) || (!$cpassword) ) {
throw new Exception( FIELDS_MISSING );
}
if($password !== $cpassword ) {
throw new Exception( PASSWORD_NOT_MATCH );
}
$password = md5( $password );
$query = "INSERT INTO users (user_id, name, email, password, created) VALUES (NULL, '$name', '$email', '$password', CURRENT_TIMESTAMP)";
if(mysqli_query( $this->_con, $query )) return true;
} else {
throw new Exception( USER_REGISTRATION_FAIL );
}
}

public function login(array $data) {
$_SESSION['logged_in'] = false;
if( !empty($data) ){
$trimmed_data = array_map('trim', $data);

$email = mysqli_real_escape_string( $this->_con, $trimmed_data['email'] );
$password = mysqli_real_escape_string( $this->_con,$trimmed_data['password'] );

if((!email) || (!password) ) {
throw new Exception( LOGIN_FIELDS_MISSING );
}
$password = md5( $password );
$query = "SELECT user_id, name, email, created FROM users WHERE email = '$email' AND password = '$password' ";
$result = mysqli_query($this->_con, $query);
$data = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
if( $count == 1 ){
$_SESSION = $data;
$_SESSION['logged_in'] = true;
return true;
} else{
throw new Exception( LOGIN_FAIL );
}
} else{
throw new Exception( LOGIN_FIELDS_MISSING );
}
}

public function account( array $data )
{
if( !empty($data) ){
$trimmed_data = array_map('trim', $data);

$password = mysqli_real_escape_string( $this->_con, $trimmed_data['password'] );
$cpassword = $trimmed_data['confirm_password'];
$user_id = mysqli_real_escape_string( $this->_con, $trimmed_data['user_id'] );

if((!$password) || (!$cpassword) ) {
throw new Exception( FIELDS_MISSING );
}
if($password !== $cpassword ) {
throw new Exception( PASSWORD_NOT_MATCH );
}
$password = md5( $password );
$query = "UPDATE users SET password = '$password' WHERE user_id = $user_id ";
if(mysqli_query($this->_con, $query)) return true;
} else {
throw new Exception( FIELDS_MISSING );
}
}

public function logout()
{
session_unset();
session_destroy();
header('Location: index.php');
}

public function forgetPassword( array $data )
{
if( !empty($data) ){
$email = mysqli_real_escape_string( $this->_con, trim( $data['email'] ) );

if((!$email) ){
throw new Exception( FIELDS_MISSING );
}
$password = $this->randomPassword();
$password1 = md5( $password );
$query = "UPDATE users SET password = '$password1' WHERE email = $email";
if(mysqli_query($this->_con, $query)){
$to = $email;
$subject = "New Password Request";
$txt = "Your New Password ".$password;
$headers = "From: admin@abc.com (mailto:admin@abc.com)" . "\r\n" . “CC: admin@abc.com (mailto:admin@abc.com)“;

mail($to,$subject,$txt,$headers);
return true;
}
} else {
throw new Exception( FIELDS_MISSING );
}
}

private function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}
}
?>

e. DBclass.php:
class Cl_DBclass
{
public $con;

public function __construct()
{
$this->con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if( mysqli_connect_error()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
?>

What I have tried:

I don't know much about PHP(like autoload function in PHP). I need a solution to this problem because I have to make the application further by adding other useful features.
Posted
Updated 31-Aug-20 19:56pm
Comments
Mohibur Rashid 3-Aug-16 20:00pm
   
This is really surprising that your autoloader is looking for Cl_User.php, I have just copied part of your code and execute, it works properly...

add this line in your __autoload function
echo DIRECTORY_SEPARATOR . "<br>";
and see what happens. And also print this part
echo $path . '.php';
and see what result do you get. Printing value is the quickest way to simulate debug..

alternative for your __autoload function
function __autoload($class)
{
require_once str_replace("_", DIRECTORY_SEPARATOR , $class).".php";
}
Asad Chishti 4-Aug-16 3:30am
   
Thanks for your answer.

I will test it and let you know if this works on my system.

1 solution

Fatal error: require(): Failed opening required 'connection.PHP' (include_path='.;C:\PHP\pear') in C:\wamp\WWW\5b\customer registration.PHP on line 58
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900