0

嗨,我正在尝试从 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结果如何解决?

4

1 回答 1

0

首先,如果您要发布表单,则应使用 $_POST['userID'] 并首先检查它是否检索 $_POST['userID'] 中的值,然后您必须像这样开始会话

session_start();
$_SESSION['userID'] = $_POST['userID']

但是第一个代码我知道您正在使用 codeigniter 或 OOP $user->user_id 第一个 $user 是您的变量,第二个是 user_id 这可能是您的访问修饰符还检查 $user->user_id 它具有值然后做同样的事情

session_start();
$_SESSION['userID'] = $user->user_id;
于 2013-10-02T17:30:08.997 回答