John
John

Reputation: 750

CodeIgniter BASEPATH

Hi I am new to Codeigniter. From my understanding of the BASEPATH, it is used to activate the Codeigniter framework. The code

defined('BASEPATH') OR exit('No direct script access allowed');

is used at top of the page to prevent direct access to the controller. But it is not working in my code. When I try to directly access a view via controller, the view gets loaded. I have checked index.php and BASEPATH is defined there. Please advice.

Upvotes: 6

Views: 46600

Answers (7)

Ali Sheykhi
Ali Sheykhi

Reputation: 95

what ever you do , you can not access view page without getting in index.php (unless you tun off php server or change ".htaccess" file ) so any request will first go to index.php ! that why it is gets loaded

Upvotes: 0

Kenneth Kipchumba
Kenneth Kipchumba

Reputation: 59

defined('BASEPATH') OR exit('No direct script access allowed'); does the following:

  1. defined('BASEPATH') => Checks whether CodeIgniter constant called BASEPATH is defined. If it is defined, it returns TRUE and nothing happens further. If it is not defined, it returns FALSE and the preceding code which is exit('...'); is thereby executed with the information 'No direct script allowed'.

When you say it is not working on your code. Which code are you referring to? Can you share the said code? View files are usually loaded by the controllers.

Upvotes: 3

Akshay Kumar
Akshay Kumar

Reputation: 7660

Actually codeigniter won't allow any its files direct accessible All the request are processed by index.php which is present in root folde of code igniter. whenever we request any codeigniter url index.php process it and define a constant . defined('BASEPATH') OR exit('No direct script access allowed'); check whether constant defined or not if not then php script exit and won't process further.

if you call any view file from controller then it work and not exit your script because your request already processed by index.php when you hit any controller. if you try to access direct view file then it won't work and php script exit

Upvotes: 1

User accesses the framework through a controller (http://example.com/index.php/controllername). The respective controller is activated through the URL parameters. As PHP assumes that classes within the framework are already declared, this line assures that the framework is properly booted up and the server is not running a single PHP file. Not using this can be a potential security breach, and if the framework is not booted up, the controller file would run on its own and fail.

Details of the statement.

  1. defined('BASEPATH') OR exit('No direct script access allowed');The function defined() checks if the constant exists
  2. BASEPATH is a constant defined in Codeigniter reserved names, which contains the path to the system folder.
  3. exit() function just prints the string and terminates the script execution.

source-->https://www.quora.com/profile/Gursharan-Singh-Dhanjal

Upvotes: 0

Zeeshan Ahmad
Zeeshan Ahmad

Reputation: 1

Use this code before Class start, for example:

defined('BASEPATH') OR exit('No direct script access allowed');

class Dataentry extends CI_Controller { }

Upvotes: -2

Rwd
Rwd

Reputation: 35180

defined('BASEPATH') OR exit('No direct script access allowed'); is used to make sure that the request has gone through index.php in your root dir. This is for reasons such as making sure that all Codeigniter base classes are being loaded and making sure certain vars have been set etc.

So, yes you will be able to access a view file if you're going through a controller.

Hope this helps!

Upvotes: 12

user5885520
user5885520

Reputation:

defined('BASEPATH') OR exit('No direct script access allowed');
  • defined: Checks whether a given named constant exists
  • BASEPATH: it´s a constant from codeigniter reserved names
  • OR exit prints the string: 'No direct script access allowed'
  • exit — Output a message and terminate the current script.

Other reference: PHP 5 Constants

Upvotes: 7

Related Questions