SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to make sense of global variables


    I thought I had been doing this all along without any problem:

    extract($row)
    . . . when $row is an array built from . . .
    $row=mysql_fetch_array($result_of_query,MYSQL_ASSOC);
    . . . and when I have a session_start() at the top of the page . . .

    But now when I do this, I get this error:
    Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled.

    It happens when I try to extract the array, $row, when the page starts with a session_start().

    So now I'm confused. I've been familiar with making variables global in a function so they can be use by the rest of the program. Is $row in my script a global variable despite never having declared it as such because it was made an array by a function? I've tried building a new array from $row, but it's still causing the error. The error does not occur only if I do not use session_start().

    Is $_SESSION a global variable by nature -- despite not being declared as such? Guess that would make sense, but why does the session extension give a damn what I'm doing with $row when I'm not trying to put any value from $row into the $_SESSION variable or even use any value from $_SESSION?

    I've read as much as I can on this particular error, and I understand just enough to know that it would be best not to enable registar_globals for security reasons, but I still don't understand why what I am doing is causing this error.

    Thanks for any ideas, leads or links.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,345
    Mentioned
    192 Post(s)
    Tagged
    5 Thread(s)
    In PHP 4.2.x there was a change to the defaults (for security reasons).
    The most controversial (at least, the one with the most impact on the PHP community) was register_globals.
    This article has all the detail you should need.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    All the fields names starting $_ are global arrays.

    The variable you are trying to use is $_SESSION['row']
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both!

    I've been reading up on the directive for register_globals as well as information on the super globals in general and $_SESSION in particular. Things might be clearing up for me if I could understand what felgall wrote about how the variable I am trying to use is $_SESSION['row']. If I am not adding $row to the $_SESSION array at any point, how does $row become $_SESSION['row']? And if it is part of $_SESSION, how come it doesn't appear if I print_r($_session) or var_dump($_session)?

    I keep looking through the manual to see if there is something that says something about a variable created after session_start() is somehow made to be global, but everything I read seems to repeat that registering the variable (which is n/a 'cause register globals is off, right?) or adding it to $_SESSION is the only way a variable becomes part of the session super global.

    What, pertaining to this, am I not getting?

    Thank you!

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Where is the value in $row coming from? If it is set in the current page then it should still be called $row. If it was set in a prior page then it will need to have been passed in one of the global valiables such as $_SESSION, $_COOKIE, $_GET or $_POST or the current page will not be able to access it at all unless you change the register_globals setting to make all variables global (and hence have less control on what values can be passed into the page).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it's set in the current page. That's why I'm so confused by this.

    $row is given values by a query:
    Code:
    $row=mysql_fetch_array($result,MYSQL_ASSOC);
    Here basically what I've got:

    Code:
    <php? session_start();?>
    <html><body>
    <?php
    include("$the_connection");
    $query="SELECT * FROM cp_rec_list_items";
    $result=mysql_query($query);
    mysql_close($connection);
    $row=mysql_fetch_array($result,MYSQL_ASSOC);
    extract($row);
    ?>
    </body></html>
    Here's var_dump($_session):
    array(6) { ["username"]=> string(19) "address@gmail.com" ["user_id"]=> string(2) "21" ["list_id"]=> string(2) "35" ["group_1"]=> NULL ["group_2"]=> NULL ["loginvalid"]=> int(1) }
    Last edited by 8080a; Oct 18, 2007 at 14:17. Reason: added connection include

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    I can't see in that code where you establish the connection to the database. Is the code to do the connection using something passed from another page?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops . . . just added it back in. For some reason this morning I thought I should leave it out of the post. I had been up for only about a minute. (Thanks for taking a look.)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •