开发者

Want to give a file name in include_once php but with the root address. How?

I am working on PHP with AJAX. Because of AJAX, one can not say that in which directory you are at in a particular point of time. So to call files I can't use a fixed path and filename. So, I want to give path starting from the root to the file. It is working well.

But the problem comes if I want to include a file. Say I want to include a file test.php like:

include_once("http://localhost/sms/test.php");

The file is included but the problem is:

<?php
   $i = 9;
   include_once("http://localhost/sms/test.php");
?>

test.php contains,

<?php
  echo $i;
?>

This code should give output as:

9

...but it gives nothing. I know the reason: the browser is requesting the server via separate HTTP request because path contains "http" so server returns HTML output. so $i doesn't exist: no output.

How do I call files in include using their path from root to run AJAX properly开发者_JAVA技巧?


try require_once($_SERVER['DOCUMENT_ROOT'] . '/sms/test.php');


You should never use hostnames in local file paths. Instead, use $_SERVER['DOCUMENT_ROOT'], like this:

require_once("{$_SERVER['DOCUMENT_ROOT']}/sms/test.php");

This will append something like /var/www (or whatever the root path is) to the start of the file path. Also, using require_once() instead of include_once() is better; if PHP can't find the file to include, it will say so and stop execution, instead of possibly failing silently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜