0

can I use a string from file A in the function from file B in php?

File A:

<? 
    $_CONF['db_host'] = "XXX.XXX.XXX.XX";
?>

File B:

<?
    require_once "A.php";
    function test() {
        echo $_CONF['db_host'];
    }
?>

If I use the test() function nothing happens.

I get the error:

Notice: Undefined variable: db_host in /path/to/file/B.php on line 4

chris85
  • 23,846
  • 7
  • 34
  • 51
luguhe
  • 25
  • 7

1 Answers1

0

you can set variables and get them using session or cookie. Also dont use short tags in php it has been deprecated. Use full php tags

To pass a variable using session you can do this: (Not Recommended to store database config) In file A you do this

$_SESSION['db_host'] = "XXX.XXX.XXX.XX";

In file B you call like this

$db_host = $_SESSION['db_host'];

YOu can also do through cookies (Not Recommended for sensitive data)

In file A you do this

$_COOKIE['db_host'] = "XXX.XXX.XXX.XX";

In file B you call like this

$db_host = $_COOKIE['db_host'];

You can also use GLobal variables but that is a risky.

Best way is to define variables inside a class and include the class inside your files where you need those variables.

So in File A you can set the config

<?php
 class Config {
        public $host = DB_HOST;
        public $dbuser = DB_USER;
        public $dbpass = DB_USER_PASSWORD;
        public $db = DB;
 }
?>

In file B

include_once("A.php");

$dbnew = new Config;

Then access the variables this way

$host = $dbnew->host;
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • Can anyone explain the reason for downvote? – Amit Ray Jul 09 '16 at 19:00
  • not sure why ,so i will upvote =) – Maksym Semenykhin Jul 09 '16 at 19:05
  • Thanks @СеменихинМаксим. Ethically they should give a reason, so that if I have mistake I can improve. I can also make mistakes. – Amit Ray Jul 09 '16 at 19:07
  • i think downvote was because it should be like config array so it is not good to store it in session especially in cookie. Static Object or singleton is a way to do it – Maksym Semenykhin Jul 09 '16 at 19:13
  • @СеменихинМаксим I can understand. I took it as general one passing a variable to another page. But security wise their are definitely other options like Joomla does by writing configuration data in a file configuration.php with permission 444, that is not writable from outside. – Amit Ray Jul 09 '16 at 19:18
  • Sorry @AmitRay, the reason for the downvote is because you're trying to have him use things like cookies, when the question is pretty specific. He's trying to include another file, and get that variable in a function. This solution is not only bad, but it causes this variable (Like $_COOKIE) to be sent down to the client. Imagine if this was a PASSWORD! Also, your other solution(`$_SESSION`) causes unnecessary information to be stored in between requests, and should not be used in this fashion. It's mainly used to store user data, not configuration options. – Blue Jul 09 '16 at 20:54
  • @FrankerZ Thanks for that info. As stated earlier I took this question as passing variables from one page to another. I didnt take it as db config. But as per your suggestion I have updated the answer for passing database variables. – Amit Ray Jul 10 '16 at 03:18