Forum Moderators: coopster

Message Too Old, No Replies

Global keyword safe?

         

HoboTraveler

6:17 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Hi All,

In a php script (foo.php), I include a file (db_connection.php) that contains the db connect info.

I call a SELECT statement through a function in the foo.php file. Instead of passing the db connect variable to the function, is it ok to use the global keyword for the db connect in the function itself? or would this create a security issue?

Is it safer to pass a variable as opposed to using the global keyword?

eg:

// Contents of foo.php
require (db_connection.php)
require (select.php)

$get_contents = get_contents($ID);

// end foo.php

// contents of select.php
function get_contents ($ID)
{
global $db_connect_var;

// execute select statement
}

// end select.php

ahmedtheking

7:29 pm on Jun 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do this all the time. I've never thought of it as being 'unsafe' but don't quote me on that! I've always defined my connection, eg: define(LINK,mysql_connect());

eelixduppy

3:51 am on Jun 23, 2007 (gmt 0)



It doesn't make it insecure. The only way it would is if there was some other vulnerability in the script. It is only global to the script, as if you were to define it at the top; nothing crazy :)

[php.net...]