Page 1 of 1

using session in class?

Posted: Fri Dec 12, 2008 2:47 am
by eskio
Hi,I used the mysql.user table to authenticate the users. So I used the following class to connect to the database (in the login form)

Code: Select all

<?php
class connect_db {
private $MySQLDatabaseName='mysql';
private $MySQLDatabaseLogin='mylogin'; 
private $MySQLDatabasePassword='mypass';   
private $HostServerName="localhost";
public $linkdb;
 
function __construct(){
    $this->linkdb=@mysql_connect($this->HostServerName,$this->MySQLDatabaseLogin,
        $this->MySQLDatabasePassword) or die("Couldn't connect to MySQL!");
    @mysql_select_db($this->MySQLDatabaseName,$this->linkdb) or die("Error: ".mysql_error
 
().'<br>Error No: '. mysql_errno());
} // function __construct()
 
function closedb(){
    @mysql_close($this->linkdb) or die("Error: ".mysql_error());
} //function closedb()
 
}  // class connect_db 
?>
Once the user’s information is correct (login & password), I want to use this information to access my database. So I create another class to connect to my database:

Code: Select all

<?php
class connect_db {
 
private $MySQLDatabaseName='mydbname';
private $MySQLDatabaseLogin= $_SESSION['login'] ; 
private $MySQLDatabasePassword= $_SESSION['pwd'];   
private $HostServerName="localhost";
public $linkdb;
 
// Constructor
function __construct(){
    $this->linkdb=@mysql_connect($this->HostServerName,$this->MySQLDatabaseLogin,
        $this->MySQLDatabasePassword) or die("Couldn't connect to MySQL!");
    @mysql_select_db($this->MySQLDatabaseName,$this->linkdb) or die("Error: ".mysql_error
 
().'<br>Error No: '. mysql_errno());
} // function __construct()
 
function closedb(){
    @mysql_close($this->linkdb) or die("Error: ".mysql_error());
} //function closedb()
 
}  // class connect_db 
?>
But when I tried to connect using this class, I have the following error: Parse error: parse error, unexpected T_VARIABLE in W:\www\project\cls_db_session_connect.php on line 5
I understand that it couldn’t read the $_SESSION['login'] as well as the $_SESSION['pwd'] session variables.
When I use

Code: Select all

<?php
if (!isset($_SESSION['login'])){print 'Impossible to connect to database';exit;} 
 
global $MySQLDatabaseName;
global $linkdb;
$MySQLDatabaseName= 'mydbname';
$linkdb=@mysql_connect('localhost',$_SESSION['login'],$_SESSION['pwd']) or die("Couldn't connect to DB! ".mysql_error());
@mysql_select_db($MySQLDatabaseName,$linkdb) or die("Error: ".mysql_error().'<br>Error No: '. mysql_errno());
 
function closedb_session($linkdb){
    @mysql_close() or die("Error: ".mysql_error());
} //function closedb()
?>
I do not have a connection problem. It works very well.
I want to know that it means it is not possible to use session variable into a class? and how can I use a session into a class?

Re: using session in class?

Posted: Fri Dec 12, 2008 3:27 am
by Mark Baker
It is possible within the methods.

get the class constructor to populate the $MySQLDatabaseLogin and $MySQLDatabasePassword attributes, with 'mylogin'and 'mypass' as defaults (as defined in your first version of the connect_db class). Then you don't need two versions of the connect_db class either.

Re: using session in class?

Posted: Wed Dec 31, 2008 7:35 am
by eskio
It works using class constructor but my problem now is how can I use the PASSWORD() function (within PHP) which MySQL uses to encrypt the password. I have to encrypt 'mypass' for comparision in MySQL.