【问题标题】:php calling variables from another script to assign for the sessionphp 从另一个脚本调用变量以分配给会话
【发布时间】:2014-08-22 05:11:44
【问题描述】:

我在一个单独的脚本(main.php)中有一个登录功能,如下所示

public function login($username,$password) {
$linkingcon = $this->getConnection();
$sqlquery = "SELECT ac.userID,ac.name,us.usertype FROM users us JOIN accounts ac ON us.userID = ac.userID WHERE us.username='$username' AND us.password='$password';";
$result = mysql_query($sqlquery , $linkingcon);
$this->throwMySQLExceptionOnError($linkingcon);
$row = mysql_fetch_array($result);
$survey = new stdClass();
if($row) {
    $res->userID = (int)$row['userID'];
    $res->name = $row['name'];
    $res->usertype = (int)$row['usertype'];
            $string = rand() . 'SurveyLand' . rand() . $username. $password;
        $_SESSION['SURVEYLAND_KEY'] = md5($string);
} else {
    $res = false;
}
return $res;

}

我从另一个脚本调用上述登录函数,但我目前无法调用上述函数的“usertype”变量...下面是我编写的调用“usertype”的函数,任何人都可以检查下面的函数有什么问题

function login($parameters) {
$main = new Main();
$log = $main->login($parameters["username"], $parameters["password"]);
if($log != false){
    $_SESSION['usertype'] = $res->usertype;
    print_r($_SESSION);
    }
return $log;

}

【问题讨论】:

  • 您遇到的错误是什么?第二个代码中的“$pro”是什么?
  • 你试过打印 $res->usertype;在那边?它是印刷价值吗?

标签: php session login session-variables


【解决方案1】:

尝试在第二种方法中将 $res 更改为 $log。 $res 是您在第一种方法中使用的变量名,但在第二种方法中超出了范围。但是,您将第一种方法(即 $res)的响应分配给第二种方法中的 $log。

function login($parameters) {
  $main = new Main();
  $log = $main->login($parameters["username"], $parameters["password"]);
  if($log != false){
    $_SESSION['usertype'] = $log->usertype;
    print_r($_SESSION);
  }
  return $log;
}

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多