Welcome Guest, Not a member yet? Register   Sign In
Using codeigniter Model from a php script
#1

Hi everyone!

I am developing a login script and want to authenticate users throw an existing application model , coded with CI . I know that the best way is to create the class in the CI folder structure and therefore but I have to do it like that . My problem is when i try to access the model of my script , I get the following error: Not allowed direct script access . ( I guess the problem is missing BasePath ) .

I do not know what files I need to include in my script and how can I remove this error? I just want to use the existing model that is already configured with the database .

I do not want to make database applications directly from my script but only use the existing model.

Thank you for the help . Smile
Reply
#2

I guess you can just define a constant named BASEPATH before requesting the model.
Reply
#3

Thank you Avenirer for your response. I cannot re-define BASEPATH that is already defined in config file. It doesn't work.

He is my code, please tell me what is wrong or how should i proceed.

<?php

include './server/application/config/config.php';//<--this line provide an error
require_once './server/configuration.php';//<--this line provide an error
include './server/application/models/user_model.php';//<--this line provide an error
//
//encode post data to not to be visible in http request in the web browser
$username = base64_encode(filter_input($_POST['username']));
$pass = base64_encode(filter_input($_POST['password']));

if($username && $pass){

$model = new User_Model();
$userdata = $model->check_login(base64_decode($username), base64_decode($password));
if($userdata){
//if we have a user -> set the session
$_SESSION['username'] = $username;
//fetch his appointments from database
//(not implemented yet)
//redirect user to member space
header('Location: memberSpace.php');
}else{
//set session error to display an error message in the view
$_SESSION['error'] = 'error';
//redirect user to the login page (customer.php)
header('Location: customer.php');
}
}

Any other ideas ? thanks in advance.
Reply
#4

The first line of application/config/config.php is most likely something like this:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed'); 

This simply checks whether BASEPATH is defined and, if not, exits with the given error.


For this reason, as long as you wish to use this file and refuse to use CodeIgniter itself, you need to define BASEPATH before you include this file (or most other CodeIgniter files). CodeIgniter defines BASEPATH in the index.php file in your CI project's root directory.

Of course, once you have BASEPATH defined, you will undoubtedly run into additional errors because the files in question assume certain things about the environment which will not be in place without CodeIgniter running. It's very unlikely that you'll be able to use a model based on the core CI_Model without CI itself, given that CI_Model is going to require the log_message and get_instance functions to work properly.

PHP Code:
class CI_Model {
    public function 
__construct()
    {
        
log_message('info''Model Class Initialized');
    }

    public function 
__get($key)
    {
        return 
get_instance()->$key;
    }

Reply
#5

You'd also need to load the db before trying to use a model. I think this will be a lot more complicated than it's worth doing it this way.

Why not have all of the work done in a CI controller, and then just have your other app use CURL or an AJAX request to hit that controller in your CI app?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB