Accessing global variables in a separate PHP script? - php

I'm trying to import some variables from a PHP script. It seems simple but I cannot get it to work.
The script contains some global variables like that:
$server_hostname = "localhost";
$server_database = "kimai";
$server_username = "root";
$server_password = "";
$server_conn = "mysql";
$server_type = "";
$server_prefix = "kimai_";
$language = "en";
$password_salt = "7c0wFhYHHnK5hJsNI9Coo";
Then in my script, I would like to access these variables, so I've done:
require_once 'includes/autoconf.php';
var_dump($server_hostname);
But this just outputs NULL. I've also tried:
require_once 'includes/autoconf.php';
global $server_hostname;
var_dump($server_hostname);
but still not working.
I've added some echo statements in the "autoconf.php" file so I know that it's being loaded.
Any idea how I could access these variables?

You have to define the variable as global first:
global $server_hostname;
$server_hostname = "localhost";

It turns out the file was included somewhere else in the application so when I was calling require_once, the file was not being included at all. I changed it to just require and now it works.

Maybe the file was not included properly.
require_once 'includes/autoconf.php';
check current work directory where you include autoconf.php
try this
if (file_exists('includes/autoconf.php')) require_once 'includes/autoconf.php';
else echo 'File not exists';
to check it out.

How about using a constant?
define("server_hostname","localhost");
define("server_hostname","localhost");

If you include the file and the variable is in a plain text, and not inside a function / class it works without the global
Go to your php.ini and put display_errors=On and errors to E_ALL so you will see which is the correct reason

This is evil, but it may get the job done.
<? //PHP 5.4+
\call_user_func(static function(){
$globals = \get_defined_vars();
include 'includes/autoconf.php';
$newVars = \array_diff_key($globals, \get_defined_vars());
foreach($newVars as $name => $value){
\define($name, $value);
}
});
//Variables defined in file are now constants!
?>

better way to use and correct global variables is first assign value to variable and then declare global.
this is:
$server_hostname = "localhost";
global $server_hostname;

Related

Can't pass variable from one file to another using $GLOBALS array [duplicate]

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

PHP session variables getting lost after a header redirect

I cannot figure this out. Sometimes after the redirect (see code below), the session variables are lost. Any ideas?
Note the script is initially called with ?p=1&u=2&k=3.
As you can see, the script redirects to itself. The session variables something are lost after the redirect.
<?php
session_start();
if ((isset($_SESSION['p'])) and ($_SESSION['p'] != "")) {
// do something
} else {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
header("Location: http://".$_SESSION['w'].$_SERVER['PHP_SELF']."");
exit();
}
?>
Cheers
Copied and pasted your code and it works just fine for me.
Do you maybe have some spaces or whatever before your <?php-tag?
I am not sure why it happens.
Probably you have some misconfiguration on your php.ini file.
Or you don't have the right session.save_path or permissions to write there.
But if the problem persists, try this way:
<?php
session_start();
if (!$_SESSION['p']) {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
}
//code comes here
?>
In my opinion, this is the way things should be done.

How to access a variable across two files

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

Having Difficulty passing variables through includes using globals

I have a three tier tree for displaying content on a page. It uses includes to display specific PHP pages based on the URL. What doesn't happen is the variables are not understood in included PHP files.
index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
page2.php
global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;
There is no need at all for those $global lines. Any variables defined in the main script are defined in the included file. It's basically like taking the code in the included file and shoving it in the place of the include call (with a few exceptions)
This line:
include_once 'page2.php;
should change to:
include_once 'page2.php';
You have a missing quote.
Have you tried removing ALL those four global lines? I don't know if that's the problem, but they're not necessary at all!
When including or requiring a file all the variables declared above are available to the included/required file.
If that didn't solve it, maybe you have the wrong path on include.
index.php
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
page2.php
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
global example
function dosomethingfunky(){
global $user, $color;
echo 'hi '.$user.'I hear you like '.$color;
}

how to set variables in a php include file?

I have a set of variables that are set outside the target php include file, how would i set them in the target php include file. Example:
<?php
$fname = "david";
?>
Now how would I set $fname in another php file?
In your other PHP file you would do:
<?php
$fname = "david";
?>
This answers your question directly, but I would hazard a guess that you actually want to have access to variables that are set in a file that is included into your current file or something along those lines.
So
File1.php:
<?php
$fname = "david";
?>
File2.php
<?php
require_once 'File1.php';
echo $fname;
Would result in david being printed to screen.
Just use it directly.
$fname = "your value";
The PHP docs have an article about variable scopes.
The global variable scope is shared across all included and required files. In your example, once you've defined $fname globally, all other PHP lines executed afterwards can access $fname even if they're in different files.
Example: if a.php is:
<?php
$fname = "david";
?>
and b.php is:
<?php
$fname = "sarah";
include 'a.php';
// $fname is now "david"
?>
then executing b.php would define $fname as "sarah", then redefine it as "david" (through a.php).

Categories