Welcome Guest, Not a member yet? Register   Sign In
Having problems with data persistency - or how to keep a DB connection alive?
#1

[eluser]duartix[/eluser]
Cheers.

Here's my problem. I've just discovered that PHP (and a lot of web protocols) are stateless and that all variables are lost between requests... Sad

This is posing me with a big problem. I have the need to keep the login information in order to re-establish the database connection between requests. I've stored that information on user session variables through my home controller, like this:

Code:
$this->session->set_userdata('username', $_POST['username']);
$this->session->set_userdata('password', $_POST['password']);
$this->session->set_userdata('hostname', $_POST['hostname']);

and then I set them on database.php like this:

Code:
$CI =& get_instance();
$db['oracle']['username'] = $CI->session->userdata('username');
$db['oracle']['password'] = $CI->session->userdata('password');
$db['oracle']['hostname'] = $CI->session->userdata('connectString')

so that when the db connection is loaded, those values are read.

I've successfully implemented a login mechanism this way, but now the problem is that as soon as I load the first model to fetch data from the database, I don't have that session data anymore and when CI goes through database.php it has no access to those parameters anymore...

To add insult to injury, the database where the users connect to, are dynamic, so I will need constant access to username/password/SID (they are all Oracle) to load the database in the models.

Any suggestions on how to approach this problem?

Thank you.
#2

[eluser]jairoh_[/eluser]
maybe there is some part in your code that you have destory/unset the session?
#3

[eluser]duartix[/eluser]
[quote author="jairoh_" date="1380070109"]maybe there is some part in your code that you have destory/unset the session?[/quote]
I do have some destroying code but that is only called when the user clicks the logout button, so that's not the reason.

However I have managed to find a solution. It seems that there is an upper level of persistency. I changed my previous code to:

home controller:
Code:
$_SESSION["username"] = $_POST['username'];
$_SESSION["password"] = $_POST['password'];
$_SESSION["hostname"] = $_POST['hostname'];

database.php
Code:
$db['oracle']['username'] = $_SESSION["username"];
$db['oracle']['password'] = $_SESSION["password"];
$db['oracle']['hostname'] = $_SESSION["hostname"];

This will survive the page transition. Smile
Now what still puzzles me is why the user session data before that was lost. I didn't have any session_start statements before i started using the superglobal $_SESSION. Could that be the issue?

(EDIT) Well, I've tested it and that was it . My initial approach was correct, the only issue with it was session management. Now that I have the session_start statements (from the second approach) it 's working.




Theme © iAndrew 2016 - Forum software by © MyBB