Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9
  1. #1
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    I'm having a small (huge) problem with return()'s on the site I'm working on. The big hurdle I'm trying to ... err ... hurdle, is returning all the information stored in my user database so that I can recall the info on other pages via a function stored and included from another page (ie. include/functions.php).

    My login assigns a $_SESSION['username'], and I can, without a problem, echo the username using this, but only on the login page. I want to be able to use this globally...which I thought could be fixed if I simply assigned that to an alternate variable via a function. This doesn't work, or at least I can't get it to work.

    My goal is to be able to use a function (ie. fetch_user_data($var)) that I can use the $_SESSION['username'] value (through the variable I assign it to, we'll call it $sess_username), to fetch the user information from the database. How would I be able to go about taking this value and forcing it into a global $variable that I can use anywhere that the session is active?

    (Obviously my goal is to use information (like registration date, user name, etc) and be able to impliment it throughout the "logged in session" portion of the site...like stamping the users name in a greeting tag on the header of the page, etc).
    Owner - http://www.project-guild.com (in development)

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Are you having the necessary session code (at the very start) of each PHP file that needs to use the session?

  4. #3
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    Quote Originally Posted by bfsog
    Are you having the necessary session code (at the very start) of each PHP file that needs to use the session?
    Yes, all of my pages include the session_pagestart() function I wrote (which essentially just starts the session, but I'm going to add some db stuff in the future for tallying members online, and so forth, using the sessions stored in the db).

    For instance, on the login.php page, I can write:
    PHP Code:
    echo "Thank you for logging in" $_SESSION['username'] . "!"
    ...after the login is posted and the session is assigned the username from the $_POST. But, if I assign this to a variable, like:
    PHP Code:
    $sess_username $_SESSION['username']; 
    ...I cannot use it globally because it isn't stored in the global $_SESSION any longer.

    One of the things I've tried is actually using $_SESSION['username'] in my query to fetch the user data (which, in theory, should work just fine), but when I do this...it doesn't return any information from the function that performs the query, for instance:

    PHP Code:
    function fetch_user_data($_SESSION['username'])
    {
    open_db();
    $sql "SELECT * FROM `user_table` WHERE user_name = '$_SESSION['username']'";
    $result mysql_query($sql) or die(mysql_error() . '<br>Error @ fetch_user_data');
    $userdata mysql_fetch_assoc($result);
    return 
    $userdata;
    close_db();

    I get an error in the function to expect a ")" instead of "[" at the opening of the $_SESSION assigned ['username']. And I tried to fix this by making the variable above, but it's not returning any information when I, say, echo "{$userdata['user_name']}" somewhere on one of my pages.
    Owner - http://www.project-guild.com (in development)

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Your problem is that you're misusing the String interpolation :-)

    You want:
    PHP Code:
    $sql "SELECT * FROM `user_table` WHERE user_name = '{$_SESSION['username']}'"
    The {} around the session reference ensures that it's all read appropriately; otherwise, you'll get issues because PHP can't tell that the whole thing is a variable.

    EDIT: Also, you run close_db after returning, but once you've returned, the function's over. Any code after that return statement will never run.

  6. #5
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    Well that explains the error in reading $_SESSION['username']...thanks for that heads up for using curly's.

    One of the things that bothered me is how if you put $_SESSION['username'] into a variable and then use it in an included pages function, it just doesn't work. I've managed to get it to work doing the entire query on each page using this:

    PHP Code:
    $sess_username $_SESSION['username'];

    open_db();
    $sql "SELECT * FROM pg_users WHERE user_name = '$sess_username'";
    $result mysql_query($sql) or die(mysql_error());
    $userdata mysql_fetch_assoc($result);
    close_db(); 

    But it has to be done on the page I want the $userdata on, I can't return it from an included function. Is there a way to do that?
    Owner - http://www.project-guild.com (in development)

  7. #6
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    Correct me if I'm wrong, but you could store the session in a variable outside of the function, and pass it to the function in an argument, right? Or you could do session stuff on the page, then include your files.

    There's probably some security thing about sessions in external files or something like that.

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    This actually isn't a security thing. You can refer to *session* variables outside, or to *global* variables. however, if you declare a variable inside a function, it's a *local* variable, and only exists within the scope of that function (i.e., while that function is running). You can make it global by adding this declaration somewhere in your function before you use the variable:
    PHP Code:
    global $myVariable 

  9. #8
    Junior Member
    Join Date
    Jul 2007
    Location
    Orlando Florida
    Posts
    20
    Member #
    15537
    is it more apropriate, or proper to write the varible in the string. Or is it alright to write the code above as follows-


    PHP Code:
    $sql="SELECT * FROM pg-users WHERE user_name='".$sess-username."'"
    I know both are acceptable (or they both work), but what is considered correct, or more commonly used.

    I tend to use the above, so I can see color shift in the variables that I am inserting. I'm wondering if I should change my ways, as to not upset any other developers who may work on my code.

  10. #9
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    As long as the string is escaped (using the MySQL escaping functions), either way works and there's no real preference as far as I know. Interpolating the variable results in less typing, however, and also less reading and reasoning.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:46 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com