【发布时间】:2013-10-02 18:34:39
【问题描述】:
您好,我可能会发布关于此问题的第四个问题,但到目前为止没有人可以帮助我,所以让我们从头开始,我需要的很简单,我需要:从名为“userID”的表中设置 userID 变量users" 到 Sessions 数组中:$_SESSION['userID']
这是我的登录页面代码:
<?php
session_start();
include_once("config.php"); //include the settings/configuration
?>
<?php if( !(isset( $_POST['login'] ) ) ) { ?>
<?php
//else look at the database and see if he entered the correct details
} else {
session_start();
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $usr->userLogin() ) {
header( 'Location: cursos.php' ) ;
$_SESSION["loggedIn"] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
这是我操作登录功能的用户类:
public function userLogin() {
$success = false;
try{
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
$success = true;
if ($result!==false) { // Check there is a match
$_SESSION['userID']= $result['userID'];
}
$success = true;
$con = null;
return $success;
}catch (PDOException $e) {
echo $e->getMessage();
return $success;
代码运行良好,我只想在其中添加一些内容,以便在用户登录后从数据库中的 users 表的列中获取 userID 值并将其存储到 $_SESSION['userID'] 中。
那么知道如何在我的代码中实现这个目标吗?谢谢!
【问题讨论】:
-
到目前为止您还没有收到任何回复的原因可能是因为您应该更清楚您已经完成的工作,以及它如何无法按照您想要的方式工作。问题出在哪里,究竟是什么在您的代码中不起作用?
-
header()函数应该在您设置 $_SESSION 值之后进行,以便您在设置之后重定向页面。您还需要确定(阅读疑难解答)您的登录代码是否正常工作。 -
这个
$_SESSION['password'] = $_POST['password'];不安全。您将用户密码以明文形式保存在文本文件中。 -
你好 Alexis 你对这个问题有什么建议?
标签: php