• Resolved reachingout

    (@reachingout)


    In my wp-config.php file I am trying to create a global variable like this:

    global $DBAA;
    $DBAA='S(89ydeT5a';

    In another php file in my themes folder I am trying to acces this viariable but is empty…
    echo $DBAA;
    gives nothing.

    • This topic was modified 2 years, 4 months ago by Jan Dembowski.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You have to declare global in every scope where you want to use it. Do so again before echoing and you’ll see the value output. If you use the global in several different functions, you’ll need to declare it in each function because every function has its own local scope.

    Unlike many languages where you only need to declare globals once, PHP is quirky in this respect.

    Thread Starter reachingout

    (@reachingout)

    Actually S(89ydeT5a is my database password so I do not want to show if everywhere, but on the other hand maybe no one can access my php-files anyway…?

    • This reply was modified 2 years, 4 months ago by reachingout.
    Moderator bcworkz

    (@bcworkz)

    Yeah, if some outsider can read your PHP source code your entire site is compromised anyway. In that case exposing your password is not your biggest concern ?? I hope that’s an example password and not your actual one. Even though we don’t know where your DB is, you ought to change it if it’s your actual password. By now it has made its way onto lists hackers use to brute force their way into sites.

    The WP DB password is assigned to a constant, which is global in scope by its nature. Instead of using a global variable, you could do define('DBAA', 'S(89ydeT5a');
    Then when needed $mysqli = new mysqli("localhost","my_user", DBAA,"my_db"); or whatever.

    You cannot redefine constants within the same request, but there would be no need to do so for passwords. FWIW, it’s considered by many to be poor programming practice to rely upon global variables. Only to be used when there is no other good option. OTOH, WP makes extensive use of them.

    Thread Starter reachingout

    (@reachingout)

    Thanks, I will try with
    define('DBAA', 'S(89ydeT5a');
    (No it is not my real password)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to create a global variable but fail.’ is closed to new replies.