I recently moved a large CubeCart installation to a new server and it created a whole bunch of issues. Most of them I'm getting sorted out error by error, but I'm stuck on this one:
In the main index.php file the config file is included, which sets key variables like $glob['rootDir']. Then a few lines later in the main index.php file another important script is included and calls some of those $glob variables, but under the $GLOBALS superglobal. However this isn't working. If I put on the second included file var_dump($GLOBALS) it's all there... but if I put echo $GLOBALS['rootDir'] I get nothing.
I'm not getting any bad errors or anything, just Undefined Index.
Is this an issue with PHP settings? The site was working fine before on the previous server, though I don't know what version of PHP it was running or specific settings.
Code samples:
MAIN INDEX FILE:
//INCLUDE CORE VARIABLES & FUNCTIONS
include_once("includes/global.inc.php");
//... other stuff ...
include_once("includes/sessionStart.inc.php");
GLOBAL.INC.PHP:
$glob['dbhost'] = 'localhost';
//other variables, including $glob['rootRel'];
SESSIONSTART.INC.PHP:
$sessionDomain = substr($GLOBALS['rootRel'],0, strlen($GLOBALS['rootRel'])-1);
//the above is where it throws Undefined Index
Thanks!
$glob and $GLOBALS are different variables, so this behaviour is ok.
Related
My PhP files contain some long string constants and I'm trying to factor them out. So I created "my_string_constants.php" and used include in header.php. So far that works fine.
Now another file, page.php also requires the string constants and header.php. The scheme below tries to clarify these dependendies.
The string constants now seem available in my header only, not the rest of my page. I tried to resolve this by adding global ... to each string constant in string_constants.php. This resolved the error of "Unknown variable" but my string constants still seem unavailable to most of the page content.
What's the right way to get this working?
UPDATE
The issue's been solved. I should have used define(myString instead of $myString = .... By doing so, I need just one include in header.php and the constants will be available to page.php as well.
Thanks a million, you guys are great.
One thing you would want to do is distinguish a constant from a variable. Especially if other developers end up working on this, they will be confused by your terminology.
For constants, you do not need to declare them as global and they are defined like so:
define('MY_CONSTANT', 'Value');
It seems to me that the constants file is acting as your site wide configuration file, so to me, it makes sense to have that on every page, regardless of whether header is used or not. I would normally create a bootstrap file for this purpose.
bootstrap.php:
<?php
require_once(__DIR__ . '/constants.php');
require_once(__DIR__ . '/database.php');
require_once(__DIR__ . '/session.php');
You get the point, then every accessible page needs to include this bootstrap file and then possibly the header.
page.php:
<?php
require_once(__DIR__ . '/bootstrap/bootstrap.php');
require_once(__DIR__ . '/header.php');
?>
<h1>Page Title</h1>
In header.php, since it requires these constants, you can handle this in two ways, either check that the constants are defined (meaning, bootstrap was included first) or just use another require_once to make sure that file was loaded.
if (!defined('MY_CONSTANT')) exit('Bootstrap failure');
or
require_once(__DIR__ . '/bootstrap/constants.php');
Going to many directories or "files" deep regarding includes can really cause issues later when you are trying to debug. As a rule I try to only go one level deep in regards to including files. I.e. Create a folder called includes and place everything in there. If there is a file that needs multiple variables, functions etc, then include them in the needed pages at that point doing several includes like so:
<?php
include("includes/header.php");
includes("includes/functions.php");
?>
There are also other issues in regards to having multiple includes, like if you have sessions or cookies some LAMP stacks will require you to declare
session_start();
at the top of every page including all included php files that may need access to that session or cookie.
So to answer your question I believe the simplest solution would be to re-organize your site or script.
in the header page ontop u write
include 'header.php';
and in header.php you write
include 'my_string_constants.php';
so in this case the page.php calls the header and in the header the my_string_constants is being called...is this what you mean?
This is a newbie question, and I know it.
Template structure is your usual index.php, with a few require_once()'s for the header/footer etc.
I define a var at the top of index.php before any of the require_once()'s for the base url, such as $url = 'http://url';
I then want to echo this out into all template files, header/index/footer etc, it works inside index.php as expected, but fails with a undefined var in all template files that are included in.
I know it's a var scope issue, but I'm totally perplexed how to fix it.
I'm aware that the manual says vars are available to included files, however they aren't. Could it be a issue with my local PHP install?
edit : Created a couple of test files, and a var is defined between 2 files, so why are they not working on my main site files?
Any helps gracefully recieved.
Many Thanks
if you use functions or methods (functions in classes) then you need to do global $variable inside the function. Otherwise you will not have access to it, you also could define it as constant. A constant is always global.
define('MYURL', $url);
You might want to use a PHP framework, if you not already do so.
I was wondering if it possible to add constants to php before any scripts are ran, thus on startup. If this is possible, could it be done with classes etc aswell?
I was thinking in the direction of creating a plugin for php but maybe there is a way simpler way.
I don't mean including a file in every script.
thanks in advance
Not constants as far as I'm aware, but this is ok:
.htaccess
SetEnv MYVAR "hello"
somefile.php
echo $_SERVER['MYVAR'];
See the Apache docs on SetEnv for more.
To directly answer the question, there are two approaches:
Use auto_prepend_file to auto include a PHP file that has define calls.
Configure your web server to set server variables.
I think the second is a better approach. However, I don't see how either of them are very useful in the context of a plugin. Usually a class autoloader of some sort is the way to go there, or to require a single include file.
If I understand your question correctly, what I do is to include a file before all else on my index.php. That same file contains tons of constants, control verifications, initialization for the DB object, etc...
e.g.,
INSIDE index.php
<?php
$moduleRoot = dirname(__FILE__);
require_once($moduleRoot."/components/inc/inc.php");
// continue to render the web page and perform as usual
?>
INSIDE THE inc.php
// When in development, all errors should be presented
// Comment this two lines when in production
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Website id for this project
// the website must be present in the table site in order to get
// the configurations and records that belong to this website
define("CONF_SITE_ID",1);
// Domain path where the project is located
// Should be like the access used on the browser
$serverDomain = $_SERVER["HTTP_HOST"];
$serverAccess = (!empty($_SERVER['HTTPS'])) ? ('https://') : ('http://');
$serverRoot = dirname(__FILE__);
define("CONF_DOMAIN", $serverAccess.$serverDomain);
// etc ...
EDITED
Since you have multiple "startup" files and you need all of them to call inc.php, the best choise seems to be .user.ini as of PHP 5.3.0, you can read about it here!.
And an article on the subject.
As the title suggests, I am having problems trying to move an existing site over to a new host.
I have edited my .htaccess find to point the php.ini session.save_path value to a new folder stored in my non public root.
This is working fine, I can see the sessions appear in this folder, with the correct entries written to them.
But for some reason, my scripts cannot make use of these sessions, as in the variables associated to them hold no value, as in, they come out blank.
Now, these scripts are in use on my old host and do work perfectly. And comparing the actual session data, once the files have been downloaded off each host, they are both exactly the same.
This leads me to think that this could be a server side issue. Possibly another php.ini value.
Has this happened to anyone before, or can anyone suggest a reason behind this kind of behavior.
It anyone has absolutely any input regrading this, or could point me in the right direction as to solve this issue. It would be more than greatly appreciated.
Thank you!
#Marc
sess.php
<?php
session_start();
$_SESSION['test'] = 'test';
include 'sess2.php';
?>
sess2.php
<?php
echo ''.$test.'';
var_dump($test);
?>
session data file value
test|s:4:"test";
Now when I load sess.php it includes sess2.php but the page only displays the vardump which is NULL. This is odd because the data has been written to the session as shown in the downloaded data file value...
Any ideas?
Looks like you're depending on register_globals. That's a hideously BAD thing in PHP which defaults to off these days. Try echo $_SESSION['test'] instead now. As well, such variables are only registered at script startup time/session_start. You'd need to use session_register() (DON'T) to make it take effect during the current execution run
I have the following problem: in index.php, I have set the variable $activelang. I
$activelang = active_language ();
echo $activelang; //works perfectly
Later in my index.php code, I include a new php file.
include ('myotherfile.php');
If I try to use $activelang in myotherfile.php, it does not work! I am not using it inside a function, and I tried using global $activelang and it doesnt work either
All of these is happening in a wordpress install, but the code I am talking about is plain php. I am using php 5.3
Why is this happening? As I undestand it, the include works as a copy paste in my main file, so I shouldnt be having any problems with variable scope, right?
include should be used to include files, not some network resources.
if you bother to run the code you posted here you'll be surprized, as it will print $activelang all right.
When working with WordPress templates you should create functions in your functions.php file for handling includes and then call those functions in your templates.
So in functions.php you should have:
function myOtherFile() {
include('myotherfile.php');
}
And in index.php call myOtherFile()
Now, if you still have trouble with scope try this:
function myOtherFile($activelang) {
include('myotherfile.php);
}
And then in index.php do this:
$activelang = active_language();
myOtherFile($activelang);
If that doesn't work then I think your problem is being caused elsewhere. Because like Col. Shrapnel said, the code you posted here works.