Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No direct script access when using include

Tags:

include

php

I have a PHP powered CMS website. I'd like to include database.php in a PHP script. database.php is in the config folder and has the db connections details (pass, db_name etc).

Here is the script:

<?php 
  echo __DIR__ ."<br /><br />"; 
  //make sure your assumptions on directories and path are correct for the 
  //include below

  include '../application/config/database.php';

?>

Running the script, I get this message:

/Applications/XAMPP/xamppfiles/htdocs/tslocal/textbook_scripts

No direct script access.

On the first line of database.php is this line:

<?php defined('SYSPATH') or die('No direct script access.');

I'm guessing that the CMS has somehow "protected" the config file which is preventing me from including it. What is "No direct script access"? Google gives me lots of examples of people seeking to add this functionality. I wish to remove it. Possible? Likely? How do I tell PHP to let me access database.php?

like image 735
Doug Fir Avatar asked Aug 22 '13 15:08

Doug Fir


2 Answers

The basic way for going about this is somewhere in the application (prior to the loading of database.php) there is a line something along the lines of:

define( 'APPLICATION_LOADED', true );

In the database.php there is a check being performed against this, similar to:

if( !defined('APPLICATION_LOADED') || !APPLICATION_LOADED ) {
    die( 'No direct script access.' );
}

Look in database.php and whatever files it includes to determine how it is checking to see if the script is being directly accessed or not. Then you can mimic the conditions necessary if you would like to include that file in your script.

like image 112
Jeff Lambert Avatar answered Nov 01 '22 04:11

Jeff Lambert


I got this PHP error from Codeigniter's Email.php class called CI_Email:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CI_Email {
   var $smtp_default_from = '';
   var $smtp_default_name = '';
   var $useragent = "CodeIgniter";
   var $mailpath  = "/usr/sbin/sendmail";

   //more code omitted

}

What I did was try to include this class directly like this:

<?php
    include("Email.php");
?>

And when I run it, it says:

No direct script access.

The PHP method "defined" checks whether the given constant exists and is defined.

So for my particular class it says that BASEPATH must be defined. So if you define it like this:

<?php
  define('BASEPATH', "foobar");
  include("Email.php");
?>

Then the error is no longer thrown. But then we have to wonder why the developers put in this particular restriction and what will happen if we bypass it.

like image 5
Eric Leschinski Avatar answered Nov 01 '22 02:11

Eric Leschinski