Re: Pushing Vars into $_SESSION

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, April 18, 2006 10:05 am, Chris Grigor wrote:
> Was wondering if there is an easier way to get returned variables into
> the $_SESSION rather than going through each one??
>
> eg
>
> $link_id = mysql_connect($dbhost, $dbuser, $dbpass)
>                 or die("Connection to $dbhost failed on verification
> section");
>
> mysql_select_db($logindb, $link_id)
>         or die("Selection of database failed on verification
> section");
>
> $query = "SELECT * FROM blah where blah = 'blah'";
>
> $result = mysql_query($query) or die ("Query failed: " .
> mysql_error());
>
>         while ($line = mysql_fetch_object($result, MYSQL_ASSOC)) {
>                 $id = $line->id;
>                 $firstname = $line->firstname;
>                 $surname = $line->surname;
>                 $email = $line->email;
>
>
>         }
> mysql_close($link_id);
>
> $_SESSION["id"] = "$id";
> $_SESSION["firstname"] = $firstname;
> $_SESSION["surname"] = $surname;
> $_SESSION["email"] = $email;

foreach($line as $key => $value) $_SESSION[$key] = $value;

But this will do EVERY thing in $line, which may not be what you
really want.

And it brings up some big questions:
  How much do you TRUST your data in the database?
  Cuz if you are just going to blindly cram it into $_SESSION, it
  should really be data you TRUST.

  Why do this?
  Is it really going to help you to have the data stored in TWO places?
  What happens when the database changes from another source, and
  the $_SESSION copy is out-dated?
  What about the other way around, when another part of your application
  use $_SESSION['email'] for something else, or, worse, you hit this
  code again and get some other $id / record in there, but your
  application has a bug where it thinks you still have the previous
  $id / record in the $_SESSION


I predict that if you really think about what you are doing, you won't
want to do this.  There are simply too many pitfalls for this to be a
Good Idea 99.9% of the time.  Maybe you've got that 0.01% situation on
your hands, though...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux