您好我正试图从sessein数组中的表中的userID列获取值。
在我的注册表中我有以下代码:
<?php
class Users {
public $username = null;
public $password = null;
public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w";
public function __construct( $data = array() ) {
if( isset( $data['username'] ) ) $this->username = stripslashes( strip_tags( $data['username'] ) );
if( isset( $data['password'] ) ) $this->password = stripslashes( strip_tags( $data['password'] ) );
}
public function storeFormValues( $params ) {
//store the parameters
$this->__construct( $params );
}
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();
$valid = $stmt->fetchColumn();
if( $valid ) {
$success = true;
$_SESSION['userID'] = $user->user_id;
}
对于哪种语法,我不太确定它是否会记录已登录用户的UserID值。
并且在创建数组的登录中我有
<?php
session_start();
include_once("config.php"); //include the settings/configuration
?>
<?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'];
$_SESSION['userID'] = $_POST['userID'];
但它每次都给我NULL结果如何修复它?
答案 0 :(得分:0)
首先,如果您要发布表单,则应该使用$ _POST ['userID']并首先检查它,如果您在$ _POST ['userID']中检索该值,那么您必须像这样启动会话< / p>
session_start();
$_SESSION['userID'] = $_POST['userID']
但是第一个代码我知道你使用的是codeigniter或OOP $ user-&gt; user_id第一个$ user是你的变量,第二个是user_id,可能是你的访问修饰符也检查$ user-&gt; user_id it有价值然后做同样的
session_start();
$_SESSION['userID'] = $user->user_id;