开发者

PHP global path setting

开发者 https://www.devze.com 2023-03-24 03:13 出处:网络
I have this setting. root dir| index.php config.php file.php| scripts|a.js account |index.php member开发者_StackOverflow|index.php

I have this setting.

 root dir| 
       index.php
       config.php
       file.php  |
                 |
                 |scripts|a.js
                 |
                 |account
                         |index.php
                         |         |
                         |member   开发者_StackOverflow|index.php
  • Now, I've included index.php of member dir into index.php of account dir. Also , the account index.php includes the config.php which contains,

    define( 'PATH', (__DIR__) );
    

Now , for all includes in account index.php I use,

require_once( PATH . '\file.php' ); 

and is working properly. But when I try to add the path for script src such as,

<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>

I get an error, so how can i include the a.js in scripts folder into index.php of account using the globally defined path.

Thanks.


The PHP "__DIR__" and "__FILE__" are absolute to the server. You shouldn't need to use either for your script.

<script src="/scripts/livevalidation.js"></script>

Also, your PHP looks like it has some syntax errors, this would be correct (although still wouldn't work:

<script src="<?php echo PATH.'/scripts/livevalidation.js'; ?>"></script>


You're missing a print or echo statement in the PHP statement in your script tag. You're also placing the concatination periods in the wrong place. On top of that, however, the JavaScript you're trying to include doesn't need to be in the PHP statement.

All that said, the final line should read like the following:

<script type="text/javascript" src="<?php echo PATH ?>scripts/livevalidation.js"></script>

On top of that, however, I don't think the above will work as you expect. __DIR__ outputs a server-side filesystem path, which wouldn't make sense when importing JavaScript over HTTP. I'd recommend something more along the lines of the following:

<?php define('URL_ROOT', '/'); ?>

<script type="text/javascript" src="<?php echo URL_ROOT ?>scripts/livevalidation.js"></script>

In the above example URL_ROOT would point an absolute URL under which your static media (CSS, JavaScript, and so on) is served.

0

精彩评论

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