Jump to content

Functions and $_SESSION


mewhocorrupts

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?
Link to comment
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?
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.