virtualhost - PHP absolute path not working -
i have set simple virtual host.
just added these lines @ bottom
<virtualhost *:80> serveradmin webmaster@yourdomain.com documentroot "/var/www/reminder/" <directory "/var/www/reminder"> allowoverride require granted php_admin_value open_basedir "/var/www/reminder/" </directory> php_admin_value open_basedir "/var/www/reminder/" servername test.mydomain.com errorlog "logs/yourdomain.com-error_log" customlog "logs/yourdomain.com-access_log" common </virtualhost>
what want is, while including files in php files inside reminder folder, if give absolute path , "/test/" , should consider reminder folder root, , search test folder inside reminder.
right now, when type echo dir , shown /var/www/reminder.
my file structure is:
/var/www/reminder/database/db_connect.php /var/www/reminder/registration/registration_handler.php
this db_connect.php has :
<?php define('dbhost','localhost'); define('dbuser','root'); define('dbpass','password'); define('dbname','reminder'); $conn=mysqli_connect(dbhost,dbuser,dbpass) ; if(!$conn) { echo mysqli_connect_error(); } else { echo "connected successfully"; } ?>
this registration_handler has :
<?php echo "hi"; echo __dir__; require_once("/database/db_connect.php"); mysqli_query($conn,"insert users () values ()") or die (mysqli_error($conn)); ?>
i believe, if fine, should able "connected successfully" message (from db_connect) , if run registration_handler.php , thats not happening.
when include same file :require_once("../database/db_connect.php"); , output
the open_basedir
option restricts paths allowed access php code. not affect way file lookups inside php code works.
this means setting open_basedir
/var/www/reminder/
cause error if try open example /etc/passwd
(because outside of allowed allowed directories). not transform open of /etc/passwd
/var/www/reminder/etc/passwd
.
what want instead use __dir__
constant in order open relative path. example, load /var/www/reminder/database/db_connect.php
/var/www/reminder/registration/registration_handler.php
, use like:
$basedir = dirname(__dir__); require_once($basedir . '/database/db_connect.php');
we use dirname()
strip away last path component end of __dir__
. i.e.:
__dir__
:/var/www/reminder/registration
dirname(__dir__)
:/var/www/reminder
Comments
Post a Comment