我使用$_SESSION['name'];
后使用Notice: Undefined index: name in C:\Users\..\logged_in.php on line 2
从数据库中呼叫姓名我得到这个Hey, <?php echo $_SESSION['name']; ?>. You are logged in.
Try to close this browser tab and open it again. Still logged in! ;)
<a href="index.php?logout">Logout</a>
你能告诉我什么了吗?请帮忙...
登录in.php
<?php
/**
* Class login
* handles the user's login and logout process
*/
class Login
{
/**
* @var object The database connection
*/
private $db_connection = null;
/**
* @var array Collection of error messages
*/
public $errors = array();
/**
* @var array Collection of success / neutral messages
*/
public $messages = array();
/**
* the function "__construct()" automatically starts whenever an object of this class is created,
* you know, when you do "$login = new Login();"
*/
public function __construct()
{
// create/read session, absolutely necessary
session_start();
// check the possible login actions:
// if user tried to log out (happen when user clicks logout button)
if (isset($_GET["logout"])) {
$this->doLogout();
}
// login via post data (if user just submitted a login form)
elseif (isset($_POST["login"])) {
$this->dologinWithPostData();
}
}
/**
* log in with post data
*/
private function dologinWithPostData()
{
// check login form contents
if (empty($_POST['user_name'])) {
$this->errors[] = "Username field was empty.";
} elseif (empty($_POST['user_password'])) {
$this->errors[] = "Password field was empty.";
} elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {
// create a database connection, using the constants from config/db.php (which we loaded in index.php)
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// change character set to utf8 and check it
if (!$this->db_connection->set_charset("utf8")) {
$this->errors[] = $this->db_connection->error;
}
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
// escape the POST stuff
$user_name = $this->db_connection->real_escape_string($_POST['user_name']);
// database query, getting all the info of the selected user (allows login via email address in the
// username field)
$sql = "SELECT user_name, user_email, user_password_hash
FROM users
WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';";
$result_of_login_check = $this->db_connection->query($sql);
// if this user exists
if ($result_of_login_check->num_rows == 1) {
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// write user data into PHP SESSION (a file on your server)
$_SESSION['user_name'] = $result_row->user_name;
$_SESSION['user_email'] = $result_row->user_email;
$_SESSION['user_login_status'] = 1;
} else {
$this->errors[] = "Wrong password. Try again.";
}
} else {
$this->errors[] = "This user does not exist.";
}
} else {
$this->errors[] = "Database connection problem.";
}
}
}
/**
* perform the logout
*/
public function doLogout()
{
// delete the session of the user
$_SESSION = array();
session_destroy();
// return a little feeedback message
$this->messages[] = "You have been logged out.";
}
/**
* simply return the current state of the user's login
* @return boolean user's login status
*/
public function isUserLoggedIn()
{
if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
return true;
}
// default return
return false;
}
}
的login.php
{{1}}
答案 0 :(得分:1)
也许无法创建会话。您在文件中使用了start_session()
吗?
echo
用户名,如<?php echo (isset($_SESSION['name'])) ? $_SESSION['name'] : 'guest'; ?>
,并查看是否已创建会话。
答案 1 :(得分:0)
我在session_start
函数中看到了Login::__construct
,但在尝试访问new Login
个变量之前,您是否构建了$_SESSION
?我没有看到......我建议确保session_start()
在您的初始化代码中的某处,在其他任何地方之前调用。
答案 2 :(得分:0)
我假设有一些其他代码调用登录脚本。无论如何,该通知意味着该名称&#39; key没有在$ _SESSION中注册,这是有道理的,因为我认为你的意思是&#39; user_name&#39;。
所以试试:
Hey, <?php echo $_SESSION['user_name']; ?>. You are logged in.
Try to close this browser tab and open it again. Still logged in! ;)
<a href="index.php?logout">Logout</a>
否则你必须注册&#39;名称&#39;在会话中,假设$ result_row具有name属性:
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// write user data into PHP SESSION (a file on your server)
$_SESSION['name'] = $result_row->name;
$_SESSION['user_name'] = $result_row->user_name;
$_SESSION['user_email'] = $result_row->user_email;
$_SESSION['user_login_status'] = 1;
}
答案 3 :(得分:0)
1st。请将您的session_start()
移到一个文件中,该文件将包含在您的所有php文件中,删除来自您的Login类:)
也许您可以将其移动到将常量设置为DB_HOST
,DB_USER
等的位置。
第二次检查您的$ _SESSION密钥:)您正在设置$_SESSION['user_name']
,但尝试抓住$_SESSION['name']
答案 4 :(得分:0)
您无法在不启动会话的情况下访问$_SESSION
变量。此外,如果您已经在php文件中的任何地方启动过会话,那么您将收到一条会话已经启动的警告。因此,在访问$_SESSION
变量之前,请考虑使用以下语句:
if( !session_id() ) session_start();