我在functions.php文件中添加了这行代码
public function login(){
$sql = "SELECT `ID`,`userName` FROM `userdata` WHERE `userName`=:userN AND `password`=:passd";
$stmt = $this->pdo->prepare($sql);
$stmt -> bindValue(":userN",$this->userName);
$stmt ->bindValue(":passd",$this->password);
$stmt ->execute();
$userData = $stmt ->fetch();
$check = $stmt ->rowCount();
if($check==1){
session_start();
$_SESSION['login'] = true;
$_SESSION['uid'] = $userData['ID'];
$_SESSION['uName'] = $userData['userName'];
$_SESSION['Name'] = $userData['Name'];
$_SESSION['login_msg'] = "Login Successful";
return true;
}else{
return false;
}
}
public function getSession(){
return @$_SESSION['login'];
}
在我的index.php文件中,我想获取需要来自mysql数据库的Name。
session_start();
include_once "functions.php";
$object = new functions();
$userName = $_SESSION['uName'];
$Name = $_SESSION['Name'];
if(!$object->getSession()){
header("Location: login.php");
exit();
}
echo "Welcome". $Name;
但这并没有显示index.php页面。有什么问题?
答案 0 :(得分:0)
试试这个
session_start();
public function login(){
$sql = "SELECT `ID`,`userName` FROM `userdata` WHERE `userName`=:userN AND `password`=:passd";
$stmt = $this->pdo->prepare($sql);
$stmt -> bindValue(":userN",$this->userName);
$stmt ->bindValue(":passd",$this->password);
$stmt ->execute();
$userData = $stmt ->fetch();
$check = $stmt ->rowCount();
if($check==1){
$_SESSION['login'] = true;
$_SESSION['uid'] = $userData['ID'];
$_SESSION['uName'] = $userData['userName'];
$_SESSION['Name'] = $userData['Name'];
$_SESSION['login_msg'] = "Login Successful";
return true;
}else{
return false;
}
}
您甚至不必使用函数来获取会话。您可以随时随地使用$ _SESSION [' login']。但请确保您必须在要使用会话的所有文件中包含functions.php。