using session in class?
Posted: Fri Dec 12, 2008 2:47 am
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)
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:
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
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?
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
?>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
?>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 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?