dqdz6464 2014-08-03 13:43
浏览 35
已采纳

我可以在PHP中存储一个可以在所有文件中访问的变量,而不包含另一个文件吗?

The idea is to store the base address of the Test Website, which is essentially a subfolder within the main domain (eg: www.mydomain.com/mywebsite/). It is not difficult to get this string value. I just have to use $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'] in the index page.

But what I want is to store this value and use it to include other files. So it doesn't make sense to include a file storing the variable to get the full path. I want to use it as, something similar to $_SESSION["someName"]. The problem with this is that, for security reasons, I wrapped the session_start() in a function secure_session_start(), which is in another file and needs to be included before being called.

So, in short, I need to get the value of this variable before even a single file is included() or required(), after it was once set from the 'index.php' page.

If I could use the session_start(), It would have been possible to store in the $_SESSION. But I can't...

EDIT:-

Here's the hierarchy of files:

enter image description here

Now here Public_html points to WWW.MYDOMAIN.COM. That is my website.

I am currently creating a website for my client: WWW.CLIENTWEBSITE.COM. But I'm hosting it only after the website is completed. But for now, the client can see the progress through, WWW.MYDOMAIN.COM/CLIENTWEBSITE/

So when I'm developing the website, I need to either use 'relative paths' (eg: ../somefolder/something.etc) or I can use the 'WWW.MYDOMAIN.COM/CLIENTWEBSITE/' to prefix the url's for include, require, etc. But it will change, once I host it on their domain to WWW.CLIENTWEBSITE.COM. But I don't want to end up changing each and every url's in all the pages. So I want a variable with somewhat a super-global scope which can store this base-url, that I need to change it only once.

I've been experiencing few problems with Relative paths. So, I want to use the full URL to link to a file. That is the reason why I had this question raised.

Problem I'm facing on Relative Paths: (This is not the actual question, but I'm extending it)

index.php

<?php
include_once 'includes/bootstrap_functions.php';

/* ...functions following it... */

bootstrap_functions.php

<?php
include_once "../common/dbhandler.php";

function someDBFunction(){
    global $DBH;
    .
    .
    .
/* ...functions following it... */

dbhandler.php

<?php

include_once "psl-config.php";

$DBH = new PDO("mysql:host=".HOSTNAME.";dbname=".DBNAME, DBUSER, DBPASS);

/* ...functions following it... */

psl-config.php

<?php

define("HOSTNAME","localhost");
define("DBNAME","somename");
define("DBUSER","someuser");
define("DBPASS","somepassword");

And then when I run index.php, I get this:

enter image description here

  • 写回答

4条回答 默认 最新

  • dougou6213 2014-08-03 19:21
    关注

    Lots of scripts use a constant for the base URL/path of the original script. Put this before your includes:

    define( "URL", $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'] );
    

    Now you can use use within those files, and they can't change it.

    $img = URL . "/image.png";
    

    I'm not sure HTTP_HOST and SCRIPT_NAME are correct for your usage, but that was not the question.

    Note: Don't use sessions/cookies to store this. It's just weird. Unless the path was random. If you need this same variable on multiple scripts to be the same, you need a structural change. Consider what might happen if the user lands on a page other than the front page.

    UPDATE

    A problem was introduced that the script which is defining the URL is not actually in the "document root", and in fact the domain changes from localhost to example.org.

    Note: I am unable use mydomain dot com in this answer. Using example.org instead!

    If your script is in a subfolder, you have two options. Navigate up two folders using dirname or remove the subfolder via str_replace. The first option is less likely to break if you rename your folders/scripts in the future.

    Input: http://example.org/subfolder/include/common/common_functions.php
    Target http://example.org/subfolder/
    

    Option 1: Up two folders (and strip filename)

    $siteurl = 'http://' .  $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
    $siteurl = dirname($siteurl); // removes "common_function.php"
    $siteurl = dirname($siteurl); // removes "common/"
    $siteurl = dirname($siteurl); // removes "include/"
    define( "URL", $siteurl ); // result: http://example.org/subfolder/
    

    Option 2: Removing relative filename

    $siteurl = 'http://' .  $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
    $siteurl = str_replace('include/common/common_function.php', '', $siteurl);
    define( "URL", $siteurl ); // result: http://example.org/subfolder/
    

    In both options, we use $_SERVER['HTTP_HOST']. By this answer's definition, HTTP_HOST is the hostname that the visitor sees. This should be localhost for you, and example.org for your client. I encourage you to test this yourself.

    In both options, the use of $site_url is completely optional, so that it is easier to read. You can combine these if you don't like clean code:

    define( "URL", dirname(dirname(dirname('http://' .  $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] ))) );
    

    Paths VS URLs

    My above examples are using URLs. These are what you send to the browser, especially for images, javascript and CSS files. eg: <img src="<?php echo URL; ?>/images/my-image.png">.

    If you are including other scripts, you do not want to use a URL. You want a path instead.

    $sitepath = __FILE__; // /var/www/example.org/include/common/common_functions.php
    $sitepath = dirname( $sitepath ); // removes common_functions.php
    $sitepath = dirname( $sitepath ); // removes common/
    $sitepath = dirname( $sitepath ); // removes include/
    define( "PATH", $sitepath ); // /var/www/example.org/
    

    Using your path may not even be necessary, but you may define it:

    include( PATH . '/include/common/another-script.php' );
    // Alternatives relative to current script:
    include( 'another-script.php' ); // When the current script is in the same folder
    include( 'common/another-script.php' ); // Current script is in the "include" folder
    include( '../rare/another-script.php' ); // "rare" folder is next to "common", current script is in "common"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧