Php 为什么不';是否将我的会话变量传递到其他页面?
我有一个类验证器,它使用Datatype User登录用户Php 为什么不';是否将我的会话变量传递到其他页面?,php,oop,session,login,Php,Oop,Session,Login,我有一个类验证器,它使用Datatype User登录用户 include('User.datatype.php'); $usher = new Authenticator; $usher->checkCreds(); $usher->startSession(); Class Authenticator { protected $user; protected function getCreds() { if (!isset($_POST['lo
include('User.datatype.php');
$usher = new Authenticator;
$usher->checkCreds();
$usher->startSession();
Class Authenticator {
protected $user;
protected function getCreds() {
if (!isset($_POST['login']))
throw new Exception("There was an error processing your request", 1);
else if ($_POST['username'] == '' || $_POST['password'] == '')
throw new Exception("You must enter a username and password", 1);
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$this->user = new User;
$this->user->username = $username;
$this->user->password = $password;
}
public function checkCreds() {
$this->getCreds();
if (empty($this->user->username) || empty($this->user->password))
throw new Exception("Error Processing Request", 1);
include('dbconnect.php'); // Normally I'd store the db connect script outside of webroot
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare('SELECT username FROM Users WHERE username = :uname AND password = :pword');
$stmt->bindParam(':uname', $this->user->username);
$stmt->bindParam(':pword', $this->user->password);
$stmt->execute();
$status = $stmt->fetch(PDO::FETCH_NUM);
$this->user->status = $status;
}
protected function createSessionID() {
$seshID = mt_rand(99999, 1000000000);
return $seshID;
}
public function startSession() {
if ($this->user->status === false)
throw new Exception("There was a problem connecting to the database", 1);
session_start();
$_SESSION['username'] = $this->user->username;
$_SESSION['id'] = $this->createSessionID();
$secret = $_SESSION['id'];
header('Location:loggedin.php?' . $secret);
return true;
}
}
登录工作,会话启动()工作,但当我尝试
打印“欢迎”$_会话['username']
在loggedin.php上,会话变量为空
loggedin.php的HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product Cost Calculator</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="container">
<?php
/*require_once ('Authenticator.php');
if (!Authenticator::startSession())
print 'you are not logged in';*/
print 'Welcome, ' . $_SESSION['username'];
?>
</div>
</body>
</html>
产品成本计算器
您应该在当前页面中设置会话以使用会话相关代码
session_start();
include('User.datatype.php');
//rest of code etc etc
你必须先开始会话对不起,在哪里?HTML还是在PHP函数中?在每个PHP调用、POST或GET中,通过ajax、常规浏览器请求或提交操作,会话必须从php文件的顶部开始。-在知道使用了正确的登录信息之前,我不想启动会话。但是有两个不同的东西:php会话和应用程序的应用程序会话层。后者需要前者工作,但应该是独立的。哦,我明白了,它不会覆盖任何内容?
session\u start()
不会覆盖会话,而是初始化它session\u destroy()
-顾名思义-销毁整个会话。当我添加它时,我得到以下警告:无法发送会话缓存限制器-标头已发送如果在调用sessoin\u start()之前通过echo
、print
或仅在PHP标记之外发送一些数据(到浏览器),则会发生此问题
。