我有一个带登录功能的类文件
当用户已经注册时,我在登录功能中创建会话但是当我在另一个页面中获取该会话时,它显示空会话
operation.php
session_start();
class Operation
{
private $_conn;
public function __construct()
{
// code of connection with DB
$this->_conn = $con;
// connection succcessfully done
}
function login() {
$login_sql = $this->_conn->prepare("SELECT * FROM tbl_user WHERE (email=:email OR mobile=:mobile) AND password=:password");
$login = $login_sql->execute(array('email' => $email,'mobile' => $mobile,'password' => $password));
$res_login = mysqli_num_rows($login);
if ($res_login == 1) {
$res_login = mysqli_fetch_assoc($login);
$login_arr = array('user_id' =>$res_login['id'],'first_name' =>$res_login['first_name'],'last_name' =>$res_login['last_name'],'email' =>$res_login['email'],'mobile' =>$res_login['mobile'],'point'=>($res_login['accumulated_points']-$res_login['used_points']));
$_SESSION["login_data"] = $login_arr;
print_r($_SESSION); // Here session print successfully
echo "User successfully login";
} else {
echo "invalid user";
}
}
}
op = new Operation();
$op->login();
get_data.php
session_start();
print_r($_SESSION);
首先,我在浏览器选项卡上运行operation.php它会显示会话数据,但之后我在另一个选项卡中运行get_data.php它会显示空数组。
请帮助我,我错了...
答案 0 :(得分:0)
将这些值硬编码到类代码中通常是不切实际的,所以我会选择构造函数方法:
<?php
class pardEngine{
private $hostname;
private $database;
private $database_user;
private $database_user_pass;
public function __construct($hostname, $database, $database_user, $database_user_pass){
$this->hostname = $hostname;
$this->database = $database;
$this->database_user = $database_user;
$this->database_user_pass = $database_user_pass;
}
}
session_start();
$engine = new pardEngine($_SESSION['HOSTNAME'], $_SESSION['DATABASE'], $_SESSION['USER']);