Jump to content

Archived

This topic is now archived and is closed to further replies.

mewhocorrupts

Functions and $_SESSION

Recommended Posts

I'm creating a site that utilizes the $_SESSION global heavily.  I understand, in general, what it takes to make proper use sessions, insomuch as initialization, registering variables, and then assigning them.  My question lies in whether or not it is possible, and if so, safe from a security point of view, to call a function from an included file that uses values stored in the user's session.

For example:

[code]
<?php
session_start();

// Here we have some DB work, but the final product is the index of the user's record in MySQL...

$ret = setLoginDBVars($index);
?>
[/code]

And in the included file:

[code]
<?php
function setLoginDBVars($uid)
{
    $db = new mysqli($GLOBALS['DB_SERVER'],$GLOBALS['DB_USER'],$GLOBALS['DB_PASS'],$GLOBALS['TBL_USERS']);
    $dbq = "SELECT ";  // I'll have to get this from the DB structure.
    $res = $db->query($dbq);
    $row = mysqli_fetch_array($res, MYSQLI_ASSOC);
   
    foreach($row as $key => $val)
{
session_register($key); // Register $key (col name) with the session.
$_SESSION[$key][$val]; // Dump all values into session global.
}

return true;
}
?>
[/code]

I don't have the database set up yet on my host, so the SELECT statement in the above code is obviously invalid.  This is also the reason that I haven't live-tested yet to see the errors.  In any case, will this be a valid function?

Share this post


Link to post
Share on other sites
Yes. It is perfectly exceptable code and in fact even good practice to break your code into functions. Are you aware however the [i]session_register[/i] has long been deprictaed and unless your running a pretty old version of php is no longer required?

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=103225.msg410952#msg410952 date=1154914012]
Yes. It is perfectly exceptable code and in fact even good practice to break your code into functions. Are you aware however the [i]session_register[/i] has long been deprictaed and unless your running a pretty old version of php is no longer required?
[/quote]

I was actually not aware of that.  Thanks for the info, I'll drop it from my code.

Much appreciated.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.