John Smith
John Smith

Reputation: 9

Stuck on a bizarre pathing error

I have pathing errors :

Warning: require_once(test/assets/includes/memberfunc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

Fatal error: require_once(): Failed opening required 'test/assets/includes/memberfunc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

Yet my paths are correct. Base directory is test. Test is where all my pages sit. Above it is assets/includes/logininc.php and assets/configs/db_config.php. Everything should be correct. Please advise

This is my very simple form that sits in test called login.php.

<form id="login-form" method="post" action="assets/includes/logininc.php"> <fieldset> 
  <legend>Login </legend> 
  <p>Please enter your username and password to access the administrator's panel</p>
  
   <label for="username"> <input type="text" name="username" id="username" />Username: </label> <label for="password"> <input type="password" name="password" id="password" />Password: </label> <label for="submit"> <input type="submit" name="submit" id="submit" value="Login" /> </label> </fieldset> </form>

Which in turn, directs me to the file below. Logininc.php. It is located in assets/includes/logininc.php

<?php


require_once("/test/assets/includes/memberfunc.php");  //LINE 4
require_once("/test/assets/config/db_config.php"); 



session_start(); 

if($_SESSION['logged_in'] ==true) {
    
    redirect('/test/index.php');
        echo "success";
}
else{
    if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) or                                        
    
    
    (!ctype_alnum($_POST['username'])))
    
    redirect("/test/login.php");
}
                                            
    $mysqli = @new mysqli(HOST, NAME, PW, DB);
    
//check connection

if(mysqli_connect_errorno())
{
    printf("Unable to connect to DB! : %s",
           mysqli_connect_error());
    exit();
}

//Escape unsafe chars 

$username = $mysqli->real_escape_string($_POST['username']);
$username = $mysqli->real_escape_string($_POST['password']);

//Time to make a SQL Q for execution

$sql = "SELECT * FROM users WHERE username= '".$username . "'AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);

// If one row is returned, USER AND PW is valid!

if (is_object($result) && $result->num_rows == 1) {
    
    //set session variable for login true
    
    $_SESSION['logged_in'] = true;
              
              redirect('../../index.php');
}
else{ //if number of rows is not one redirect back to login
redirect('../../login.php');
}

    
    ?>

.

Please advise.

Upvotes: 0

Views: 133

Answers (3)

Simon Germain
Simon Germain

Reputation: 6844

What you want is the current working directory. This is achieved through 2 different ways:

Relative path:

require_once('./assets/includes/memberfunc.php');

Absolute path:

require_once(getcwd() . '/assets/includes/memberfunc.php');

The major difference here is that the HTML template builds its relative path based on the URL passed to the browser. PHP builds its relative path based on which directory on disk the files are executed from.

Upvotes: 0

DaveBaldwin
DaveBaldwin

Reputation: 44

'memberfunc.php' is in the same directory so you don't need the path. And out of shear laziness, I would put a copy of 'db_config.php' in that directory also so your statements would look like this:

require_once("memberfunc.php");  //LINE 4
require_once("db_config.php"); 

Upvotes: 1

jh314
jh314

Reputation: 27812

How about using dirname(__FILE__):

require_once(dirname(__FILE__)."/test/assets/includes/memberfunc.php");
require_once(dirname(__FILE__)."/test/assets/config/db_config.php"); 

Upvotes: 0

Related Questions