Where to put global scope statement?

Discussion in 'PHP' started by gfreeman, May 15, 2007.

  1. #1
    I have just created an account with a new hosting company, and started some php from scratch.

    The following code outputs "Hello " because I have not put any global statements in there, but where to put them?

    index.php:
    <?php
    require 'authentication.inc';
    echo"Hello $auth_name";
    ?>
    Code (markup):
    authentication.inc:
    <?php
    require '1.inc';
    require '2.inc';
    getUserInfo("1"); //A simplified version of the real thing ...
    $auth_name=$userdata['username'];
    ?>
    Code (markup):
    1.inc:
    <?php
    function do_sql($sql) {
    $conn = mysql_connect("localhost","mysqlusername","password");
    mysql_select_db($db, $conn);
    $result=mysql_query($sql, $conn) or die(mysql_error());
    }
    ?>
    Code (markup):
    2.inc:
    <?php
    function getUserInfo($id) {
    $sql="SELECT * FROM users WHERE id='{$id}'";
    $result=do_sql($sql);
    $userdata=mysql_fetch_array($result);
    return $userdata;
    }
    ?>
    Code (markup):
    This is a cut down (obviously) from the heaps of code already there, but the idea is that every page on the site will call 'authentication.inc' which goes off to check a few things, then sets up variables that are passed to the page that's calling it. 'authentication.inc' relies on 1.inc and 2.inc for functions.

    It's late, my brain has faded and given a clear head I could see through this, but please - how do I get "Hello Foo" to appear, given that the mySQL database has an entry "id=1, username=Foo"?
     
    gfreeman, May 15, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Don't use .inc as file extension, because all MySQL information such as username and password can be viewed by simply opening the page in the browser. Change the extensions to .php.

    This is not going to fix your problem, but you should keep this in mind.
     
    nico_swd, May 15, 2007 IP
  3. gfreeman

    gfreeman Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not a problem - my server is set up to deny direct access:
    see us3.php.net/manual/en/security.php
     
    gfreeman, May 15, 2007 IP
  4. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nico is still right, if you were to ever move servers and forget you would be out of luck. It's not hard to rename them and will save you grief in the future :)
     
    CodyRo, May 15, 2007 IP
  5. gfreeman

    gfreeman Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, that aside - any input on the global issue?
     
    gfreeman, May 16, 2007 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This isn't really a global scope issue. In 2.inc you have the getUserInfo function that is returning an array of data, but you're not storing it when you call the function.


    in authentication.inc:
    getUserInfo("1"); //A simplified version of the real thing ...

    should be:
    $userdata = getUserInfo("1"); //A simplified version of the real thing ...


    I believe...
     
    TwistMyArm, May 16, 2007 IP
  7. gfreeman

    gfreeman Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Duh.

    That worked. How could I have overlooked that?

    Thanks for making a blind man see again :)
     
    gfreeman, May 16, 2007 IP